r - flexdashboard isolate table for child Rmd

r - flexdashboard isolate table for child 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")
```