How to format the condition in a dynamically generated conditionalPanel in Shiny?

送分小仙女□ 提交于 2021-01-28 01:12:19

问题


I am trying to create widgets in Shiny by using for loop. each block contains:

  1. label
  2. check box
  3. choices selector
  4. Two numeric inputs

I want to do condition for showing or hiding the two numeric inputs according to the value of the check box and the value of the choices selector. In the for loop that I created I added an index for each widget variable. I am wondering how can I execute the condition of the conditional panel in the for loop knowing that the variables are indexed in their names. Thanks for helping. I share with you the server.R and the UI.R scripts:

 library(shiny)
shinyServer(function(input, output) {

  output$input_value2 <- renderUI({
    fluidRow(

      lapply(1:8, function(i) {

        column(width = 4,
               output[[paste0('b', i)]] <- renderUI({strong(paste0('Variable N ', i))}),
               checkboxInput(label = 'Inclued',paste0('c', i)),
               selectInput(paste0('popDist', i), "Distribution",list("Normal" = "normal","Uniforme" = "uniforme")),

               conditionalPanel(
                 condition = "eval(parse(text=paste0('input.popDist',i)))== 'uniforme'& (eval(parse(text=paste0('input.c',i)))==1",
                 numericInput(paste0('min', i), label = "Min:", value = 1),
                 numericInput(paste0('max', i), label = "Max:", value = 2)
               ),

               conditionalPanel(
                 condition = "eval(parse(text=paste0('input.popDist',i)))== 'uniforme'& (eval(parse(text=paste0('input.c',i)))==1",

                 numericInput(paste0('mean', i), label ="mean:", value = 0),
                 numericInput(paste0('sd', i), label = "st. dev.:", value = 1)

               )
        )
      })
    )
  })
})

    library(shiny)
shinyUI(fluidPage(
  titlePanel("TEST conditional panel"), 
  uiOutput("input_value2")
  ))

回答1:


Your first condition should be:

paste0("input.popDist",i,"== 'uniforme'& input.c",i,"==1")

and the second:

paste0("input.popDist",i," == 'normal'& input.c",i,"==1")

so they become, with i=1:

"input.popDist1== 'uniforme'& input.c1==1"

and

"input.popDist1 == 'normal'& input.c1==1"

respectively.

Working example:

library(shiny)
library(shinyjs)
server <- shinyServer(function(input, output) {

  output$input_value2 <- renderUI({
    fluidRow(

      lapply(1:8, function(i) {

        column(width = 4,
               output[[paste0('b', i)]] <- renderUI({strong(paste0('Variable N ', i))}), 
               checkboxInput(label = 'Inclued',paste0('c', i)),
               selectInput(paste0('popDist', i), "Distribution",list("Normal" = "normal","Uniforme" = "uniforme")),

               conditionalPanel(
                 condition = paste0("input.popDist",i,"== 'uniforme'& input.c",i,"==1"),
                 numericInput(paste0('min', i), label = "Min:", value = 1),
                 numericInput(paste0('max', i), label = "Max:", value = 2)
               ),

               conditionalPanel(
                 condition = paste0("input.popDist",i," == 'normal'& input.c",i,"==1 "),

                 numericInput(paste0('mean', i), label ="mean:", value = 0),
                 numericInput(paste0('sd', i), label = "st. dev.:", value = 1)

               )   
        )
      })
    )
  })

})

library(shiny)
ui <- shinyUI(fluidPage(
  useShinyjs(),
  titlePanel("TEST conditional panel"), 
  uiOutput("input_value2")
))

shinyApp(ui,server)


来源:https://stackoverflow.com/questions/48343968/how-to-format-the-condition-in-a-dynamically-generated-conditionalpanel-in-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!