2014-12-03 15 views
11

Có phương pháp để xuất (giao diện người dùng) Các ô sáng bóng sang PDF để người dùng ứng dụng tải xuống không? Tôi đã thử các phương pháp khác nhau tương tự như những người liên quan đến ggplot, nhưng có vẻ như downloadHandler không thể hoạt động theo cách này. Ví dụ sau đây chỉ tạo ra các tệp PDF bị hỏng mà không mở.Xuất lô đất sáng bóng (không phải ggplot) sang PDF

library(shiny) 
runApp(list(
    ui = fluidPage(downloadButton('foo')), 
    server = function(input, output) { 
    plotInput = reactive({ 
     plot(1:10) 
    }) 
    output$foo = downloadHandler(
     filename = 'test.pdf', 
     content = function(file) { 
     plotInput() 
     dev.copy2pdf(file = file, width=12, height=8, out.type="pdf") 
     }) 
    } 
)) 

Rất biết ơn sự trợ giúp.

+0

Để tham khảo, @ phương pháp Victorp của dưới đây vẫn thất bại khi lưu trữ tại [shinyapps.io] (https://geotheory.shinyapps.io/pdf_test/) với 'Lỗi: chỉ có thể in từ một thiết bị màn hình'. – geotheory

Trả lời

6

Giải Quyết. Cốt truyện phải được lưu cục bộ với pdf(), không phải thiết bị màn hình (như với dev.copy2pdf). Dưới đây là ví dụ làm việc: shiny::runGist('d8d4a14542c0b9d32786'). Đối với mô hình đẹp thử cơ bản:

server.R

library(shiny) 
shinyServer(
    function(input, output) { 

     plotInput <- reactive({ 
      if(input$returnpdf){ 
       pdf("plot.pdf", width=as.numeric(input$w), height=as.numeric(input$h)) 
       plot(rnorm(sample(100:1000,1))) 
       dev.off() 
      } 
      plot(rnorm(sample(100:1000,1))) 
     }) 

     output$myplot <- renderPlot({ plotInput() }) 
     output$pdflink <- downloadHandler(
      filename <- "myplot.pdf", 
      content <- function(file) { 
       file.copy("plot.pdf", file) 
      } 
     ) 
    } 
) 

ui.R

require(shiny) 
pageWithSidebar(
    headerPanel("Output to PDF"), 
    sidebarPanel(
     checkboxInput('returnpdf', 'output pdf?', FALSE), 
     conditionalPanel(
      condition = "input.returnpdf == true", 
      strong("PDF size (inches):"), 
      sliderInput(inputId="w", label = "width:", min=3, max=20, value=8, width=100, ticks=F), 
      sliderInput(inputId="h", label = "height:", min=3, max=20, value=6, width=100, ticks=F), 
      br(), 
      downloadLink('pdflink') 
     ) 
    ), 
    mainPanel({ mainPanel(plotOutput("myplot")) }) 
) 
3

(Xin chào), chỉ cần sử dụng pdf:

library(shiny) 
runApp(list(
    ui = fluidPage(downloadButton('foo')), 
    server = function(input, output) { 
    plotInput = reactive({ 
     plot(1:10) 
    }) 
    output$foo = downloadHandler(
     filename = 'test.pdf', 
     content = function(file) { 
     pdf(file = file, width=12, height=8) 
     plotInput() 
     dev.off() 
     }) 
    } 
)) 

EDIT: Tôi không biết ... Đó là lạ. Một cách giải quyết là sử dụng dev.copy2pdf như bạn đã làm ở nơi đầu tiên nhưng trong reactive chức năng thay vì downloadHandler:

## server.R 
library(shiny) 
shinyServer(
    function(input, output) { 
    plotInput <- reactive({plot(rnorm(1000)) 
          dev.copy2pdf(file = "plot.pdf") 
          }) 
    output$myplot <- renderPlot({ plotInput() }) 
    output$foo <- downloadHandler(
     filename <- "plot.pdf", 
     content <- function(file) { 
     file.copy("plot.pdf", file) 
     }) 
    } 
) 
+1

Có, tôi đang sử dụng Windows, tôi cũng đang gặp khó khăn trên Linux, nhưng tôi không có máy Mac ... – Victorp

+0

Có lỗi, xin lỗi Victorp Tôi cần phải mở lại vì giải pháp này dành riêng cho Windows và cũng không thành công cho các ứng dụng được lưu trữ tại 'shinyapps.io'. – geotheory

+0

Không sao cả, tôi tò mò về câu trả lời. – Victorp

Các vấn đề liên quan