R Shiny reactive subset data - ERROR object of type 'closure' is not subsettable

9,645 views
Skip to first unread message

Edana Merchan

unread,
Apr 15, 2015, 2:42:25 PM4/15/15
to shiny-...@googlegroups.com

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.

Martin Lavoie

unread,
Apr 16, 2015, 4:49:04 AM4/16/15
to shiny-...@googlegroups.com
Hi Edana,

if I am not mistaken, you will have to use data_user() instead of data_user

martin

Edana Merchan

unread,
Apr 16, 2015, 9:32:10 AM4/16/15
to shiny-...@googlegroups.com
It didn't work, if I assign the reactive statement as: data_user() <- reactive({...})
I get the error:

Error in data_user() <- reactive({ : 
invalid (NULL) left side of assignment

And I was thinking output of reactive is a dataframe so I don't know about the parenthesis. 

Thanks anyway.

Edana

Michael Cawthon

unread,
Apr 16, 2015, 10:26:04 AM4/16/15
to shiny-...@googlegroups.com
use data_user() when you *refer* to the reactive object; use data_user when you initially make the assignment

ie,

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

    })  ##This is good
...

 data_user()$date <- as.Date(data_user()$date) ##Note the syntax
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/302ad8b1-9589-48e3-99f3-e0bccee0624e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 

Michael Cawthon
Chief Investment Officer
Green Street Energy LLC
mcaw...@greenstenergy.com
p: 479-442-1407

Joe Cheng

unread,
Apr 16, 2015, 12:08:02 PM4/16/15
to Michael Cawthon, shiny-...@googlegroups.com
Martin and Michael are right that you should use data_user() when retrieving the value. I'd like to further point out that attempting to mutate the reactive expression doesn't make a lot of sense, as in the left-hand side of this:

data_user()$date <- as.Date(data_user()$date)

Instead you should either make a temporary variable in the renderPlot call, or (much) better yet, do the as.Date conversion right within the data_user reactive expression.

Edana Merchan

unread,
Apr 16, 2015, 12:17:49 PM4/16/15
to shiny-...@googlegroups.com
yes, I understand now the difference and it works fine by including a df<-data_user() statement within the plot render. 

I agree that the change to the reactive expression should be reactive as well, I tried:

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

But it doesn't not like it, it comes back to the same object of type 'closure' is not subsettable error.

Edana


On Wednesday, April 15, 2015 at 2:42:25 PM UTC-4, Edana Merchan wrote:

Joe Cheng

unread,
Apr 16, 2015, 12:20:35 PM4/16/15
to Edana Merchan, shiny-...@googlegroups.com
data_user <- reactive({ 
    df <- subset(data_all, data_all$consumername %in% input$user)
    df$date <- as.Date(data_user$date)
    df
})

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.

Joe Cheng

unread,
Apr 16, 2015, 12:21:46 PM4/16/15
to Edana Merchan, shiny-...@googlegroups.com
FYI, Hadley Wickham's recently announced readr package has a fast replacement for read.csv that automatically detects dates.

Jia Shen

unread,
Nov 18, 2015, 9:43:40 PM11/18/15
to Shiny - Web Framework for R, edanam...@gmail.com
can you let me know what's the purpose of rewriting df again in the reactive function?

I myself had come across another problem. Below is my code:
shinyServer(function(input,output){
  
  data=reactive({
    dat1=filter(dat,gender==input$gender & age==input$age & name==input$cty)
    avg=round(mean(dat1$steps),0)
  })
 
   
   
  tablevalues=reactive({
    data.frame(
      Metrics = c("City", 
                  "Age",
                  "Gender",
                  "Average Steps in your group"
                  ),
      Value = c(input$cty, 
                             input$age,
                             input$gender,
                             data()), 
      stringsAsFactors=FALSE)
  })
  # Show the values using an HTML table
  output$values <- renderTable({
    tablevalues()
  })
  
})

And i'm trying to get data() rendered in the output but it's always NaN in the table for that row. Can you offer some advice? Thanks!

Joe Cheng

unread,
Dec 24, 2015, 1:14:23 AM12/24/15
to Jia Shen, Shiny - Web Framework for R, edanam...@gmail.com
If this is returning NA:

  data=reactive({
    dat1=filter(dat,gender==input$gender & age==input$age & name==input$cty)
    avg=round(mean(dat1$steps),0)
  })

then I'd assume that dat1$steps contains NA values? You have to decide whether it makes more sense to fail in that case or whether to ignore the NA values, and use na.fail() or na.omit() respectively.

Joe Cheng

unread,
Dec 24, 2015, 1:16:13 AM12/24/15
to Jia Shen, Shiny - Web Framework for R, edanam...@gmail.com
Oh whoops, I thought this was a recent message...
Reply all
Reply to author
Forward
0 new messages