Plotly "Download as png" image resizing when used in RShiny application

I use the package plotlyto Rdisplay some large graphs in the application shiny, the graphs display well as intended. However, when I click the "Download as png" button, the downloaded .png has been changed. here is a demonstration of this behavior .

How can I specify permission for the exported site?

Here is a minimal example that demonstrates the problem, having a very long header that gets cut at boot

app.R

library(shiny)
library(ggplot2)
library(plotly)

ui <- fluidPage(titlePanel("Plotly Download demo"),

                plotlyOutput("demoPlotly"))


server <- function(input, output) {
  output$demoPlotly <- renderPlotly({
    #make an arbritrary graph with a long title
    p <- iris %>%
      ggplot(aes(x = Petal.Length, y = Petal.Width, color = Species)) +
      geom_point() +
      labs(title = "This is a really long title to demo my problem of plotly resizing my downloaded output")
    ggplotly(p)

  })
}

# Run the application
shinyApp(ui = ui, server = server)

The loaded graph is as follows: Cropped graphic

+4
source share
1 answer

Michael

plotly_IMAGE(). [ 4.7.1]. . , 800 600.

, , , . , . plotly_IMAGE, - , . .

,

.


- :

, plotly api. ~/.Renviron. : https://plot.ly/r/getting-started/

.

plotly_username="xxxxx"
plotly_api_key="xxxxxx"

library(shiny)
library(ggplot2)
library(plotly)

wrapper <- function(x, ...) {
  paste(strwrap(x, ...), collapse = "\n")
}

# Example usage wrapper(my_title, width = 20)

my_title <- "This is a really long title to demo my problem of plotly resizing my downloaded output"

ui <- fluidPage(titlePanel("Plotly Download demo"), plotlyOutput("demoPlotly"))

pal <- c("blue", "red", "green")
pal <- setNames(pal, c("virginica", "setosa", "versicolor"))

layout <- list(title = wrapper(my_title, width = 60),
               xaxis = list(title = "Petal Length"),
               yaxis = list(title = "Petal Width"),
               margin = list(l = 50, r = 50, b = 100, t = 100, pad = 4))

server <- function(input, output) {
  output$demoPlotly <- renderPlotly({
    # make an arbitrary graph with a long title
    p <- iris %>%
      plot_ly(x = ~Sepal.Length,
              y = ~Petal.Width,
              color = ~Species,
              colors = pal,
              mode = "markers",
              type = "scatter") %>%
      layout(autosize = F,
             title = layout$title,
             xaxis = layout$xaxis,
             yaxis = layout$yaxis,
             margin = layout$margin)
    p$elementId <- NULL
    # Example usage of plotly image
    plotly_IMAGE(p, 
                 width = 800, 
                 height = 600, 
                 format = "png", 
                 scale = 1, 
                 out_file = "output.png")
    p
  })
}

# Run the application
shinyApp(ui = ui, server = server)

-

Screen Image


-

Save as PNG Image


plotly_IMAGE() 800 600

enter image description here

+3

Source: https://habr.com/ru/post/1685760/


All Articles