Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Last active January 5, 2022 17:55
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aagarw30/87c14725be815f9fd038 to your computer and use it in GitHub Desktop.
Save aagarw30/87c14725be815f9fd038 to your computer and use it in GitHub Desktop.
R Shiny Download GGPLOT demo
library(shiny)
library(ggplot2)
shinyServer(function(input,output)({
# x contains all the observations of the x variable selected by the user. X is a reactive function
x <- reactive({
iris[,as.numeric(input$var1)]
})
# x contains all the observations of the y variable selected by the user. Y is a reactive function
y <- reactive({
iris[,as.numeric(input$var2)]
})
# xl contains the x variable or column name of the iris dataset selected by the user
xl <- reactive({
names(iris[as.numeric(input$var1)])
})
# yl contains the y variable or column name of the iris dataset selected by the user
yl <- reactive({
names(iris[as.numeric(input$var2)])
})
# render the plot so could be used to display the plot in the mainPanel
output$plot <- renderPlot({
# plot(x=x(), y=y(), main = "iris dataset plot", xlab = xl(), ylab = yl())
ggplot(iris, aes(x=x(), y=y())) + geom_point(shape=1) # ploting through GGPLOT
})
# downloadHandler contains 2 arguments as functions, namely filename, content
output$down <- downloadHandler(
filename = function() {
paste("iris", input$var3, sep=".")
},
# content is a function with argument file. content writes the plot to the device
content = function(file) {
if(input$var3 == "png")
png(file) # open the png device
else
pdf(file) # open the pdf device
# plot(x=x(), y=y(), main = "iris dataset plot", xlab = xl(), ylab = yl()) # draw the plot
print(ggplot(iris, aes(x=x(), y=y())) + geom_point(shape=1)) # for GGPLOT
dev.off() # turn the device off
}
)
}))
library(shiny)
shinyUI(fluidPage(
titlePanel("Download base plot in Shiny - an example"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "var1", label = "Select the X variable", choices = c("Sepal.Length" = 1, "Sepal.Width" = 2, "Petal.Length" = 3, "Petal.Width" = 4)),
selectInput(inputId = "var2", label = "Select the Y variable", choices = c("Sepal.Length" = 1, "Sepal.Width" = 2, "Petal.Length" = 3, "Petal.Width" = 4), selected = 2),
radioButtons(inputId = "var3", label = "Select the file type", choices = list("png", "pdf"))
),
mainPanel(
plotOutput("plot"),
downloadButton(outputId = "down", label = "Download the plot")
)
)
))
@mendeslr
Copy link

mendeslr commented Jan 5, 2021

Neat! Thank you for sharing te code for your download button.
In my case, I would like to specify the width and height using a variable I made elsewhere. The variable name is called plotWidth and plotHeight. I have tried:

pdf(file, width = plotWidth, height = plotHeight) but that did not work (although using pdf(file, width = 10, height = 5) works fine). Any suggestions?

The code I use to output the plot to the UI is:
output$facetedPlots <- renderPlot({ if (is.null(v$facetedPlots)) return() v$facetedPlots }, height = plotHeight, width = plotWidth )

And the currently working code for the download button is:
output$downPDF <- downloadHandler( filename = function() { paste("FACS Plot.pdf") }, # content is a function with argument file. content writes the plot to the device content = function(file) { pdf(file) # open the pdf device print(v$facetedPlots) # for GGPLOT dev.off() # turn the device off } )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment