0

I recently asked a question about issues with reactivity in Shiny, and I modified my code due to the comments I received, but I still cannot get it to work. As I mentioned before, I know that without the reactivity, the plot works, so it's definitely a reactivity issue.

In my UI I have a selectInput with choices A, B, C, D. (Corresponding to 4 data frames A, B, C, D).

In my server:

colors <- c("red", "blue", "green", "purple")

data <- reactive({
    if(input$selectInput == "A"){
        A
    } else if(input$selectInput == "B") {
        B
    } else if(input$selectInput == "C") {
        C
    } else if(input$selectInput == "D") {
        D
    }

   data <- na.omit(as.data.frame(data$cover_group))
   names(data) <- c("tmp")
 })


   group_factor <- reactive({
      group_factor <- as.factor(data()$tmp)
     group_factor <- fct_recode(group_factor, OTHER = "E", OTHER = "SPONGES", OTHER = "F", OTHER = "G", OTHER = "H", OTHER = "I", OTHER = "J")
            })



 bar <- reactive({
     ggplot(data = data()) +
            geom_bar(mapping = aes(x = group_factor(), y = ..count../sum(..count..)), fill = colors) + xlab("Cover Group") + ylab("Percent")
         })


 observe({
   output$coverTypeBarChart <- renderPlot(bar())
        })
John Smith
  • 393
  • 1
  • 6
  • 17
  • I forgot to mention: The error is: ggplot2 doesn't know how to deal with data of class character – John Smith Jun 16 '17 at 19:24
  • anything reactive returns values as type character. try using `as.type(reactive())` where type is your needed type and reactive is your reactive function name. – Phi Jun 16 '17 at 19:28
  • OK, I did ggplot(data = as.data.frame(data()) and got the error: "The $ operator is invalid for atomic vectors". – John Smith Jun 16 '17 at 19:33
  • try using different names. `data` and `data()` is going to cause you issues – Phi Jun 16 '17 at 19:39
  • You need to create some sample data, generate a minimal working example so others can better help you. Just posting server code and hope others can find the problem by gazing on them seldom work unless it's too obvious. – dracodoc Jun 16 '17 at 19:43
  • And basic debugging skills should help: place a `browser()` call after data is generated, run the app, inspect data to see what has go wrong. – dracodoc Jun 16 '17 at 19:45
  • Without looking too closely, you have to return a value from your reactives! Use return(data) and return(group_factor) etc. explicitely (or just put the make "group_factor" the last line of the reactive without anything else, same effect) – Colin D Jun 16 '17 at 19:45
  • And you need to learn reactive basics. Putting ggplot in a reactive expression, then put reactive expression in renderPlot call, using observe, all of these are not right in your case. Why so many shiny beginners are using `observe`, and I don't recall any reactive tutorial I read using it. Are you using some old tutorial or some old example code? – dracodoc Jun 16 '17 at 19:47
  • It was my understanding reactive is used to store values, and observe is used to perform some action. I forget where I read this, and I may well be completely wrong. – John Smith Jun 16 '17 at 19:53
  • an observer is used for its resulting side effects. a reactive is used to return a value. a reactiveValue is used to store a value – Phi Jun 16 '17 at 20:01

0 Answers0