我正在尝试一个以.csv作为输入的Shinyapp,并计算列的mean
并显示输出。另外,我需要以pdf
格式下载结果作为报告。
问题是当我运行应用程序时它工作正常并在屏幕上显示结果但是当我尝试下载报告时会抛出错误说明
"Shinyoutput Objects not allowed."
我在SO中遇到的帖子很少但无法找到合适的解决方案。
以下是我的app.R和report.rmd文件
App.R
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", label = (" Choose Data "),multiple = F),
actionButton("button", "Calculate Mean",style = "background-color : skyblue",
icon = icon("stats", lib = "glyphicon"),width = 250 )
),
# Show a plot of the generated distribution
mainPanel(
verbatimTextOutput("avg", placeholder = TRUE),
downloadButton("reportpdf", label = 'Download Report',
style = "background-color : orange", width = 250)
)
)
)
server <- function(input, output) {
dataframe <- reactive( {
### Create a data frame reading data file to be used by other functions..
inFile <- input$file1
if (is.null(inFile)){
createAlert(session, "alert", "exampleAlert", title = "Oops!!!",
content = "Please select the input file.", append = FALSE)}
else{closeAlert(session, "exampleAlert")}
data <- read.csv(inFile$datapath, header = TRUE)
})
avg <- eventReactive(input$button, {mean(dataframe()$A)})
output$avg <- renderText({avg()})
output$reportpdf <- downloadHandler(
filename = function() {
paste('my-report', sep = '.', 'pdf')
},
content = function(file) {
src <- normalizePath('report.Rmd')
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report.Rmd', overwrite = TRUE)
library(rmarkdown)
out <- render('report.Rmd', pdf_document())
file.rename(out, file)
params <- list(mean = output$avg)
}
)
}
shinyApp(ui = ui, server = server)
Report.rmd
---
title: " Analysis Report"
output: pdf_document
params: mean
---
##Analysis Result
Percentage of data points spread: `params$mean`
答案 0 :(得分:1)
解决这个问题的方法是调用反应函数而不是变量名:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", label = (" Choose Data "),multiple = F),
actionButton("button", "Calculate Mean",style = "background-color : skyblue",
icon = icon("stats", lib = "glyphicon"),width = 250 )
),
# Show a plot of the generated distribution
mainPanel(
verbatimTextOutput("avg", placeholder = TRUE),
downloadButton("reportpdf", label = 'Download Report',
style = "background-color : orange", width = 250)
)
)
)
server <- function(input, output, session) {
dataframe <- reactive( {
### Create a data frame reading data file to be used by other functions..
inFile <- input$file1
if (is.null(inFile)){
createAlert(session, "alert", "exampleAlert", title = "Oops!!!",
content = "Please select the input file.", append = FALSE)}
else{closeAlert(session, "exampleAlert")}
data <- read.csv(inFile$datapath, header = TRUE)
})
avg <- eventReactive(input$button, {mean(dataframe()$A)})
output$avg <- renderText({avg()})
output$reportpdf <- downloadHandler(
filename = function() {
paste('my-report', sep = '.', 'pdf')
},
content = function(file) {
src <- normalizePath('report.Rmd')
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report.Rmd', overwrite = TRUE)
library(rmarkdown)
out <- render('report.Rmd', pdf_document())
file.rename(out, file)
params <- list(mean = avg())
}
)
}
shinyApp(ui = ui, server = server)
Markdown更新:
---
title: " Analysis Report"
output: pdf_document
params: mean
---
##Analysis Result
Percentage of data points spread:
```{r}
params$mean
```
答案 1 :(得分:1)
相反,您可以使用内嵌代码。
App.R
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", label = (" Choose Data "),multiple = F),
actionButton("button", "Calculate Mean",style = "background-color : skyblue",
icon = icon("stats", lib = "glyphicon"),width = 250 )
),
# Show a plot of the generated distribution
mainPanel(
verbatimTextOutput("avg", placeholder = TRUE),
downloadButton("reportpdf", label = 'Download Report',
style = "background-color : orange", width = 250)
)
)
)
server <- function(input, output, session) {
dataframe <- reactive( {
### Create a data frame reading data file to be used by other functions..
inFile <- input$file1
if (is.null(inFile)){
createAlert(session, "alert", "exampleAlert", title = "Oops!!!",
content = "Please select the input file.", append = FALSE)}
else{closeAlert(session, "exampleAlert")}
data <- read.csv(inFile$datapath, header = TRUE)
})
avg <- eventReactive(input$button, {mean(dataframe()$A)})
output$avg <- renderText({avg()})
output$reportpdf <- downloadHandler(
filename = function() {
paste('my-report', sep = '.', 'pdf')
},
content = function(file) {
src <- normalizePath('report.Rmd')
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report.Rmd', overwrite = TRUE)
library(rmarkdown)
out <- render('report.Rmd', pdf_document())
file.rename(out, file)
}
)
}
shinyApp(ui = ui, server = server)
Report.Rmd
---
title: " Analysis Report"
output: pdf_document
params: mean
---
##Analysis Result
Percentage of data points spread: `r mean(dataframe()$A) `
希望this document有帮助!!!