在 R markdown 报告中使用来自 Shiny 应用程序的图表和数据表
Using plots and data tables from Shiny app in R markdown report
我创建了一个 Shiny 应用程序,它涉及用户导入数据、操作数据、创建数据table 和相关绘图。
我想使用 Rmarkdown(我才刚刚开始使用)构建可下载的报告。但是,我不确定如何在 Rmarkdown 脚本中打印 R 中生成的数据 table 和绘图,而不从 Shiny 应用程序复制整个 R 代码。这是一段很长的代码,所以我希望能够直接使用输出。
作为示例,我复制了以下应用程序来证明我的观点。
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 markdown 文件中的绘图和数据 table。
我的方法,使用 app.R 和 test.Rmd
在app.R
创建一个包含 plot/chart 的反应变量(替换为您自己的图)。
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())
注意图表1后面的()!!!
对表以及您希望包含在降价中的所有其他对象遵循相同的结构..
我创建了一个 Shiny 应用程序,它涉及用户导入数据、操作数据、创建数据table 和相关绘图。
我想使用 Rmarkdown(我才刚刚开始使用)构建可下载的报告。但是,我不确定如何在 Rmarkdown 脚本中打印 R 中生成的数据 table 和绘图,而不从 Shiny 应用程序复制整个 R 代码。这是一段很长的代码,所以我希望能够直接使用输出。
作为示例,我复制了以下应用程序来证明我的观点。
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 markdown 文件中的绘图和数据 table。
我的方法,使用 app.R 和 test.Rmd
在app.R 创建一个包含 plot/chart 的反应变量(替换为您自己的图)。
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())
注意图表1后面的()!!!
对表以及您希望包含在降价中的所有其他对象遵循相同的结构..