如何将 "reactive" 数据作为参数传递给 R Markdown?
How to pass "reactive" data into R Markdown as a parameter?
我希望能够将反应式 data.table
传递到我的 Rmarkdown 文档,该文档将对 table 进行排序并打印顶部(例如 5)排序的条目。
我在 setorderv
将 运行 保存到 "Error : x must be a data.frame or data.table"。我怀疑反应性干扰了 is.data.table
检查。
我如何简单地将数据的副本传递给 Rmd 文档?并移除反应性包装器?
这是一个从反应性和 downloadHandler 库存示例中拼凑而成的简化示例。我查看了建议使用 observe 的 this on SO,但我不确定如何实现它。谢谢,我很感激这方面的帮助。
RMARKDOWN - SOtestreport.Rmd
---
title: "Top in Segment Report"
output: html_document
params:
n: 5
dt: NA
top.field: NA
---
```{r setup, echo = FALSE}
library(data.table)
library(knitr) # for kable
```
Data report as at: `r format(Sys.time(), "%a %b %d %Y %X")`
```{r, echo = FALSE}
table.str <- paste0("Showing Top ", params$n ," tables for these fields: ",
params$top.field)
# is this the best way of having dynamic labels?
```
`r table.str`
```{r, echo = FALSE}
# tried this
# test.dt <- copy(dt)
# normally a function GenerateTopTable with limit sort field etc.
setorderv(dt, params$top.field, -1) # -1 is descending. THIS IS WHERE IT CRASHES
out <- dt[1:params$n,]
```
```{r toptable, echo = FALSE}
kable(out, digits = 3)
```
app.R
# SO test report
# mini example based on https://shiny.rstudio.com/gallery/reactivity.html
library(shiny)
library(datasets)
server <- function(input, output) {
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})
output$caption <- renderText({
input$caption
})
output$summary <- renderPrint({
dataset <- datasetInput()
summary(dataset)
})
output$view <- renderTable({
head(datasetInput(), n = input$obs)
})
# PROBLEM BIT WHERE I AM ADDING ON GALLERY EXAMPLE
# handler from
output$topreport <- downloadHandler(
filename = "topreport.html",
content = function(file) {
# Copy the report file to a temporary directory before processing it, in
# case we don't have write permissions to the current working dir (which
# can happen when deployed).
tempReport <- file.path(tempdir(), "SOtestreport.Rmd")
file.copy("SOtestreport.Rmd", tempReport, overwrite = TRUE)
# Set up parameters to pass to Rmd document
# TRIED THIS TOO
# my.dt <- as.data.table(datasetInput())
my.dt <- datasetInput()
my.params <- list(n = input$obs,
dt = my.dt,
top.field = names(my.dt)[1])
rmarkdown::render(tempReport, output_file = file,
params = my.params,
envir = new.env(parent = globalenv())
)
}
)
我修复了这个例子 - Rmd 中的 dt
没有以 params$
!
开头
setorderv(params$dt, params$top.field, -1) # -1 is descending
out <- params$dt[1:params$n,]
我仍在寻找上述方法是否正确的答案(将 Shiny 中的参数传递给 RMarkdown 应用程序)。
我还想向其他人指出这些答案也很有用:
@NicE 使用 ReactiveValues
What's the difference between Reactive Value and Reactive Expression?
我还利用复制数据集来打破反应性,即
my.dt <- copy(params$dt)
#do this before passing into render params list
RStudio 中的另一个提示是不要使用内部查看器窗格,而是在浏览器外部 运行。这修复了临时文件的保存位置问题。您可以在下图中的 运行 应用程序下选择始终 运行 外部:
我希望能够将反应式 data.table
传递到我的 Rmarkdown 文档,该文档将对 table 进行排序并打印顶部(例如 5)排序的条目。
我在 setorderv
将 运行 保存到 "Error : x must be a data.frame or data.table"。我怀疑反应性干扰了 is.data.table
检查。
我如何简单地将数据的副本传递给 Rmd 文档?并移除反应性包装器?
这是一个从反应性和 downloadHandler 库存示例中拼凑而成的简化示例。我查看了建议使用 observe 的 this on SO,但我不确定如何实现它。谢谢,我很感激这方面的帮助。
RMARKDOWN - SOtestreport.Rmd
---
title: "Top in Segment Report"
output: html_document
params:
n: 5
dt: NA
top.field: NA
---
```{r setup, echo = FALSE}
library(data.table)
library(knitr) # for kable
```
Data report as at: `r format(Sys.time(), "%a %b %d %Y %X")`
```{r, echo = FALSE}
table.str <- paste0("Showing Top ", params$n ," tables for these fields: ",
params$top.field)
# is this the best way of having dynamic labels?
```
`r table.str`
```{r, echo = FALSE}
# tried this
# test.dt <- copy(dt)
# normally a function GenerateTopTable with limit sort field etc.
setorderv(dt, params$top.field, -1) # -1 is descending. THIS IS WHERE IT CRASHES
out <- dt[1:params$n,]
```
```{r toptable, echo = FALSE}
kable(out, digits = 3)
```
app.R
# SO test report
# mini example based on https://shiny.rstudio.com/gallery/reactivity.html
library(shiny)
library(datasets)
server <- function(input, output) {
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})
output$caption <- renderText({
input$caption
})
output$summary <- renderPrint({
dataset <- datasetInput()
summary(dataset)
})
output$view <- renderTable({
head(datasetInput(), n = input$obs)
})
# PROBLEM BIT WHERE I AM ADDING ON GALLERY EXAMPLE
# handler from
output$topreport <- downloadHandler(
filename = "topreport.html",
content = function(file) {
# Copy the report file to a temporary directory before processing it, in
# case we don't have write permissions to the current working dir (which
# can happen when deployed).
tempReport <- file.path(tempdir(), "SOtestreport.Rmd")
file.copy("SOtestreport.Rmd", tempReport, overwrite = TRUE)
# Set up parameters to pass to Rmd document
# TRIED THIS TOO
# my.dt <- as.data.table(datasetInput())
my.dt <- datasetInput()
my.params <- list(n = input$obs,
dt = my.dt,
top.field = names(my.dt)[1])
rmarkdown::render(tempReport, output_file = file,
params = my.params,
envir = new.env(parent = globalenv())
)
}
)
我修复了这个例子 - Rmd 中的 dt
没有以 params$
!
setorderv(params$dt, params$top.field, -1) # -1 is descending
out <- params$dt[1:params$n,]
我仍在寻找上述方法是否正确的答案(将 Shiny 中的参数传递给 RMarkdown 应用程序)。
我还想向其他人指出这些答案也很有用:
@NicE 使用 ReactiveValues
What's the difference between Reactive Value and Reactive Expression?
我还利用复制数据集来打破反应性,即
my.dt <- copy(params$dt)
#do this before passing into render params list
RStudio 中的另一个提示是不要使用内部查看器窗格,而是在浏览器外部 运行。这修复了临时文件的保存位置问题。您可以在下图中的 运行 应用程序下选择始终 运行 外部: