2016-08-20 42 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并下载文件 –

相关问题