2017-06-14 64 views
0

我想构建一个应用程序,它允许用户从预先提​​供的列表中选择一个或多个函数(模块),然后根据它们的选择为各种参数选择值功能。R交互式地发光渲染UI小部件

基本上,我试图重建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) 

回答

0

我与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) 
    }) 

} 

enter image description here

+0

谢谢!! call和do.call之间的区别究竟是什么? do.call只是创建一个调用,然后评估它在哪里调用简单地创建调用? – HuntAC

+0

调用只是返回函数调用,但不会对其进行评估(因此,do.call中的“do”因为它是相同的,但是被评估)。 “do.call(”意思是“,列表(5,6))给出5.5但是”call(“mean”,5,6)“给出了”5“和”6“的未评估调用:mean(5, 6)':) – shosaco

+0

增加:你可以用'eval'评估一个调用:'myCall < - call(“mean”,c(5,6)); eval(myCall)'产生'5.5' – shosaco