I have a shiny app that allows a user to choose what to plot on the x and y axis based on a loaded data frame. I am trying to allow the user to download the current plot view. I am opening launching the app in Google Chrome because I know it does not work to save a file if the app is launched within R studio. As of now it saves a png file, but it is blank.
Screenshot of app UI can be seen here
I have used these posts to try to resolve the issue:
Downloading png from Shiny (R)
Downloading png from Shiny (R) pt. 2
The app does not work if vis()
is changed to a function rather than a reactive. However, when filteredData()
is changed from a reactive to a function, the app still works the same. However, a blank png is still yield if within downloadHandler(...
I have vis()
, filteredData()
or print(filteredData())
. If print(vis())
is used, another window pops up in the browser that says "Key / already in use". A minimal working code that replicates the issue is below and any help you can provide would be much appreciated.
#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)