2017-03-01 70 views
0
library(shiny) 
ui <- fluidPage(
    checkboxGroupInput("data", "Select data:", 
        c("Iris" = "iris", 
         "Cars" = "mtcars")), 
    ##### 
    checkboxGroupInput("display", "Fit to data:", 
        c("Yes" = "fit", 
         "No"= "nofit")), 
    ##### 
    plotOutput("myPlot") 
) 

server <- function(input, output) { 
    dat <- reactive({ 
    switch() 
    }) 
    output$myPlot <- renderPlot({ 
    dat <- switch(input$data, 
        "iris" = iris, 
        "mtcars" = mtcars) 
    plot(Sepal.Width ~ Sepal.Length, data = get(input$data)) 
    }) 
} 


shinyApp(ui, server) 

我有一个复选框,让用户决定他/她是否希望数据适合。但是,我不确定我如何将其实施到我的server。从本质上讲,如果用户在复选框中选择Yes,那么我希望程序通过renderPlot,否则,不要麻烦。也许另一个switch包含我的renderPlot如何使用复选框来防止某些输出运行?

回答

0

只需添加一个开关,或者如果您的renderPlot()调用依赖于输入$显示。

output$myPlot <- renderPlot({ 
    if(input$display == 'fit') { 
     dat <- switch(input$data, 
        "iris" = iris, 
        "mtcars" = mtcars) 
     plot(Sepal.Width ~ Sepal.Length, data = get(input$data)) 
    } else {NULL} 
    })