闪亮的 flexdashboard,数据框作为变量

Shiny with flexdashboard, dataframe as variable

在 Flexdashboard 中使用 shiny 我想绘制和显示一个数据框,其中这个数据框是我侧边栏中输入的变量:

Inputs {.sidebar}
-----------------------------------------------------------------------

```{r}
selectInput("df", label = h3("Select df"),  choices = list("january" = "df1", "february" = "df2"))
```

然后我在标签集中绘制和显示我的数据框:

Row {.tabset}
-----------------------------------------------------------------------

### Plot
```{r}
renderPlot({
plot(fread(paste("/Users/woshitom/Desktop/shiny/",input$df,".csv",sep="")),type="o", col="blue")
})
```

### Data
```{r}
renderTable(fread(paste("/Users/woshitom/Desktop/shiny/",input$df,".csv",sep="")))
```

如您所见,我正在加载 2 倍的 csv:

fread(paste("/Users/woshitom/Desktop/shiny/",input$df,".csv",sep=""))

相反,我想将它存储在一个变量中:

my_df <- fread(paste("/Users/woshitom/Desktop/shiny/",input$df,".csv",sep=""))

但是当我这样做时,出现以下错误:

Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

知道我应该如何存储这个数据框吗?

闪亮的输入应该在渲染函数、观察者或反应器中使用。这就是您收到错误的原因。在你的情况下,因为你想将结果存储在一个变量中,所以要走的路是用 reactive() 创建一个反应变量。这是解决方案:

my_df <- reactive({fread(paste("/Users/woshitom/Desktop/shiny/",input‌​$df,".csv",sep=""))}‌​)