Commit c32d64f4 authored by Charles's avatar Charles

update app

parent a6b5e389
rvelovapp.Rproj
#' @import shiny
#' @importFrom DBI dbGetQuery
#' @importFrom dygraphs dygraphOutput dyOptions
app_ui <- function() {
q <- "SELECT DISTINCT number FROM dispo"
choices <- dbGetQuery(pool, q)$number
q <- "SELECT DISTINCT name FROM dispo"
choices <- dbGetQuery(pool, q)$name
tagList(
# Leave this function for adding external resources
golem_add_external_resources(),
# List the first level UI elements here
fluidPage(
h1("rvelov"),
selectInput("number_choice", "Number:",
choices = choices),
dygraphOutput("dispo_over_time", height = "600px")
)
)
}
#' @import shiny
golem_add_external_resources <- function(){
addResourcePath(
'www', system.file('app/www', package = 'rvelov')
)
tags$head(
golem::activate_js(),
golem::favicon()
# Add here all the external resources
# If you have a custom.css in the inst/app/www
# Or for example, you can add shinyalert::useShinyalert() here
#tags$link(rel="stylesheet", type="text/css", href="www/custom.css")
)
}
#' Run the Shiny Application
#'
#' @export
#' @importFrom shiny shinyApp
#' @importFrom golem with_golem_options
#' @importFrom pool poolClose
run_app <- function(...) {
pool <<- dbPool(
drv = RMySQL::MySQL(),
dbname = Sys.getenv("VELOV_DB"),
user = Sys.getenv("VELOV_USER"),
password = Sys.getenv("VELOV_PASSWORD"),
host = Sys.getenv("VELOV_HOST"),
port = as.numeric(Sys.getenv("VELOV_PORT")))
golem::with_golem_options(
app = shinyApp(ui = app_ui,
server = app_server,
onStart = function() {
onStop(function() {
poolClose(pool)
})
}),
golem_opts = list(...)
)
}
library(data.table)
library(ggplot2)
pool <- pool::dbPool(
drv = RMySQL::MySQL(),
dbname = Sys.getenv("VELOV_DB"),
user = Sys.getenv("VELOV_USER"),
password = Sys.getenv("VELOV_PASSWORD"),
host = Sys.getenv("VELOV_HOST"),
port = as.numeric(Sys.getenv("VELOV_PORT"))
)
#' @import shiny
#' @import ggplot2
#' @importFrom pool dbPool
#' @importFrom DBI dbGetQuery
#' @importFrom data.table data.table ":="
#' @importFrom dygraphs dygraph dyRangeSelector renderDygraph
#' @importFrom magrittr '%>%'
app_server <- function(input, output, session) {
function(input, output, session) {
output$dispo_over_time <- renderDygraph({
# Data per station
dispo_data <- reactive({
q <- paste0("SELECT last_update_fme, available_bikes ",
"FROM dispo ",
"WHERE number = ", input$number_choice)
dt <- data.table(dbGetQuery(pool, q))
# browser()
dt[, last_update_fme := as.POSIXct(last_update_fme)]
dygraph(dt,
main = paste0("Disponibilité Vélo'v pour la station ",
input$number_choice)) %>%
dyRangeSelector %>%
dyOptions(colors = "#00225F")
setkey(dt, last_update_fme)
})
}
# Apply filters
dispo_data_filtered <- reactive({
dispo_data()[
last_update_fme >= as.character(input$date_interval[1]) &
last_update_fme <= as.character(input$date_interval[2])
]
})
# Display graph
output$dispo_over_time <- renderPlot({
dt <- dispo_data_filtered()
ggplot(dt, aes(x = last_update_fme, y = available_bikes)) +
geom_line()
})
}
\ No newline at end of file
q <- "SELECT DISTINCT number FROM dispo"
choices <- DBI::dbGetQuery(pool, q)$number
fluidPage(
h1("rvelov"),
selectInput(
inputId = "number_choice",
label = "Number:",
choices = choices
),
dateRangeInput(
inputId = "date_interval",
label = "Date interval",
start = "2020-01-01",
end = "2020-01-31",
min = "2020-01-01",
max = "2020-12-31"
),
plotOutput(
outputId = "dispo_over_time",
height = "600px"
)
)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment