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.
totals
dataframe does not have acount
column anymore, so your non-redundant approach fails atsummarize(visitors = sum(count))
, try doingsummarize(visitors = sum(visitors))
in thequaterly
reactive block. – NicEtotals()
should adress the right data set. Could you supply the error code? Maybetotals()
is not the type you expect it to be. And /or the error is withindplyr
. – K. Rohde