如何在Shiny中动态生成的conditionalPanel中格式化条件?

时间:2018-01-19 15:04:13

标签: r loops shiny conditional panel

我正在尝试使用for循环在Shiny中创建小部件。每个块包含:

  1. 标签
  2. 复选框
  3. 选择选择器
  4. 两个数字输入
  5. 我想根据复选框的值和选择选择器的值来显示或隐藏两个数字输入。 在我创建的for循环中,我为每个widget变量添加了一个索引。 我想知道如何在for循环中执行条件面板的条件,知道变量在其名称中被索引。谢谢你的帮助。我与您分享了server.R和UI.R脚本:

     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 个答案:

答案 0 :(得分:0)

你的第一个条件应该是:

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

和第二个:

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

所以他们变成了i=1

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

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

分别

工作示例:

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)