2016-04-25 15 views
0

plotlyでいくつかの問題があります。私はplotlyをpdfとしてダウンロードできるようにしたいと思います。しかし、このコードは、pdfファイルをダウンロードするために取り組んでいる(私はplotlyggplotを転送する場合は、xとy軸のタイトルがカットされている原因)私のコードにGGPlotly:空のプロットを与えるdownloadHandler

をいくつかxとy軸のパラメータを追加しているとき:

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)} 

が空のプロットを与える...

助けてくれてありがとう!

+0

しかし、場所を正確にあなたがplotly USER_NAMEなどあなたは '使用することができている場合は、私はのためにif文を使用する必要が..Thanリターンは、(a)は' ' –

+0

をdownloadHandler''使用する必要があります。ここでは

コードですplotly_IMAGE'を入力します。プロットには別の関数を使用し、pdfには1つの関数を使用する必要があります。ggplot – Batanichek

+0

私はplotly user_nameを持っていません。 'plotly'の問題は、Internet Explorerで画像のボタン**をプロットしてダウンロードすることができないということです。なぜなら、唯一の解決策は、私にpdfファイルを与える' downlaodHandler'を加えることだけです。 –

答えて

0

私は私の質問を解決しました。解決策はエレガントではありませんが、機能します!

だから、xとyのタイトルはrenderPlotlyに設定し、testplot()では設定しないでください。

x軸とy軸のタイトルは、testplot()関数に追加入力する必要があります。これは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() 
     })}) 
関連する問題