我想将Shiny应用程序的输出另存为PDF和Word文件。就像here一样。
在Shiny服务器上可以找到相同的代码,并且可以完美地生成报告,但是当我在本地运行该代码时,生成的输出是空白的HTML文件,而PDF则显示服务器错误。 我已经尝试过,创建一个空白
report.Rmd
fluidPage(
title = 'Download a PDF report',
sidebarLayout(
sidebarPanel(
helpText(),
selectInput('x', 'Build a regression model of mpg against:',
choices = names(mtcars)[-1]),
radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
inline = TRUE),
downloadButton('downloadReport')
),
mainPanel(
plotOutput('regPlot')
)
)
)
shinyServer(function(input, output) {
# list of data sets
datasetInput <- reactive({
switch(input$dataset,
"cars" = mtcars,
"longley" = longley,
"MLB" = mlb11,
"rock" = rock,
"pressure" = pressure,
"Your Data" = df())
})
# dependent variable
output$dv = renderUI({
selectInput('dv', h5('Dependent Variable'), choices = names(datasetInput()))
})
# independent variable
output$iv = renderUI({
selectInput('iv', h5('Independent Variable'), choices = names(datasetInput()))
})
# regression formula
regFormula <- reactive({
as.formula(paste(input$dv, '~', input$iv))
})
# bivariate model
model <- reactive({
lm(regFormula(), data = datasetInput())
})
# create graphics
# data view
output$view <- renderTable({
head(datasetInput(), n = input$obs)
})
# summary statistics
output$summary <- renderPrint({
summary(cbind(datasetInput()[input$dv], datasetInput()[input$iv]))
})
# histograms
output$distPlot_dv <- renderPlot({
x <- datasetInput()[,input$dv]
bins <- seq(min(x), max(x), length.out = input$bins_dv + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white', main = 'Dependent Variable', xlab = input$dv)
})
output$distPlot_iv <- renderPlot({
x <- datasetInput()[,input$iv]
bins <- seq(min(x), max(x), length.out = input$bins_iv + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white', main = 'Independent Variable', xlab = input$iv)
})
# scatter plot
output$scatter <- renderPlot({
plot(datasetInput()[,input$iv], datasetInput()[,input$dv],
xlab = input$iv, ylab = input$dv, main = "Scatter Plot of Independent and Dependent Variables", pch = 16,
col = "black", cex = 1)
abline(lm(datasetInput()[,input$dv]~datasetInput()[,input$iv]), col="grey", lwd = 2)
})
# correlation matrix
output$corr <- renderGvis({
d <- datasetInput()[,sapply(datasetInput(),is.integer)|sapply(datasetInput(),is.numeric)]
cor <- as.data.frame(round(cor(d), 2))
cor <- cbind(Variables = rownames(cor), cor)
gvisTable(cor)
})
# bivariate model
output$model <- renderPrint({
summary(model())
})
# residuals
output$residuals_hist <- renderPlot({
hist(model()$residuals, main = paste(input$dv, '~', input$iv), xlab = 'Residuals')
})
output$residuals_scatter <- renderPlot({
plot(model()$residuals ~ datasetInput()[,input$iv], xlab = input$iv, ylab = 'Residuals')
abline(h = 0, lty = 3)
})
output$residuals_qqline <- renderPlot({
qqnorm(model()$residuals)
qqline(model()$residuals)
})
# hotable
output$hotable1 <- renderHotable({
df <- data.frame(String = c('a', 'b', 'c', 'd', 'e','a', 'b', 'c', 'd', 'e'),
Numeric1 = numeric(10),
Numeric2 = numeric(10))
return(df)
}, readOnly = FALSE)
df <- reactive({
hot.to.df(input$hotable1) # this will convert your input into a data.frame
})
# download report
output$downloadReport <- downloadHandler(
filename = function() {
paste('my-report', sep = '.', switch(
input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
))
},
content = function(file) {
src <- normalizePath('report.Rmd')
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report.Rmd')
library(rmarkdown)
out <- render('report.Rmd', switch(
input$format,
PDF = pdf_document(), HTML = html_document(), Word = word_document()
))
file.rename(out, file)
})
})
答案 0 :(得分:0)
filename = "report.html"
您尝试将其更改为.pdf吗?
答案 1 :(得分:0)
我已经设法根据上面提供的代码来渲染文档(我也测试了单词输出)。我必须根据您的链接中提供的代码进行一些更改。
您需要确保/注意以下事项:
render()
包中的rmarkdown
函数必须采用原始的xx.Rmd
模板作为输入,需要将其存储在工作目录中。 render()
函数将如(1)中所述的原始输入传递给knitr
,该原始代码执行所有R
代码块并创建一个新的.Rmd
文档,该文档随后传递到pandoc
中,它会生成最终输出(pdf,html,word)pdflatex
,可以通过Miktex发行版(https://miktex.org/download)或TeX Live发行版(https://www.tug.org/texlive/acquire.html)来获得。 pandoc
方法,并且该方法必须采用rmarkdown::html_document(), rmarkdown::pdf_document(), rmarkdown::word_document()
的形式。 rmarkdown::render(report.Rmd, pdf_document())
或rmarkdown::render(report.Rmd, pdf_document())
以下是我作为示例创建的report.Rmd
模板,请将其存储在您的工作目录中:
---
title: "Test R Markdown"
author: "RickTastic!!"
date: "January 9, 2019"
output: html_document
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. You need a document like this to pass to the render function. You will then want to embed your output code like this `r sum(c(1, 2, 3))`
## Showing the slider number
```{r sliderNumber, include = FALSE}
sliderNumber <- function() return(as.numeric(input$slider))
```
The input selected from the slider is `r sliderNumber()`. Your function needs to be defined in your .Rmd template.
library(shiny)
library(rmarkdown)
ui <- fluidPage(sliderInput("slider", "Slider", 1, 100, 50), downloadButton("report", "Generate report"))
server <- function(input, output, session) {
sliderInput <- reactive(return(as.numeric(input$slider)))
output$report <- downloadHandler(
filename = function() return("myReport.html"),
content = function(file) {
src <- normalizePath("report.Rmd")
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, "report.Rmd", overwrite = TRUE)
out <- render("report.Rmd", html_document())
file.rename(out, file)
}
)
}
shiny::shinyApp(ui = ui, server = server)