R 以交互方式渲染UI小部件

R 以交互方式渲染UI小部件,r,functional-programming,shiny,R,Functional Programming,Shiny,我正在尝试构建一个应用程序,允许用户从预先提供的列表中选择一个或多个函数(模块),然后根据他们的选择,为该函数的各种参数选择值 基本上,我正在尝试重建renderUI调用树,以便在发生响应部分时调用服务器函数,如果输入$abcd.in不为NULL,renderUI函数将使用不同的参数运行,具体取决于输入$abcd.in的值 错误似乎是set_output_automatically函数的输入参数的长度为0,但我不知道接下来要做什么 下面是一个简化的例子 library(shiny) arg.i

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

基本上,我正在尝试重建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)
库(闪亮)

arg.info我将
call
替换为
do.call
(有一个参数列表)并将reactive替换为
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)
  })

}
库(闪亮)

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