RustyStatistician RustyStatistician - 2 months ago 25
R Question

Conditional Panel in Shiny doesn't update variables

I am trying to use a conditional panel in R Shiny but am running into issues with values not being assigned. Here is a snippet of my code:

conditionalPanel(condition="input['input.type']=='Use Example Data'",
textInput("Label", "Enter the label:", "A"),
),
conditionalPanel(condition="input['input.type']=='Upload Data'",
textInput("Label", "Enter the label:", "B"),
)


Now, with this code, what I was expecting to happen is if the user selects "Use Example Data" then the default value would be A (which it is), but if the user toggles to "Upload Data" then the default values is still A and not B as I would have expected. It seems only the first conditional panel stores the values since they have the same name?

A second question, when the user toggles between "Use Example Data" and "Upload Data" the conditional panel does change, but if I replace the value of A with, say, C and then toggle back and forth, the value of C will always be there instead of being reset to A. Is there a simple fix for this?

Answer

Is this what you want?

rm(list = ls())
library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    selectInput("type", "Type:",c("Use Example Data","Upload Data"))),
  dashboardBody(
    uiOutput("myui")    
  )
)
server <- function(input, output) {
  output$myui <- renderUI({
    if(input$type == "Upload Data"){
      textInput("Label", "Enter the label:", "B")
    }
    else{
      textInput("Label", "Enter the label:", "A")
    }
  }) 
}
shinyApp(ui, server)

enter image description here