【问题标题】:Rshiny: Make reactive plot with data from reactive functionR Shiny:使用来自反应函数的数据制作反应图
【发布时间】:2018-06-23 22:55:08
【问题描述】:

我正在使用 flexdashboard 在 Rmarkdown 文件中制作闪亮的应用程序,但在使用来自反应函数的数据制作 ggplot 时遇到问题。

这些是步骤/元素:

  1. 从要求日期的单选按钮获取输入,并打印答案 (检查他是否反应良好)-> 工作
  2. 读取相关 csv 文件的反应函数(基于日期) 并打印它的内容(检查)-> 作品
  3. 用 csv 文件的内容制作的 ggplot -> 不起作用

当我使用 renderPlot() 用非反应性数据集(我加载了一个)制作 ggplot 时,我得到了正确的结果,所以 ggplot-part 很好。

这与反应式 en renderPlot 组合有关,但我似乎无法弄清楚。尽管这是一个“简单”的概念,但尽管看了几部电影并阅读了几本指南,但我仍然无法掌握工作流程。

这是我当前的代码:

---
title: "Tests plot" 
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(ggplot2)
```

Input {.sidebar}
======================================
```{r}
radioButtons("countdate",h3("Datum"), c("01-12-2017"="T1","06-12-2017"="T2","24-12-2017"="T3"))
```

Data
======================================
Column
-----------------------------------------------------------------------
### Date
```{r}
reactive({ #OK
  input$countdate
})
```

Column
-----------------------------------------------------------------------
### Data
```{r}
fake2 <- reactive({read.csv2(paste(input$countdate, ".csv", sep = ""))})
fake2 #OK
```

Column
-----------------------------------------------------------------------
### Plot
```{r}
ggplot1 <- reactive({
  renderPlot({ggplot(fake2, aes(Rij, Plant)) +
      xlim(0,40) +
      ylim(0,50) +
      coord_equal() +
      geom_raster(aes(fill=Wtot)) +
      scale_fill_gradient(low="yellow", high="red")
    })
})

ggplot1
```

我在剧情部分也试过这个:

renderPlot({
 ggplot(fake2, aes(Rij, Plant)) +
  xlim(0,40) +
  ylim(0,50) +
  coord_equal() +
  geom_raster(aes(fill=Wtot)) +
  scale_fill_gradient(low="yellow", high="red")
})

我的数据如下所示:

Vplaat;Rij;Plant;Mtot;Wtot
A;4;10;2;20
B;4;46;5;35
C;9;5;1;14
D;9;30;0;42
E;11;17;8;85
...

【问题讨论】:

  • 反应是函数。试试fake2() 有用吗?
  • 是的,就是这样。另一个人已经回答了这个问题,但是因为我还需要删除 reactive() (他没有告诉我,可能忘记在代码中删除它)它不起作用。问题已解决,谢谢你们!

标签: r ggplot2 shiny


【解决方案1】:

感谢一位名叫 Florian 的开发者,我得到了答案,但不幸的是,他删除了他的评论。

他告诉我在处理响应式内容时我需要使用 x() 而不是 x,在我的例子中:ggplot(fake2()... 而不是 ggplot(fake2...

起初这并不奏效,但让我走上了正轨!

除此之外,我还必须删除 renderPlot 函数周围的reactive({}),然后它才起作用。

非常感谢您对弗洛里安的帮助!

新代码:

renderPlot({
 ggplot(fake2(), aes(Rij, Plant)) +
  xlim(0,40) + #rijen
  ylim(0,50) + #planten
  coord_equal() +
  geom_raster(aes(fill=Wtot)) +
  scale_fill_gradient(low="yellow", high="red")
})

【讨论】:

  • 嗨 Tingolfin,很抱歉删除了我的答案,我对这种降价风格的 Shiny 应用程序没有太多经验,所以我认为我的答案完全是错误的。很高兴您能够解决自己的问题!
猜你喜欢
  • 1970-01-01
  • 2019-12-17
  • 2014-09-06
  • 2016-11-30
  • 1970-01-01
  • 2017-11-17
  • 2020-11-20
  • 2015-05-10
  • 2019-05-21
相关资源
最近更新 更多