【问题标题】:R Shiny rendering UI widgets interactivelyR Shiny 以交互方式呈现 UI 小部件
【发布时间】:2017-06-14 19:49:22
【问题描述】:

我正在尝试构建一个应用程序,允许用户从预先提​​供的列表中选择一个或多个函数(模块),然后根据他们的选择,为该函数的各种参数选择值。

基本上,我正在尝试重建 renderUI 调用树,以便当闪亮的反应部分发生并且调用服务器函数时,如果 input$abcd.in 不为 NULL,则使用不同的参数运行 renderUI 函数取决于 input$abcd.in 的值是什么。

错误似乎是 set_output_automatically 函数的输入参数的长度为 0,但我不知道下一步该做什么。

这是一个简化的例子

library(shiny)

arg.info <-  list(
  list(choices = c("Yes" = T, "No" = F),
                  prompt = quote(paste("Remove Missing Values for analysis?")),
                  type = "radioButtons"),
  list(choices = c("not" = 0, "some" = 1, "very" = 3),
       prompt = quote(paste("how cool are you?")),
       type = "checkboxGroupInput"))


set_output_automatically <- function(input, arg.info){

  if(input[1] > 3){
    arg.info <- arg.info[[1]]
  } else {
    arg.info <- arg.info[[2]]
  }

  renderUI({

    call(arg.info$type, paste0(input$abcd, ".in"), label = arg.info$prompt,
         choices = arg.info$choices)
  })


}

ui <- fluidPage(

  uiOutput('abcd'),

  textOutput('value'),

  uiOutput('result')


)

server <- function(input, output){

  output$abcd <- renderUI({

    checkboxGroupInput('abcd.in', 'sample',
                       choices = c('wowe'= 1,
                                   'such' = 2,
                                   'choice' = 3,
                                   'very' = 4,
                                   'programe' = 5))

  })

  output$value <- renderPrint({input$abcd.in})

  output$result <- reactive(set_output_automatically(input$abcd.in, arg.info))

}

shinyApp(ui, server)

【问题讨论】:

    标签: r functional-programming shiny


    【解决方案1】:

    我用do.call(有一个参数列表)替换了call,用renderUI替换了反应式,这行得通。

    library(shiny)
    
    arg.info <-  list(
      list(choices = c("Yes" = T, "No" = F),
           prompt = quote(paste("Remove Missing Values for analysis?")),
           type = "radioButtons"),
      list(choices = c("not" = 0, "some" = 1, "very" = 3),
           prompt = quote(paste("how cool are you?")),
           type = "checkboxGroupInput"))
    
    
    set_output_automatically <- function(input, arg.info){
      if(input[1] > 3){
        arg.info <- arg.info[[1]]
      } else {
        arg.info <- arg.info[[2]]
      }
      do.call(arg.info$type, args=list(inputId=paste0("in", input), label = arg.info$prompt,
              choices = arg.info$choices))
    
    }
    
    ui <- fluidPage(
      uiOutput('abcd'),
      textOutput('value'),
      uiOutput('result')
    )
    
    server <- function(input, output){
    
      output$abcd <- renderUI({
        checkboxGroupInput('abcd.in', 'sample',
                           choices = c('wowe'= 1,
                                       'such' = 2,
                                       'choice' = 3,
                                       'very' = 4,
                                       'programe' = 5))
        })
    
      output$value <- renderPrint({input$abcd.in})
    
      output$result <- renderUI({
        set_output_automatically(input$abcd.in, arg.info)
      })
    
    }
    

    【讨论】:

    • 谢谢!! call 和 do.call 之间到底有什么区别? do.call 是否只是创建一个调用,然后在 as call 只是创建调用时对其进行评估?
    • Call 只返回函数调用,但不评估它(因此do.call 中的“do”,因为它相同但已评估)。 do.call("mean", list(5,6)) 给出 5.5,但 call("mean", 5,6) 给出未评估的 mean 调用,带有 5 和 6:mean(5, 6) :)
    • 补充:您可以使用eval 评估调用:myCall &lt;- call("mean", c(5,6)); eval(myCall) 产生5.5
    猜你喜欢
    • 1970-01-01
    • 2017-05-28
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多