2017-03-01 28 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} 
    })