refresh - R Shiny: Updating .csv input on a schedule -
i have .csv auto-updates every minute new data. i'd have r shiny reupdate every hour new dataset.
from i've seen, involve invalidatelater
or autoinvalidate
; however, i'm not sure how these working (or more importantly, put them).
ui.r pretty straightforward won't include it. server.r below:
library(ggplot2) library(reshape2) library(scales) # data <- reactive({ # data refresh once day # 86400000ms 1 day # invalidatelater(10000, session) # 10s test # }) shinyserver(function(input, output, session) { autoinvalidate <- reactivetimer(10000, session) observe({ x <- read.csv("wait_times.csv", header=t, stringsasfactors=false) x <- rename(x, c("fall.river" = "fall river", "martha.s.vineyard" = "martha's vineyard", "new.bedford" = "new bedford", "north.adams" = "north adams", "south.yarmouth" = "south yarmouth")) cities <- colnames(x)[-1] x[x == 999] <- na x_long <- melt(x, id.var="timestamp") ### not work after attleboro ### x_long <- rename(x_long, c("variable" = "city", "value" = "wait_time")) x_long$timestamp <- strptime(x_long$timestamp, "%d/%m/%y %h:%m %p") weeks <- as.double(difftime(x_long$timestamp[1], x_long$timestamp[length(x_long$timestamp)], units = "weeks")) + 1 print("refreshing csv!") autoinvalidate() }) # pass server-side code initial ui page output$cities <- renderui({ checkboxgroupinput("cities", label = h3("which cities view?"), as.list(cities), selected = "boston") }) output$weeks <- renderui({ sliderinput("weeks", label = h3("how many prior weeks average?"), min = 1, max = weeks, step = 1, value = 1) }) output$main <- renderplot({ x_output <- x_long[x_long$city == input$cities, ] p <- ggplot(x_output, aes(x=timestamp, y=wait_time, group=city)) + geom_line(aes(color=city), size=1.5) + scale_x_datetime(breaks = date_breaks("10 min")) + theme(axis.text.x = element_text(angle = 90, hjust=1), legend.position = "bottom") + labs(x=null, y="waiting time (minutes)") print(p) }) })
when pull out values .csv (like "weeks" , "cities"), r shiny says don't exist:
error in as.list(cities) : object 'cities' not found error in sliderinput("weeks", label = h3("how many prior weeks average?"), : object 'weeks' not found error in func() : object 'x_long' not found
what think happening since these being declared inside observe
function, encapsulated it. perhaps should invalidating/declaring these earlier? i've tried putting them before declaring shinyserver()
hasnt' seemed work either.
i've researched these sources haven't been able replicate:
observe
doesn't return value (it returns observer reference class). want use reactive
instead.
if wanted variable cities
exemple, this:
refrehed_cities <- reactive({ autoinvalidate() x <- read.csv("wait_times.csv", header=t, stringsasfactors=false) x <- rename(x, c("fall.river" = "fall river", "martha.s.vineyard" = "martha's vineyard", "new.bedford" = "new bedford", "north.adams" = "north adams", "south.yarmouth" = "south yarmouth")) cities <- colnames(x)[-1] return cities )}
and use in checkboxgroupinput
:
output$cities <- renderui({ checkboxgroupinput("cities", label = h3("which cities view?"), as.list(refreshed_cities()), selected = "boston") }) })
an other way use function reactivevalues
, enables store reactive values.
Comments
Post a Comment