beaumontmeteo/app.R
2025-10-02 10:42:13 +01:00

96 lines
2.6 KiB
R

# Load necessary libraries
library(shiny)
library(jsonlite)
library(httr)
library(shinycssloaders) # For the spinner
# Assuming you have your custom functions in a file called 'your_functions.R'
# List all R files in the directory and source them
lapply(list.files(path = "R", pattern = "\\.R$", full.names = T), source)
source("data/secrets")
# Create a Bearer token header
headers.default <- add_headers(
accept = "*/*",
apikey = token4
)
# Load station data
# Define UI for application
ui <- fluidPage(
tags$head(
tags$style(HTML("
#main {
height: 100vh; /* Full height of the viewport */
overflow-y: auto; /* Scroll if content overflows */
}
.container-fluid {
height: 100vh; /* Full height of the viewport */
display: flex;
flex-direction: column;
}
.row {
flex-grow: 1;
display: flex;
}
.col-sm-8 {
flex-grow: 1; /* Allow the main panel to grow */
}
"))
),
# Application title
titlePanel("Beaumont aka SECMONT"),
# Sidebar for user inputs
sidebarLayout(
sidebarPanel(
width=2,
numericInput(inputId = "daysBefore", label = "Nb jour à check:", value=10, min=1)
),
# Main panel for displaying plot
mainPanel(
width = 6,
withSpinner( plotOutput("dataPlot"))
)
)
)
# Define server logic required to generate and plot data
server <- function(input, output) {
# Reactive expression to fetch data based on input and display spinner while loading
# Render the plot
output$dataPlot <- renderPlot({
startdate <- format(Sys.Date() - input$daysBefore, "%Y-%m-%d")
vignass.coor=c(44.8550665,5.8441789)
allstations <- read.csv("data/allstations.csv")
test1 <- getAllFromCoord(vignass.coor, start_date = startdate, end_date = format(Sys.Date(), "%Y-%m-%d"), allstations, headers = headers.default,btw_station_sleep = .5,within_statio_sleep = 1)
# Prepare colors
cols <- palette.colors()[1:length(unique(test1$Nom_usuel))]
names(cols) <- unique(test1$Nom_usuel)
# Filter data
testsep <- test1
testsep[testsep[,3] == 0 & !is.na(testsep[,3]), c(3,4)] <- NA
plot(getDate(testsep[,2]), testsep[,3], pch = 20, col = adjustcolor(cols[testsep$Nom_usuel], .4), cex = 1.3, ylim = c(0, 8))
# Add legend
legend("topleft", col = cols, legend = names(cols), pch = 20, cex = 1)
# Highlight specific date
abline(v = as.numeric(as.POSIXlt("2024-08-07", format = "%Y-%m-%d")), lwd = 3, col = "red")
})
}
# Run the application
shinyApp(ui = ui, server = server)