2017-06-14 68 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