【发布时间】:2021-08-13 12:49:18
【问题描述】:
我有一个 Rmarkdown 文件,我想在闪亮的应用程序中显示,还有一个下载按钮来下载它。其中大部分工作正常,但我在 Rmarkdown 文档中有一个DT::datatable。下载并打开后,数据表在 Rmarkdown 中呈现良好,但该表不会显示在闪亮的应用程序本身中。
有没有办法让数据表也显示在应用程序中?我不想使用 kable 等,因为带有数据表的下载和过滤选项非常有用。
Rmarkdown("report.Rmd"):
---
title: "Example report"
output: html_document
params:
n: NA
---
```{r}
# The `params` object is available in the document.
params$n
```
A plot of `params$n` random points.
```{r plot1}
plot(rnorm(params$n), rnorm(params$n))
```
```{r table1}
DT::datatable(iris, rownames = FALSE,
colnames = c("Sepal length", "Sepal width", "Petal length", "Petal width", "Species"),
extensions = 'Buttons',
filter = "none",
options = list(pageLength = 25, autowidth = TRUE,
dom = 'Blftip',
buttons = c('copy', 'csv', 'excel')))
```
闪亮的应用:
shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("slider", "Slider", 1, 100, 50),
actionButton("generate_report", "Generate report")
),
mainPanel(uiOutput("report"))
)),
server = function(input, output) {
observeEvent(input$slider, {
output$report <- renderUI(textOutput("holding_text"))
})
output$holding_text <- renderText("Please select inputs and click 'Generate report'")
observeEvent(input$generate_report, {
# Copy the report file to a temporary directory before processing it
my_temp <- tempdir()
tempReport <- file.path(my_temp, "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
# Set up parameters to pass to Rmd document
params <- list(n = input$slider)
# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment
rmarkdown::render(tempReport,
params = params,
envir = new.env(parent = globalenv())
)
output$report <- renderUI({
tagList(
downloadButton("download", "Download report"),
htmltools::HTML(includeHTML(file.path(my_temp, "report.html")))
)
})
output$download <- downloadHandler(
# For PDF output, change this to "report.pdf"
filename = "report.html",
content = function(file) {
file.copy(file.path(my_temp, "report.html"), file)
}
)
})
}
)
【问题讨论】:
标签: r shiny r-markdown dt