R Shiny渲染UI小部件交互式

时间:2017-06-14 19:49:23

标签: r functional-programming shiny

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

基本上,我正在尝试重构renderUI调用树,以便在发生闪亮的反应部分并调用服务器函数时,如果输入$ abcd.in不为NULL,则使用不同的参数运行renderUI函数取决于输入$ 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)

1 个答案:

答案 0 :(得分:0)

我将call替换为do.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) }) } ,这可行。

{{1}}

enter image description here