2016-08-20 2 views
-1

반짝 이는 애플 리케이션에 새로운 도움이 필요합니다. 파일을 업로드하고 반짝이는 앱에서 탁상용 출력을 생성하는 데는 도움이 필요하지만 pdf로 플롯을 다운로드 할 수는 없습니다. 형식 여기 내 코드PDF 파일을 다운로드 한 후 반짝이는 앱을 사용하여 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

시도가 오류 중 하나가 우리가 기반으로 PDF 파일에 고유 한 개별 플롯을 인쇄 할 수 있습니다 –

답변

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

주셔서 감사합니다 나를 인도 할 수 팝업 플롯을 다운로드 신분증과 파일 다운로드 –