【问题标题】:Shiny with flexdashboard, dataframe as variableflexdashboard 闪亮,数据框作为变量
【发布时间】:2016-11-22 05:03:37
【问题描述】:

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

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="")))
```

如您所见,我的 csv 加载了 2 次:

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.)

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

【问题讨论】:

  • 试试my_df &lt;- reactive({fread(paste("/Users/woshitom/Desktop/shiny/",input$df,".csv",sep=""))})
  • 谢谢!工作正常,请提交您的答案,我会验证它

标签: r variables shiny fread flexdashboard


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-08-08
    • 2021-03-02
    • 2017-05-28
    • 2019-12-31
    • 2020-04-14
    • 2018-09-26
    • 2018-06-21
    • 2017-03-29
    • 2021-06-21
    相关资源
    最近更新 更多