Filter Data Frame by Date entered with dateInput

1,284 views
Skip to first unread message

Thomas Hopper

unread,
Sep 5, 2014, 10:02:14 AM9/5/14
to shiny-...@googlegroups.com

I'm somewhat new to Shiny, and having created a few very simple "hello, world" apps I'm now trying to create my first "real" app. I'm having some trouble extracting or using dates from the dateInput control, and hope someone can help me.

I have a data set with about 150000 rows that I load, and I want to plot the data using ggvis. The user needs to be able to

1) control the transparency of data points;
2) mouse over to read specific data point y-values;
3) see the complete data set or zoom in on particular date ranges (down to less than 100 rows).

I thought that a simple way to do this would be to allow them to select start and end dates using the dateInput control and then filter the data set being passed to the ggvis calls. Unfortunately, whenever I try to filter, I get errors during run-time.

Below is a minimal, working example. When I uncomment the line "filter(...)" in server.R, I receive an error like:

Error in as.vector(x, "character") : cannot coerce type 'closure' to vector of type 'character'

I've tried a bunch of different variations and always come up with some error. It seems that I'm running up against limits in my understanding of using reactive environments, despite having read the article on reactivity at http://shiny.rstudio.com/articles/reactivity-overview.html and explored a few related examples (e.g. https://gist.github.com/wch/4026749).

Any help will be much appreciated. Suggestions on better ways to implement the above three features (e.g. with googleVis instead of ggvis) would also be appreciated.

Thank you,

Tom


ui.R:


library(shiny)
library(ggvis)
library(lubridate)

shinyUI(pageWithSidebar(

# Application title
headerPanel("Date Data Explorer"),

# Sidebar with a slider input for number of bins
sidebarPanel(
sliderInput("alpha",
"Opacity:",
min = 0,
max = 1,
value = 0.6,
step = 0.05),
dateInput("start",
"Start date:",
value = "2014-07-31",
format = "yyyy-mm-dd"),
dateInput("end",
"End date:",
value = now(),
format = "yyyy-mm-dd")
),

# Show a plot of the generated distribution
mainPanel(
uiOutput("solarplot_ui"),
ggvisOutput("solarplot")
)
))






server.R:


library(shiny)
library(ggvis)
library(dplyr)
library(lubridate)

df <- data.frame(Date = c("08-01-2014", "08-01-2014", "08-02-2014", "08-02-2014"),
Time = c("0:00:00", "12:00:00", "0:00:00", "12:00:00"),
DateTime = c(NA,NA,NA,NA),
yvar = c(1, 2, 2.5, 0.9)
)
df$DateTime <- mdy(df$Date) + hms(df$Time)

shinyServer(function(input, output){

input_alpha <- reactive({input$alpha})
input_startdate <- reactive({(input$start)})
input_enddate <- reactive({(input$end)})
df %>%
#filter(DateTime > ymd(as.character(input_startdate)), DateTime < ymd(as.character(input_enddate))) %>%
ggvis() %>%
layer_points(x = ~DateTime, y = ~yvar, opacity := input_alpha, size := 20) %>%
add_tooltip(function(df) df$yvar) %>%
bind_shiny("solarplot", "solarplot_ui")


})

Fernando Garcia

unread,
Sep 24, 2015, 5:21:32 PM9/24/15
to Shiny - Web Framework for R
Just one comment. 

Do not forget to call the reactive variables with the parenthesis.
input_alpha()
input_startdate()
input_enddate()

eliav schmulewitz

unread,
Sep 25, 2015, 12:47:06 AM9/25/15
to Shiny - Web Framework for R
Did you try '%within%'
something like Date[ymd(Date) %within% new_interval(ymd(unlist(input_startdate)),ymd(unlist(input_enddate)))
i did not try it but i think that is the idea
Reply all
Reply to author
Forward
0 new messages