2017-05-15 25 views
0

作爲示例,假設我有三個不同的「函數」fun1(),fun2() and fun3(),它們分別輸出數字#1, #2, and #3checkboxGroupInput - 在Shiny中按字符串執行多個函數

如果我用閃亮的下列選項:

checkboxGroupInput("selec",label = h3("Select function"), 
choices = list("fun1" = 1,"fun2" = 2, "fun3" = 3)) 

然後我可以運行「功能」我選擇(一次一個),但我怎麼能運行所有我選擇的?

例如,如果我選擇fun1 and fun3,我想要的功能#1 and #3

輸出,如果我選擇fun1 and fun2,我想要的功能輸出#1 and #2

如果讓我選擇他們,我想所有的輸出

感謝您幫助

+0

嗨,albert,你提到sthg關於多個輸出。我認爲它是由答案涵蓋的?也許你可以提供更多的細節,.. – BigDataScientist

回答

0

我建議的do.call的組合(功能調用)和sapply()(用於遍歷數組了),見下文:

fun1 <- function() return("#1") 
fun2 <- function() return("#2") 
fun3 <- function() return("#3") 

library(shiny) 
ui <- fluidPage(
    checkboxGroupInput("functions", "Choose functions:", 
        choiceNames = 
         list("fun1", "fun2", "fun3"), 
        choiceValues = 
         list("fun1", "fun2", "fun3") 
), 
    textOutput("txt") 
) 

server <- function(input, output, session) { 
    output$txt <- renderText({ 
    print(sapply(input$functions, do.call, args = list())) 
    }) 
} 

shinyApp(ui, server)