Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R studio shiny conditional statements

Tags:

r

shiny

I've been playing around with R shiny, and have a question.

I want to create a multi-tab multi-dataset package. As the different datasets are not overly related, I want the user to be able to select which they want to look at and for that to change what filtering options are available for them to then use.

So I want something like this,

selectInput("variable", "Variable:",
list(""Cylinders" = "cyl",
"Transmission" = "am"),

if (selectInput == "Transmission") {
    sliderInput("integer", "Integer:", 
    min=0, max=1, value=0) },

else{

 sliderInput("decimal", "Decimal:", 
             min = 0, max = 1, value = 1) }

how do I do a conditional in Shiny? Treating it like a normal R conditional doesn't seem to work.

like image 663
cianius Avatar asked Nov 09 '12 18:11

cianius


1 Answers

conditionalPanel is what you want. http://rstudio.github.com/shiny/tutorial/#dynamic-ui

selectInput("variable", "Variable:",
list(""Cylinders" = "cyl",
"Transmission" = "am"),

conditionalPanel(condition = "input.variable == 'am'",
    sliderInput("integer", "Integer:", 
    min=0, max=1, value=0)),

conditionalPanel(condition = "input.variable == 'cyl'",
 sliderInput("decimal", "Decimal:", 
             min = 0, max = 1, value = 1))

(I haven't actually tried to run this code but you should be able to get the idea)

Note that conditionalPanel just hides the control, the child control still exists whether it is showing or not. In your server logic, you'll have to use if (input$variable == 'am') to see what your mode you're in (which is probably the natural way anyway) rather than testing for the existence of input$integer or input$decimal.

like image 60
Joe Cheng Avatar answered Oct 18 '22 10:10

Joe Cheng