1

I have used the below code to create checkbox from my data.I would like to create slider input for each checkbox I select from the list.For example if the checkbox has 4 variables like "sky","earth","water","fire" and if I select sky, it should dynamically open a slider input for sky and if I select water it should open up one more slider input for water. I tried conditionalPanel,but I have more than 50 variables in my checkbox,so i cannot write condition for all the 50 variables. Is there any generalized method available in shiny?

server

output$choosedigital=renderUI({

if(is.null(bk$variables))
   return()

checkboxGroupInput("choosemedia", "Choose digital", 
                    choices  = bk$variables,
                    selected = bk$variables)
})


  output$test <- renderUI({
  LL <- list(rep(0,length(input$choosedigital)))     
  for(i in 0:(length(input$choosedigital))) {
   LL[i] <- list(sliderInput(inputId = paste(input$choosedigital,i)
                             , label = paste(input$choosedigital,i), 
  min=0,max=25,value = 5))
  }       
  return(LL)                      
  })
BKS
  • 11
  • 5
  • I think you need to loop with `lapply`. Please show a small reproducible example – akrun Feb 05 '18 at 09:06
  • bk=mydigital output$choosemedia=renderUI({ if(is.null(bk$variables)) return() checkboxGroupInput("choosemedia", "Choose digital", choices = bk$variables, selected = bk$variables) }) ; code for slider input ; output$test=renderUI({ if (is.null(input$choosemedia)) return() switch(input$choosemedia, sliderInput("dynamic", "Dynamic", min = 1, max = 20, value = 10)) }) – BKS Feb 05 '18 at 09:23
  • I have updated my code using for loop.But still its not working.Any suggestions? – BKS Feb 06 '18 at 08:30

1 Answers1

1

You want to put your sliderInputs inside a conditionalPanel in the UI and set the condition so that when the relevant checkbox is clicked the the condition equates to TRUE.

e.g.

library(shiny)
myData = c("One", "Two", "Three")

ui <- fluidPage(

  checkboxGroupInput("choosemedia", "Choose digital", 
                     choices  = myData,
                     selected = myData),
  textOutput("myText"),
  conditionalPanel(condition = "input.choosemedia.includes('One')", 
                   sliderInput("sliderOne", "Choose your value", min=0, max=100, value=50)
  ),
  conditionalPanel(condition = "input.choosemedia.includes('Two')",
                   sliderInput("sliderTwo", "Choose your other value", 

min=0, max=50, value=25))
    )

# Define server logic 
server <- function(input, output) {
  output$myText <- renderText({input$choosemedia})

}
# Run the application 
shinyApp(ui = ui, server = server)

If long as you know what the content of bk$variables is you can hardcode them, otherwise you'll have to generate these on the fly.

Hope this is enough info to get you going.

Hester Lyons
  • 803
  • 11
  • 24
  • Thanks for the solution,but what i wanted is slightly different.I don't want to write conditional panel for each input,because i have more than 50 variables inside the checkbox. – BKS Feb 05 '18 at 09:21
  • Is it not possible to generate the conditionalpanels using a loop and renderUI? I will admit I haven't ventured there. – Hester Lyons Feb 05 '18 at 09:32
  • I think this might give you the answer: [link](https://stackoverflow.com/questions/31732810/how-to-make-conditional-panels-with-dynamically-created-tabs-in-shiny-r) – Hester Lyons Feb 05 '18 at 09:34
  • I tried replicating using the code given in the link The UI as follows conditionalPanel(condition="input.level==1", sliderInput('clusters', 'Cluster count', 9, min = 1, max = 9)) Server output$test=renderUI({ Tabs<-input$choosemedia for (i in 1:length(Tabs)){ Tabs[i]=lapply(paste("input.level==",i,sep=""),conditionalPanel,sliderInput('clusters', 'Cluster count', 9, min = 1, max = 9)) } return(Tabs) }) – BKS Feb 05 '18 at 11:07