I have a working script that I'd like to convert to a shiny app for users. To run, the script calls functions from a source file.
The issue I'm having I think with shiny is the user selects their inputs but the values aren't interacting with the script. Normally those variables are hardcoded in by me, but if the user is selecting values they should show up right?
I sourced the script after the user selects their input so that it would register the new values selected.
# source for script
source('source.R', local = TRUE)
library(shiny)
ui <- fluidPage(
titlePanel("Submarket Snapshot"),
#sidebarLayout(
sidebarPanel(dateInput('report_quarter',
label = 'Enter Quarter',
min = "1900-01-01",
max = Sys.Date(),
format = "yyyy-mm-dd",
startview = "year"),
selectInput('report_market','Submarket:',
c('LA-Ventura','Orange County','Inland Empire'))),
mainPanel(plotOutput('marketview_ppt'))
)
server <- function(input, output, session) {
output$marketview_ppt <- renderPlot({
# script
source('script.R', local = TRUE)
})
}
shinyApp(ui = ui, server = server)
Edit:
The script connects to postgres and uses 'report_quarter' to grab information for that quarter. After it grabs all that information it filters by market 'report_market'. The variables are used all throughout both the script and source. Here's a snippet of the data being plotted based off quarter and market.
plot_vacancy_rate = function(historical_data,
markets = c("Inland Empire"),
report_quarter,
past_n_years = 4){
stats = historical_data %>%
filter(Market %in% report_markets,
`Snapshot Date` <= report_quarter,
`Snapshot Date` >= report_quarter - years(past_n_years)) %>%
group_by(`Snapshot Date`) %>%
summarise(vacancy_rate = sum(`Vacant SF - Total`, na.rm=T)/sum(NRA, na.rm=T))
quarter_labels = stats %>%
distinct(`Snapshot Date`) %>%
mutate(quarter = quarter(`Snapshot Date`)) %>%
group_by(year = year(`Snapshot Date`)) %>%
mutate(qlabel = case_when(quarter == min(quarter) ~ str_c(year(`Snapshot Date`), " Q", quarter),
T ~ str_c("Q", quarter))) %>%
ungroup() %>%
select(-quarter, -year)
stats %>%
ggplot(., aes(x = `Snapshot Date`, y = vacancy_rate))+
geom_col(fill = cbre_pal[1])+
geom_text(aes(y = vacancy_rate+max(vacancy_rate)/50, label = scales::percent(vacancy_rate, accuracy = .1)
}