Shiny 和 RMarkdown:渲染多个块

Shiny and RMarkdown: Rendering multiple chunks

我想创建一个允许上传自己的数据集的 Shiny-RMarkdown 应用程序。数据集有时不仅影响一个可以由 renderPlot() (或类似的)重新渲染的块,而且有时影响两个或更多块。请参阅以下示例:

---
title: Render multiple chunks
output: html_document
runtime: shiny
---

```{r echo=FALSE}
library(shiny)
fileInput('file1', 'Choose your own CSV File instead of provided 
data',accept=c('text/csv', 'text/comma-separated-values,text/plain', 
'.csv'))

go1<-reactive({
  dpath <- "CurrentBiologyData.txt"
  if(!is.null(input$file1)){
     dpath <- input$file1$datapath
  }
  CB.dat <- read.table(dpath, header = TRUE)  #choose 'CurrentBiologyData.txt'
  plot(CB.dat)
})
```

```{r echo=FALSE}
renderPlot({
      go1()
})
```

```{r}
   renderPlot({
   print(CB.dat)
   }) 
```

所以如果我上传新数据集,我有三个块会受到影响。问题是第三个块没有看到第一个块中填充的CB.dat:

Error: Object 'CB.dat' not found

我有什么想法可以让它发挥作用吗?

将我在评论中的意思应用于您的示例

Put the file reading in a reactive then use it wherever you need it

---
title: Render multiple chunks
output: html_document
runtime: shiny
---

```{r echo=FALSE}
library(shiny)
fileInput('file1', 'Choose your own CSV File instead of provided 
data',accept=c('text/csv', 'text/comma-separated-values,text/plain', 
'.csv'))

CB.dat<-reactive({
  dpath <- "CurrentBiologyData.txt"
  if(!is.null(input$file1)){
     dpath <- input$file1$datapath
  }
  read.table(dpath, header = TRUE)  #choose 'CurrentBiologyData.txt'
})
```

```{r echo=FALSE}
renderPlot(plot(CB.dat())
```

```{r}
renderTable(CB.dat())
```