2016-08-20 72 views
-1

因爲,我是新的閃亮的應用程序需要一些幫助,上傳Excel文件和生成閃亮的應用程序的表格輸出工作正常,但不能夠下載到PDF格式的情節格式 這裏是我的代碼在閱讀excel文件後使用閃亮的應用程序下載PDF圖

library(shiny) 
library(openxlsx) 
library(lattice) 

runApp(
    list(
    ui = fluidPage(
     titlePanel("plots"), 
     sidebarLayout(
     sidebarPanel(
      fileInput('file1', 'Choose xlsx file', 
        accept = c(".xlsx")), 
      tags$hr(), 
      downloadButton('down',"download plot") 
     ), 
     mainPanel(
      tableOutput('contents'), 
     plotOutput('plot')) 
    ) 
    ), 
    server = function(input, output){ 
     output$contents <- renderTable({ 
     inFile <- input$file1 

     if(is.null(inFile)) 
      return(NULL) 
     else 
     read.xlsx(inFile$datapath) 
     }) 

     plotInput <- reactive({ 
     df <- input$file1 
     xyplot(df[,2]~df[,1],df(),xlim=c(0,10),ylim=c(0,100),type = "b") 
     }) 

     output$plot <- renderPlot({ 
     print(plotInput()) 
     }) 

     output$down <- downloadHandler(
     filename = function(){paste("plot",".pdf",sep=".") }, 
     content = function(file) { 
      pdf(file) 
     xyplot(df[,2]~df[,1],df(),xlim=c(0,10),ylim=c(0,100),type = "b") 
      dev.off() 
     } 
    ) 
    } 
) 
) 
+0

時嘗試下載錯誤彈出任何一個可以指導我 –

回答

1

的問題是,在你的代碼的某些部分你通過df()訪問一個動態的數據幀,但你從來沒有定義它。

在這種類型的問題中,最好創建一個反應數據幀,如df,其中包含上傳數據並通過df()傳遞給代碼的其他反應部分。


完整的示例:

library(shiny) 
library(openxlsx) 
library(lattice) 

runApp(
    list(
    ui = fluidPage(
     titlePanel("plots"), 
     sidebarLayout(
     sidebarPanel(
      fileInput('file1', 'Choose xlsx file', 
        accept = c(".xlsx")), 
      tags$hr(), 
      downloadButton('down',"download plot") 
     ), 
     mainPanel(
      tableOutput('contents'), 
      plotOutput('plot')) 
    ) 
    ), 
    server = function(input, output){ 


     df <- reactive({ 
     inFile <- input$file1 
     req(inFile) # require that inFile is available (is not NULL) 
        # (a user has uploaded data) 

     # read.xlsx(inFile$datapath) 
     head(iris, 10) 
     }) 

     output$contents <- renderTable({ 
     # access uploaded data via df() 
     df() 
     }) 

     plotInput <- reactive({ 
     df <- df() 
     xyplot(df[,2]~df[,1], df ,xlim=c(0,10),ylim=c(0,100),type = "b") 
     }) 

     output$plot <- renderPlot({ 
     plotInput() 
     }) 

     output$down <- downloadHandler(
     filename = function(){paste("plot",".pdf",sep=".") }, 
     content = function(file) { 
      pdf(file) 
      #xyplot(df[,2]~df[,1],df(),xlim=c(0,10),ylim=c(0,100),type = "b") 

      # you have to print the plot so that you can open pdf file 
      print(plotInput()) 
      dev.off() 
     } 
    ) 
    } 
) 
) 
+1

感謝您的解決方案 –

+0

基於PDF格式才能打印獨特的個別地塊的情節ID並下載文件 –