如何在Shiny中动态生成的conditionalPanel中格式化条件? [英] How to format the condition in a dynamically generated conditionalPanel in Shiny?

查看:75
本文介绍了如何在Shiny中动态生成的conditionalPanel中格式化条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用for循环在Shiny中创建小部件.每个块包含:

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

  1. 标签
  2. 复选框
  3. 选择器
  4. 两个数字输入

我想为根据复选框的值和选择选择器的值显示或隐藏两个数字输入做条件.在我创建的for循环中,我为每个小部件变量添加了一个索引.我想知道如何知道变量已在其名称中索引,如何在for循环中执行条件面板的条件.感谢您的帮助.我与您共享server.R和UI.R脚本:

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")
  ))

推荐答案

您的第一个条件应该是:

Your first condition should be:

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)

这篇关于如何在Shiny中动态生成的conditionalPanel中格式化条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆