在 Shiny 应用程序中使用复选框切换 ggvis-layer

Toggle ggvis-layer with checkbox in a Shiny application

我想添加一个复选框,用于切换 Shiny 应用程序中 ggvis 图中显示的图层。

library(shiny)
library(ggvis)

ui <- shinyUI(fluidPage(sidebarLayout(
   sidebarPanel(
   checkboxInput('loess','loess',TRUE)
  ),
   mainPanel(
   ggvisOutput("plot")
  )
 )))


server <- shinyServer(function(input, output) {

 mtcars %>%
    ggvis(~wt, ~mpg) %>%
    layer_points() %>%
     # if(input$loess) layer_smooths() %>%
    bind_shiny("plot", "plot_ui")
 })

shinyApp(ui = ui, server = server)

这是否可以在 ggvis 管道中使用与上面代码中注释行相同的要点来完成?

我认为您不能为此直接使用管道。您还需要处于 reactive 环境中才能访问 input$loess。你可以这样做:

observe({
        plot <- mtcars %>%
                ggvis(~wt, ~mpg) %>%
                layer_points()
        if(input$loess) plot <- plot %>% layer_smooths()
        plot %>% bind_shiny("plot", "plot_ui")
})