繁体   English   中英

从反应集中下载ggvis图

[英]Download ggvis plot from reactive set

我有一个闪亮的应用程序,允许用户根据加载的数据框选择在x和y轴上绘制的内容。 我试图允许用户下载当前的绘图视图。 我即将在Google Chrome浏览器中启动该应用程序,因为我知道如果该应用程序是在R studio中启动的,则无法保存文件。 截至目前,它已保存png文件,但它为空白。

可在此处查看应用程序用户界面的屏幕截图

我使用这些帖子来尝试解决问题:

从Shiny(R)下载png

从Shiny(R)pt下载png。 2

如果将vis()更改为函数而不是响应式,则该应用程序将无法运行。 但是,将filteredData()从响应式更改为函数时,该应用程序仍可以正常运行。 但是,如果在downloadHandler(...我有vis()filteredData()print(filteredData())内,仍然会产生空白png。如果使用了print(vis())则会在浏览器中弹出另一个窗口下面是复制该问题的最低限度工作代码,您所能提供的任何帮助将不胜感激。

#Check packages to use in library
library('shiny') #allows for the shiny app to be used
library('stringr') #string opperator
library('ggvis') #allows for interactive ploting
library('dplyr')

alldata <- iris

#establish options for drop down menus
specieschoices <- unique(as.character(alldata$Species))
petalwchoices <- unique(as.character(alldata$Petal.Width))
petallchoices <- unique(as.character(alldata$Petal.Length))
sepallchoices <- unique(as.character(alldata$Sepal.Length))
sepalwchoices <- unique(as.character(alldata$Sepal.Width))

# UI

ui<-fluidPage(
titlePanel("Explorer"),
fluidRow(
column(4,
       wellPanel(
         h4("Apply Filters"),
         selectInput(inputId = "species", label="Select a Species:", choices = sort(specieschoices), selected="setosa", multiple = TRUE, selectize = TRUE),
         selectInput(inputId = "petalw", label="Select Petal Width:", choices = sort(petalwchoices), selected=petalwchoices, multiple = TRUE, selectize = FALSE),
         selectInput(inputId = "petall", label="Select Petal Length", choices = sort(petallchoices), selected=petallchoices, multiple = TRUE, selectize = FALSE),
         selectInput(inputId = "sepall", label="Select Sepal Length", choices = sort(sepallchoices), selected=sepallchoices, multiple = TRUE, selectize = FALSE),
         selectInput(inputId = "sepalw", label="Select Sepal Width", choices = sort(sepalwchoices), selected=sepalwchoices, multiple = TRUE, selectize = FALSE),
         downloadButton('downloadPlot', 'Download Plot')
         )),
column(8,
       ggvisOutput("plot1")
),
column(4,
       wellPanel(
         h4("Data Variables"),
         selectInput(inputId = "x", label="Select x-axis Variable:", choices=as.character(names(alldata[,1:4])),selected='Petal.Length', multiple = FALSE),
         selectInput(inputId = "y", label="Select y-axis Variable:", choices=as.character(names(alldata[,1:4])),selected='Petal.Width', multiple = FALSE)
       ))
))

#SERVER
server<-function(input,output,session)
{

#Set up reactive variables
filteredData <- reactive({

# Apply filters
m <- alldata %>% filter(
  `Species` %in% input$species,
  `Petal.Width` %in% input$petalw,
  `Petal.Length` %in% input$petall,
  `Sepal.Width` %in% input$sepalw,
  `Sepal.Length` %in% input$sepall
)
m <- droplevels(as.data.frame(m))
m
})

vis <- reactive({

xvar <- prop("x", as.symbol(input$x))
yvar <- prop("y", as.symbol(input$y))

p1 = filteredData() %>%
  ggvis(x = xvar, y = yvar) %>%
  layer_points(size.hover := 200,
               fillOpacity:= 0.5, fillOpacity.hover := 1,
               fill = ~Species
  )

})

#Actually plots the data
vis %>% bind_shiny("plot1")

################# THIS IS THE PART THAT I NEED HELP WITH#####################
output$downloadPlot <- downloadHandler(
  filename = paste0(input$y, '_vs_', input$x, "_", Sys.Date(), ".png"),
  content = function(file) {
    png(file)
    vis()
    dev.off()
  },
  contentType = 'image/png'
)
##############################################################################
}

#Run the Shiny App to Display Webpage
shinyApp(ui=ui, server=server)

我通过创建另一个函数来解决此问题,该函数创建了一个ggplot图,该图是我的ggvis图的精确副本,可以与downloadHandler一起使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM