2016-04-25 15 views
0

Ich habe einige Schwierigkeiten mit plotly. Ich möchte plotly als pdf herunterladen können. Doch während einige x- und y-Achse Parameter zu meinem Code hinzufügen (Ursache, wenn ich übertragen ggplot-plotly, Titel von x und y-Achse geschnitten)GGPlotly: downloadHandler geben leeres Grundstück

Dieser Code arbeitet pdf-Datei zum Download:

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

und Zugabe dieser Code die Titel des ggplotly zu beheben fehlschlägt:

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

ein leeres Grundstück gibt ...

Danke für jede Hilfe!

+0

Aber wo genau sollte ich verwenden 'return (a)' ..Than i für if-Anweisung verwenden müssen 'downloadHandler' –

+0

Wenn Sie plotly haben user_name etc können Sie' plotly_IMAGE' dafür. oder Sie müssen eine andere Funktion für plotly und eine für pdf ggplot verwenden – Batanichek

+0

Ich habe plotly user_name nicht. Das Problem mit "plotly" ist, dass ich den ** plotty download image button ** im Internet Explorer nicht benutzen kann, deshalb ist die einzige Lösung, 'downlaodHandler' hinzuzufügen, die mir pdf-Datei geben wird –

Antwort

0

Ich habe meine Frage gelöst. Die Lösung ist nicht elegant, aber es funktioniert!

Der Trick besteht also darin, die X- und Y-Titel in renderPlotly und NICHT in testplot() Funktion zu setzen.

Allerdings müssen die x- und y-Achsentitel zusätzlich in die testplot() Funktion eingegeben werden - dies wird unsere Ausgabe als pdf sein, und die Ansicht des Diagramms erfolgt mit plotly.

Hier Code:

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