将输出对象从Server.R传递到.rmd文件
我正在尝试一个Shinyapp,它以.csv作为输入,计算列的将输出对象从Server.R传递到.rmd文件,r,shiny,R,Shiny,我正在尝试一个Shinyapp,它以.csv作为输入,计算列的平均值,并显示输出。另外,我需要下载pdf格式的结果作为报告 问题是,当我运行应用程序时,它工作正常,并在屏幕上显示结果,但当我尝试下载报告时,将抛出错误,说明 不允许Shinyoutput对象。 我遇到的帖子很少,因此无法找到合适的解决方案 下面是我的app.R和report.rmd文件 App.R library(shiny) ui <- fluidPage( sidebarLayout( sidebarPanel(
平均值
,并显示输出。另外,我需要下载pdf
格式的结果作为报告
问题是,当我运行应用程序时,它工作正常,并在屏幕上显示结果,但当我尝试下载报告时,将抛出错误,说明不允许Shinyoutput对象。
我遇到的帖子很少,因此无法找到合适的解决方案
下面是我的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)
解决此问题的方法是调用被动函数而不是变量名:
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)
相反,您可以使用内联代码 应用程序R
希望有帮助 谢谢!!现在我没有得到错误,但是
params$mean
被打印为字符而不是值。我是否必须在Report.rmd
文件中进行任何更改?再次感谢,它没有打印值,而是打印为##NULL
,这可能是因为您的平均值计算不正确。如果数据集的平均值是计算出来的,请检查这一部分好吗?屏幕上显示的是平均值,这意味着数据集或计算部分没有问题,我无法确定打印##NULL
的原因。实际上代码没有变化,我已经从您的答案中复制了相同的代码。最后,非常感谢,它按预期工作。该文件是初学者的综合备忘单。非常感谢。@SBS快乐学习:)
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)
---
title: " Analysis Report"
output: pdf_document
params: mean
---
##Analysis Result
Percentage of data points spread:
```{r}
params$mean
```
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)
---
title: " Analysis Report"
output: pdf_document
params: mean
---
##Analysis Result
Percentage of data points spread: `r mean(dataframe()$A) `