从闪亮中删除ggvis图

时间:2016-06-10 19:03:02

标签: r plot shiny ggvis

我在Shiny中有多个ggvis图。 我需要提供一个操作按钮,如果单击该按钮,则需要删除所有绘图。

以下是ui.Rserver.R的示例代码:

ui.R

library(ggvis)
library(shiny)

shinyUI(fluidPage(
  titlePanel("Plotting slopes"),

  sidebarLayout(
  sidebarPanel(
  selectInput("segment", label = "Choose segment", choices = c("K 1", "K 2")),
  actionButton("abutton","Delete plots")),

mainPanel(ggvisOutput("plot"), ggvisOutput("plot2"))
)
))

server.R

library(shiny)
library(ggvis)

mtcars$cyl = factor(mtcars$cyl)
df1 = subset(mtcars, am == 0)
df2 = subset(mtcars, am == 1)

shinyServer(function(input, output) {
dataInput = reactive({
switch(input$segment,
       "K 1" = df1,
       "K 2" = df2)
})


values = function(x){
if(is.null(x)) return(NULL)
dat = dataInput()
row = dat[dat$cyl %in% unique(x$cyl), ]
paste0("Ave Weight: ", mean(row$wt),"<br />",
       "Ave Carb: ", mean(row$carb), "<br />")
}


vis1 = reactive({
dat = dataInput()
dat %>%
  group_by(cyl) %>%
  ggvis(~mpg, ~wt)  %>%
  layer_paths(stroke = ~cyl, strokeOpacity := 0.3, 
              strokeWidth := 5) %>%
  add_tooltip(values, "hover")
 })
  vis1 %>% bind_shiny("plot")

 vis2 = reactive({
dat = dataInput()
dat %>%
  group_by(cyl) %>%
  ggvis(~mpg, ~wt)  %>%
  layer_paths(stroke = ~cyl, strokeOpacity := 0.3, 
              strokeWidth := 5) %>%
  add_tooltip(values, "hover")
  })
  vis2 %>% bind_shiny("plot2")

})

当前输出的屏幕截图:screenshot

1 个答案:

答案 0 :(得分:1)

如果您容忍未删除但删除了LOOK LIKE的图表,我认为很容易给bind_shiny()一个空白图表。

server.R

  :
  vis2 %>% bind_shiny("plot2")    # the same up to here

  vis3 = mtcars %>%                   # preparation of a blank graph
    ggvis(~mpg, ~wt, opacity := 0) %>%
    layer_points() %>%
    hide_axis("x") %>%
    hide_axis("y")

  observeEvent(input$abutton, {    # When the button is clicked,
    bind_shiny(vis3, "plot")       # bind_shiny() reads and outputs a blank graph, vis3.
    bind_shiny(vis3, "plot2")      # When other Input is done, vis1 and vis2 return.
  }) 

})