Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Last active January 5, 2018 07:46
Show Gist options
  • Save aagarw30/dfec0b2b59b34c4dea69e1adce0a9264 to your computer and use it in GitHub Desktop.
Save aagarw30/dfec0b2b59b34c4dea69e1adce0a9264 to your computer and use it in GitHub Desktop.
Save the plotly object to local system
library(shiny)
library(plotly)
library(ggplot2)
shinyServer(function(input,output)({
## Below plotting using plotly
output$plotlyplot <- renderPlotly({
p = plot_ly(data = iris, y=~Sepal.Length, type = "scatter")
# Using the export function to save the plot after the click of action button
observeEvent(input$savea, {
export(p, file.path("C:/Users/Abhinav/Desktop/", "imagep.png"))
})
return(p)
})
## Below plotting using ggplotly
output$ggplotly <- renderPlotly({
data <- data.frame(x = rnorm(1:1000), y = rnorm(1:1000))
g <- ggplot(data, aes(x = x, y = y)) + geom_point()
gg <- ggplotly(g)
# Using the export function to save the plot after the click of action button
observeEvent(input$saveb, {
export(gg, file.path("C:/Users/Abhinav/Desktop/", "imageg.png"))
})
return(gg)
})
}))
library(shiny)
library(plotly)
shinyUI(fluidPage(
titlePanel("Save plotly to local machine in Shiny - an example"),
sidebarLayout(
sidebarPanel(),
mainPanel(
plotlyOutput("plotlyplot"),
actionButton("savea", "Save the plotly plot"),
plotlyOutput("ggplotly"),
actionButton("saveb", "Save the ggplotly plot")
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment