r语言 - flexdashboard为子Rmd隔离表



我正试图将我一直使用的Rmd合并到flexdashboard中。我很好奇是否有可能隔离上传的文件并按原样使用,而不是编写一堆响应式函数。如果这是我的模板,是否有可能获得一个名为df的静态对象,以便子文档可以继续运行?

---
title: "help"
runtime: shiny
output: 
flexdashboard::flex_dashboard:
orientation: columns
---
```{r}
fileInput("data", "select data")
df <- isolate(input$data)
```
```{r, child="some_code.Rmd"}
```

我真正的例子是完全不同的但是我们假设some_code.Rmd是这样的:

---
title: "some code"
output: html_document
---
```{r packages, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE)
library(tidyverse)
```
The data looks like this:
```{r}
as_tibble(df)
```
The numeric data can be summarized with the following means
```{r}
df |> 
summarise(across(where(is.numeric), mean)) |> 
gather()
```

最后成功了:

knitr::knit()+markdown::markdownToHTML()+HTML()—>renderUI()

---
title: "help"
runtime: shiny
output: 
flexdashboard::flex_dashboard:
orientation: rows
---
Sidebar {.sidebar}
==============================================
```{r file-input}
fileInput("data", "select data")
```
Row
==============================================
```{r knit-child}
observeEvent(input$data, {
df <- isolate(read.csv(input$data$datapath))
new_env <- list2env(list(df = df))

output$res <- renderUI({
knitr::knit("some_code.Rmd", quiet = TRUE, envir = new_env) |> 
markdown::markdownToHTML() |> 
HTML()
})
})
uiOutput("res")
```

最新更新