1
votes

I have a dataset that looks as the following one (but the actual dataset is much larger and contains some additional columns):

"month","year","id","hour","count"
1,2015,"place001",13,10.2319100637371
6,2015,"place001",14,42.2066290129154
10,2015,"place002",18,34.5326964651126
3,2015,"place002",8,23.0217976434084
3,2014,"place002",1,19.184831369507
3,2014,"place003",15,47.322584044784

I'm trying to build a Shiny app to explore this data. This is what I got so far, but it breaks when it tries to process the quarterly reactive function. If I use the "redundant" approach that is commented out, the app renders correctly, however I want to avoid it.

Later I will also need to use the output of totals and reshape it with the melt function, so I want to understand how I can use this reactive function as input for another reactive function.

Any help is greatly appreciated.

ui.R

library(shiny)
library(ggvis)

renderInputs <- function() {
  wellPanel(
    ## Take hour information from user for filtering.
    checkboxGroupInput("hour", "Hours",
                       c("1" = 1,
                         "8" = 8,
                         "13" = 13,
                         "14" = 14,
                         "15" = 15,
                         "18" = 18),
                       selected  = c(1, 8, 13, 15),
                       inline = T)
  )
}

shinyUI(fluidPage(
  titlePanel("Analysis"),

  fluidRow(
    ## Inputs
    column(4, renderInputs()),
    ## Plot data.
    column(8,h2("Monthly Visitors"),
           ggvisOutput("plotM")
    )),
    ## Plot quarterly data.
  fluidRow(
    column(7, h2('Quarterly Summary'),
    ggvisOutput("plotQ"))
    )
))

server.R

library(shiny)
library(dplyr)
library(zoo)

data <- read.csv("file.csv")
data$date <- as.yearmon(paste0(data$year, "-", data$month))

shinyServer(function(input, output) {

    totals <- reactive({
      data %>%
        filter(hour %in% input$hour) %>%
        group_by(date) %>%
        summarize(visitors = sum(count))
    })

    ## Quarterly, with "reactive" approach.
    quarterly <- reactive({
      totals() %>%
        mutate(quarter = as.yearqtr(date)) %>%
        group_by(quarter) %>%
        summarize(visitors = sum(count))
    })


    ## Quarterly data and percentage changes (redudant approach).
#    quarterly <- reactive({
#    data %>%
#      filter(hour %in% input$hour) %>%
#      mutate(quarter = as.yearqtr(date)) %>%
#      group_by(quarter) %>%
#      summarize(visitors = sum(count))
#    })

    ## Create estimated quarterly chart.
    visM <- reactive({
      totals() %>%
        ggvis(~as.factor(date), ~visitors) %>%
        layer_points()
    })  

    ## Create estimated quarterly chart.
    visQ <- reactive({
      quarterly() %>%
        ggvis(~as.factor(quarter), ~visitors) %>%
        layer_bars(fill := "green")
    })


    ## Send plots to UI.
    visM %>% bind_shiny("plotM")

    visQ %>% bind_shiny("plotQ")
})

Thanks.

1
Your totals dataframe does not have a count column anymore, so your non-redundant approach fails at summarize(visitors = sum(count)), try doing summarize(visitors = sum(visitors))in the quaterly reactive block.NicE
Shiny-wise, you're doing the right thing. totals() should adress the right data set. Could you supply the error code? Maybe totals() is not the type you expect it to be. And /or the error is within dplyr.K. Rohde

1 Answers

0
votes

Thanks @NicE, I totally missed that! After fixing this dumb mistake it is working correctly. Thanks to K. Rohde too.

## Quarterly, with "reactive" approach.
quarterly <- reactive({
  totals() %>%
    mutate(quarter = as.yearqtr(date)) %>%
    group_by(quarter) %>%
    summarize(visitors = sum(visitors))
})