【发布时间】:2021-05-20 01:19:01
【问题描述】:
我制作了一个 R 脚本,允许使用某种类型的数据集获取 R Markdown 报告。现在我希望其他人能够使用此脚本来获取包含他们的数据的自动报告,但无需使用此脚本(尤其是对于不掌握 R 的人)。
我尝试通过 Shiny 希望创建一个界面来加载数据集并自动生成我的脚本,但我无法在 Shiny 和我的 Rmd 之间建立链接。
如何告诉我的 Rmd 要处理的数据集不是我的 Rmd 脚本要在目录中查找的数据集,而是在 Shiny 界面上加载的数据集?
谢谢
这是带有我的 Rmd 的 Shiny 脚本,名为“traitemant_bis.Rmd”:
library(shiny)
library(rmarkdown)
ui <- fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput(
inputId = "file1", label = "Choose CSV File",
multiple = FALSE,
accept = c("text/csv", "text/comma-separated-values,text/plain", ".csv")
),
radioButtons("format", "Document format", c("PDF", "HTML", "Word"), inline = TRUE)
),
mainPanel(
tableOutput("contents"),
downloadButton("downloadReport")
)
)
)
server <- function(input, output) {
dataset <- reactive({
req(input$file1)
read.csv(file = input$file1$datapath,
na.strings = ".",
sep = ";",
header = TRUE,
nrows=10)
})
output$contents <- renderTable({
req(dataset())
head(dataset())
})
output$downloadReport <- downloadHandler(
filename = function() {
paste("my-report", sep = ".", switch(
input$format, PDF = "pdf", HTML = "html", Word = "docx"
))
},
content = function(file) {
src <- normalizePath("traitemant_bis.Rmd")
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, "traitemant_bis.Rmd", overwrite = TRUE)
out <- render("traitemant_bis.Rmd", switch(
input$format,
PDF = pdf_document(), HTML = html_document(), Word = word_document()
))
file.rename(out, file)
}
)
}
shinyApp(ui, server) ```
【问题讨论】:
标签: r shiny r-markdown report