Search code examples
rggplot2shinyplotlyr-plotly

Changing Dimensions of downloaded plot of plotly in r shiny


I have a code which downloads the png image of the graph formed in plotly with same dimensions everytime. I want to generate a image with higher quality in it. Like if i click on the buttons provided at the top right corner in plotly graphs, it should download with different dimensions

Reference code is given below

library(shiny)
library(plotly)

ui <- fluidPage(
  selectInput("choice", "Choose", choices = names(iris), selected = NULL),
  plotlyOutput("graph")
  )

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

  output$graph <- renderPlotly({
    plot_ly(iris, x = ~get(input$choice), y = ~Sepal.Length, type = 'scatter', mode = 'markers')
  })
}

shinyApp(ui, server)

Solution

  • You can use toImageButtonOptions in the config function to set the dimensions:

    plot_ly(
      iris, x = ~get(input$choice), y = ~Sepal.Length, type = 'scatter', mode = 'markers'
    ) %>% config(
      toImageButtonOptions = list(format = "png", width = 1500, height = 750)
    )