2017-10-23 2 views
0

で光沢のあるアプリからプロットやデータテーブルを使用しました。私は、ユーザデータテーブルと関連付けられたプロットを作成し、それを操作し、データをインポートすることを含む光沢のあるアプリを作成したRマークダウンレポート

私は(私はちょうど使用し始めました)Rmarkdownを使用してダウンロード可能なレポートで構築したいと思います。しかし、私は光沢のあるアプリから全体のRコードをコピーせずに、Rmarkdownスクリプトでは、Rで生成されたDataTableのプロットを印刷する方法がわからないと思います。これは、コードのかなり長い作品ですので、私は直接出力を使用することができるようにしたいと思います。

例として、私は私のポイントを証明するために、次のアプリをコピーしました。

library(shiny) 

ui <- fluidPage(

    titlePanel("Old Faithful Geyser Data"), 

    sidebarLayout(
     sidebarPanel(
     sliderInput("bins", 
        "Number of bins:", 
        min = 1, 
        max = 50, 
        value = 30) 
    ), 

     mainPanel(
     plotOutput("distPlot"), 
     dataTableOutput("summary_table"), 
     downloadButton("report", "Generate report") 
    ) 

    ) 
) 

server <- function(input, output) { 

    output$distPlot <- renderPlot({ 
     x <- faithful[, 2] 
     bins <- seq(min(x), max(x), length.out = input$bins + 1) 

     hist(x, breaks = bins, col = 'darkgray', border = 'white') 
    }) 

    output$summary_table <- renderDataTable({ 
    x <- faithful 
    head(x,5) 
    }) 

    output$report <- downloadHandler(
    filename = "report.html", 
    content = function(file) { 
     tempReport <- file.path(tempdir(), "report.Rmd") 
     file.copy("report.Rmd", tempReport, overwrite = TRUE) 
     rmarkdown::render(tempReport, output_file = file, 
         envir = new.env(parent = globalenv()) 
     ) 
    } 
    ) 
} 

shinyApp(ui = ui, server = server) 

私は私のダウンロードRマークダウンファイルにプロットし、データテーブルにアクセスしたいと思います。 app.R にapp.Rとtest.Rmd

を使用して

答えて

1

私のアプローチは、プロット/チャートを(自分のプロットに置き換え)を含む、反応性変数を作成します。

chart1 <- reactive({ 
    ggmap(get_map(location = "Netherlands", 
       zoom = 7, 
       scale = 2, 
       color="bw", 
       language = "nl-NL")) 
    }) 

、その後、値下げを呼び出します。

output$report <- downloadHandler(
filename = "test.pdf", 

content = function(file) { 
    src <- normalizePath('test.Rmd') 

    #switch to system tempdir 
    #just in case the app doesn't have write persission in the current folder 
    owd <- setwd(tempdir()) 
    on.exit(setwd(owd)) 
    file.copy(src, 'test.Rmd', overwrite = TRUE) 

    out <- render("test.Rmd", "pdf_document") 
    file.rename(out, file) 
} 

.Rmdファイルに、あなたがして、チャート上で呼び出すことができます。

plot(chart1()) 

注() chart1の後に!

+0

..あなたがあなたの値下げに含める他のすべてのオブジェクト、テーブルに同じ構造に従ってください、私は私のプロットのコードは 'renderPlotly'機能に包まれているように、上記を実現するために見えることはできません。このような出力を抽出できますか? – sym246

+0

私は分かりません。プロットでの経験はありません。 – Wimpel

関連する問題