Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Last active August 8, 2017 04:26
Show Gist options
  • Save aagarw30/d227e0131c2d81f47496fb1476699df8 to your computer and use it in GitHub Desktop.
Save aagarw30/d227e0131c2d81f47496fb1476699df8 to your computer and use it in GitHub Desktop.
Plotly, Dygraphs and R Shiny, save as png
Title: Experiment with plotly and dygraphs and save the output plot. Used export() function.
Description: Experiment with plotly and dygraphs and save the output plot. Used export() function to save the plot.
License: GPL-3
Author: Abhinav Agrawal
DisplayMode: Showcase
Tags: R, R Shiny
Type: Shiny
library(shiny)
library(plotly)
library(dygraphs)
path <- getwd()
setwd(path)
shinyServer(function(input, output, session) {
output$plot_plotly <- renderPlotly({
p <- plot_ly(x = c(1,2,3,4), y = c(2,4,1,3), type = 'scatter', mode = 'lines')
export(p, file = "plotlyplot.png")
return(p)
})
output$plot_dy <- renderDygraph({
lungDeaths <- cbind(mdeaths, fdeaths)
q <- dygraph(lungDeaths)
export(q, file = "dygraph.png")
return(q)
})
})
library(shiny)
library(plotly)
library(dygraphs)
shinyUI(
fluidPage(
titlePanel("Plotly, Dygraphs and R Shiny, save as png"),
sidebarLayout(
sidebarPanel(),
mainPanel(
plotlyOutput("plot_plotly"),
dygraphOutput("plot_dy")
)
)
)
)
@vikram-rawat
Copy link

I ran the Code but it doesn't work it gives me an error saying

no applicable method for 'plotly_build' applied to an object of class "c('dygraphs', 'htmlwidget')"

What should I do now. Please let me know...

@vikram-rawat
Copy link

I ran the code like

library(shiny)
library(plotly)
library(dygraphs)
library(htmlwidgets)

path <- getwd()
setwd(path)

ui<-shinyUI(
fluidPage(
titlePanel("Plotly, Dygraphs and R Shiny, save as png"),
sidebarLayout(

        sidebarPanel(),
        mainPanel(
            plotlyOutput("plot_plotly"),
            dygraphOutput("plot_dy")
            
            
        )
    )
)

)
server<-shinyServer(function(input, output, session) {

output$plot_plotly <- renderPlotly({
    p <- plot_ly(x = c(1,2,3,4), y = c(2,4,1,3), type = 'scatter', mode = 'lines')
    export(p, file = "plotlyplot.png")
    return(p)
    
})

output$plot_dy <- renderDygraph({
    lungDeaths <- cbind(mdeaths, fdeaths)
    q <- dygraph(lungDeaths)
    export(q, file = "dygraph.png")
    return(q)
    
})

})

shinyApp(ui,server)

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