繁体   English   中英

GGPlotly:downloadHandler提供空图

[英]GGPlotly: downloadHandler giving empty plot

我在plotly上有些困难。 我希望能够下载plotly为PDF格式。 但是,在向我的代码中添加一些x和y轴参数(因为如果我将ggplot转移到plotlyplotly x和y轴的标题切除)

此代码正在下载pdf文件:

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


shinyApp(
  ui = fluidPage(
    fluidRow(downloadButton('downloadplot',label='Download Plot')),
    plotlyOutput('plot1')
  ),
  server = function(input, output) {

    testplot <- function(){

a <- ggplot(mtcars, aes(x = interaction(cyl, carb, lex.order = T), y = mpg,fill = interaction(cyl, carb, lex.order = T))) + 
  geom_boxplot()

    }

    output$plot1 <- renderPlotly({testplot()})

    output$downloadplot <- downloadHandler(
      filename ="plot.pdf",
      content = function(file) {
        pdf(file, width=12, height=6.3)
        print(testplot())
        dev.off()
      })})

并且添加以下代码来修复ggplotly的标题失败:

a <- ggplot(mtcars, aes(x = interaction(cyl, carb, lex.order = T), y = mpg,fill = interaction(cyl, carb, lex.order = T))) + 
  geom_boxplot()

p <- ggplotly(a + ylab(" ") + xlab(" ")) 
x <- list(
  title = "[x]"
)
y <- list(
  title = "[y]"
)
p %>% layout(xaxis = x, yaxis = y)}

给出一个空的情节...

谢谢你的帮助!

我已经解决了我的问题。 解决方案不是很好,但是可以!

所以诀窍是在renderPlotly设置x和y标题,而在testplot()函数中不设置。

但是,必须在testplot()函数中另外键入x和y轴标题-因为这将作为我们的pdf输出,并且使用plotly进行图的查看。

这是代码:

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


shinyApp(
  ui = fluidPage(
    fluidRow(downloadButton('downloadplot',label='Download Plot')),
    plotlyOutput('plot1')
  ),
  server = function(input, output) {

    testplot <- function(){

      a <- ggplot(mtcars, aes(x = interaction(cyl, carb, lex.order = T), y = mpg,fill = interaction(cyl, carb, lex.order = T))) + 
        geom_boxplot()

    }

    output$plot1 <- renderPlotly({

      p <- ggplotly(testplot() + ylab(" ") + xlab(" ")) 
      x <- list(
        title = "[x]"
      )
      y <- list(
        title = "[y]"
      )
      p %>% layout(xaxis = x, yaxis = y)})

    output$downloadplot <- downloadHandler(
      filename ="plot.pdf",
      content = function(file) {
        pdf(file, width=12, height=6.3)
        print(testplot())
        dev.off()
      })})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM