Hi community
Im try to make a reactive Shiny app. Im have the code of the each plot that I need but dont run well in shiny. The ides is selece "Lugar" an the app show the plot. Are 4 "Lugar" (sites).
library(readxl)
library(tidyverse)
library(shiny)
server <- function(input, output, session) {
precip = read_excel("meses2.xlsx", sheet="F")
#Summarize Data and then Plot
data <- reactive({
req(input$sel_Lugar)
df <- precip %>% filter(Lugar %in% input$sel_Lugar) %>% group_by(Mes) %>% summarise(precip = sum(Medicion))
})
#Update SelectInput Dynamically
observe({
updateSelectInput(session, "sel_Lugar", choices = precip$Lugar)
})
#Plot
output$plot <- renderPlot({
ggplot(data(),aes(x=sel_Lugar, y=Medicion, fill=Mes,na.rm = TRUE)) +
geom_col() +
scale_fill_manual(values=c("#E23D2C","#BAA512","#512B9A","#21AB3F")) +
labs(title = "Distribución general de la precipitación",
caption= "Fuente: propia")+
ylab("Precipitación (mm)") +
theme_light()
})
}
ui <- basicPage(
h1("R Shiny Dynamically create Drop Down List"),
selectInput(inputId = "sel_Lugar",
label = "Choose Sales Rep",
"Names"),
plotOutput("plot")
)
shinyApp(ui = ui, server = server)
In one sheet/tabs put this plot and in other sheet/tabs the reactive plots.
Im want that when select the "lugar" appear the plot. Are 4 "Lugar". For reactive plot
d_New<-read_excel("meses2.xlsx", sheet="F")
ggplot(d_New, aes(x=Lugar, y=Medicion, fill=Mes,na.rm = TRUE)) +
geom_col() +
scale_fill_manual(values=c("#E23D2C","#BAA512","#512B9A","#21AB3F")) +
labs(title = "Distribución general de la precipitación",
caption= "Fuente: propia")+
ylab("Precipitación (mm)") +
theme_light()
The app run and show this.
Im check various tutorial but dont run well.
Data example
structure(list(Mes = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L), .Label = c("Marzo", "Abril", "Mayo"), class = "factor"),
Dia = c(17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
17, 18, 19, 29, 30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), Medicion = c(50,
17, 13, 12, 3, 6, 0.3, NA, 13, 1, 4, 20, 3, NA, 0, 0, 0,
0, 20, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 0, 0, 5, 9,
0, 1, 0, 0, 0, 0, 2, 17, 58, 32, 0, 9, 0.5, 1, 25, 1, 0.5,
4, 10, 3, 3, 8, 36, 13, 1, 0.5, 0.5), Lugar = c("UNIAGRARIA",
"UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA",
"UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA",
"UNIAGRARIA", "UNIAGRARIA", "GRANADA NORTE", "GRANADA NORTE",
"GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE",
"RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO",
"RIO FRIO", "RIO FRIO", "RIO FRIO", "CHICU", "CHICU", "CHICU",
"CHICU", "CHICU", "CHICU", "CHICU", "CHICU", "CHICU", "CHICU",
"CHICU", "RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO",
"RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO",
"RIO FRIO", "RIO FRIO", "GRANADA NORTE", "GRANADA NORTE",
"GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE",
"GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE",
"GRANADA NORTE")), row.names = c(NA, -60L), class = c("tbl_df",
"tbl", "data.frame"))
Thanks