I'm making a shiny app in an Rmarkdown file using flexdashboard and I'm having trouble making a ggplot with data from a reactive function.
These are the steps/elements:
- Get input from radiobuttons asking for a date, and print the answer (to check if he reacts fine) -> works
- Reactive function reading the relevant csv-file (based on the date) and printing it's contents (to check) -> works
- ggplot made with the contents of the csv-file -> does not work
When I use renderPlot() to make a ggplot with a non-reactive dataset (I loaded one), I get the correct result, so the ggplot-part is fine.
It has something to do with the reactive en renderPlot combinations, but I can't seem to figure it out. Although it's an "easy" concept, I keep having problems grasping the workflow, despite watching several movies and reading several guides.
This is my current code:
---
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
```
I also tried this in the plot-part:
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")
})
My data look like this:
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()does it work?