1

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:

  1. Get input from radiobuttons asking for a date, and print the answer (to check if he reacts fine) -> works
  2. Reactive function reading the relevant csv-file (based on the date) and printing it's contents (to check) -> works
  3. 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
...
2
  • 1
    reactives are functions. Try with fake2() does it work? Commented Jan 15, 2018 at 10:34
  • Yep that's it. Another guy already answered that, but because I also needed to delete reactive() (which he didn't tell me, probably forgot to delete it in the code) it didn't work. Problem solved and thanks to you both! Commented Jan 15, 2018 at 10:40

1 Answer 1

2

I got the answer thanks to a developer named Florian, but unfortunately he deleted his comment.

He told me I needed to use x() instead of x when working with reactive content, in my case: ggplot(fake2()... instead of ggplot(fake2...

This didn't work at first, but got me on the right track!

On top of that, I also had to remove reactive({}) around the renderPlot function and then it worked.

So thank you for your help Florian!

New code:

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")
})
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Tingolfin, sorry for deleting my answer, I do not have much experience with Shiny apps in this markdown-style, so I assumed my answer was just plain wrong. Glad that you were able to resolve your own issue!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.