1

I'm trying to make a reactive data subset to avoid doing the subsetting at each stage of the analysis. However I get the error object of type 'closure' is not subsettable. I have seen previous answers and I avoid using base-R functions.

library(shiny)

shinyServer(function(input, output) {

    data_all <- read.csv("data/usage_data.csv")

    data_user <- reactive({
    subset(data_all, data_all$consumername %in% input$user)
    })

    output$distPlot <- renderPlot({    
        #data_user <- subset(data_all, data_all$consumername %in% input$user)
        data_user$date <- as.Date(data_user$date)

        stats$mean <- mean(data_user$usage_TB)
        stats$sd <- sd(data_user$usage_TB)

        pp <- ggplot(data_user, aes(date,usage_TB)) + geom_line() + xlim(input$dates) 
        pp + geom_hline(yintercept = stats$mean, color = "red")

    })

    output$mean <- renderPrint({
        #data_user <- subset(data_all, data_all$consumername %in% input$user)
        mean(data_user$usage_TB)
    })
    output$sd <- renderPrint({
        #data_user <- subset(data_all, data_all$consumername %in% input$user)
        sd(data_user$usage_TB)
    })
    output$p75 <- renderPrint({
        #data_user <- subset(data_all, data_all$consumername %in% input$user)
        quantile(data_user$usage_TB,0.75)
    })
})

If i make the subset t each step I have no problems but I think making the subset reactive should be better.

Edana Merchan
  • 25
  • 2
  • 8

1 Answers1

1

Found question was re-asked and answered at https://groups.google.com/forum/#!msg/shiny-discuss/p2eElm-XaqQ/y3vB4l3tt3EJ

Basically, they suggested

data_user <- reactive({ 
    df <- subset(data_all, data_all$consumername %in% input$user)
    df$date <- as.Date(data_user$date)
    df
})

Not enough rep to add a comment, and I thought this would be useful for others - it was for me.

anotherfred
  • 1,330
  • 19
  • 25