This model was developed for the short-run macroeconomic analysis in my Intermediate Macroeconomics course.
-
shinyapp_ch13_AD_AS.R
This is the main file for the Chapter 13 model. -
ch13_master.qmd
This file contains older models as well as current and additional code for Chapter 13 and/or the Short-Run model.
- Packages and Scripts Loaded
- Libraries are loaded and
source("modules/simulation_calc.R")
is executed.
- Libraries are loaded and
- Helper Function Defined
initializeExperimentSet(...)
is defined.
- UI Defined
- The UI is laid out using
page_sidebar(...)
. - The main panel includes several tab panels: World, Plots, Log/Ratio Scale, and Program Results (with subtabs).
At this point, the UI is rendered in the browser, but no simulation data has been generated yet.
- The UI is laid out using
When s(ui, server)
starts, the server function is invoked once. Inside the server:
- Excel Import
excel_list <- import_list("data/savings_rate_y.xlsx")
loads available Excel sheets for the “World” tab.- Observers for regions, years, and the button to calculate the average savings rate are initialized.
- Create a
reactiveVal
to store experiments. - Observe the “Add” button to insert a new row.
- Render the experiment table with “Delete” buttons.
- Observe “Delete” events to remove rows.
When the user clicks Simulate:
simulate_first_exp_calculations
- An
eventReactive(input$simulate, { ... })
block is triggered. - It checks if the first experiment set is empty. If empty, a modal error is shown; otherwise, it calls Module Call 1 (
simulate_solow
) to run the simulation with the parameters and experiment set from Tab 1.
- An
simulate_counter_exp_calculations
- Runs Module Call 1 again, but with no experiments.
simulate_second_exp_calculations
,simulate_third_exp_calculations
,simulate_fourth_exp_calculations
- Each block uses
eventReactive(input$simulate, { ... })
to run the same Module Call 1 but with experiments from sets 2, 3, or 4 (if available; otherwise, it returnsNULL
).
- Each block uses
All these eventReactive
blocks return their respective data frames of results when the user clicks Simulate.
- Tables Rendered
- Output slots such as
results_first_exp_df
,results_counterfactual_df
,results_second_exp_df
,results_third_exp_df
, andresults_fourth_exp_df
userenderTable({ req(...); ... })
. - Each table displays the data frame returned by its respective simulation.
- Output slots such as
make_plot(...)
Function- A local helper function that constructs a ggplot2 plot.
plot_objects
ReactiveValues- A
reactiveValues()
object is used to store each ggplot output for inclusion in the ZIP.
- A
plot_specs
List- A list of plot definitions (IDs, y-variable names, titles, labels).
- Loop to Render Plots
- A
for (spec in plot_specs)
loop sets upoutput$plot_... <- renderPlot({ ... })
. - Each time the user clicks Simulate or toggles the checkboxes, these plots refresh automatically if any reactive dependencies change.
- A
- Download Data (
downloadData
)- When clicked, this handler pulls the first experiment’s data from
simulate_first_exp_calculations()
and writes it to a CSV file.
- When clicked, this handler pulls the first experiment’s data from
- Download All Plots (
downloadPlots
)- This handler allows the user to download a ZIP file containing all the plots as PNGs.
A typical user flow might look like this:
- The user launches the app, and the UI appears.
- The user selects a region/country in the World tab; observers recalculate the valid years.
- The user clicks Get Avg Savings Rate; the average is computed and populates the slider.
- The user enters experiments in any of the 4 tabs (Sets 1, 2, 3, 4), either by manually adding rows or by uploading a file.
- The user clicks Simulate, which:
- Triggers all the
eventReactive
blocks:- The first scenario is simulated; if there are no rows in Set 1, an error is shown.
- The counterfactual scenario is simulated (with no experiments).
- Additional scenarios for sets 2–4 (if rows exist) are simulated.
- All result data frames (
results_*_df
) refresh. - All plots refresh via
renderPlot
.
- Triggers all the
- The user views the new results in the Program Results tab and sees updated plots in the Plots and Log/Ratio Scale tabs.
- Optionally, the user toggles checkboxes to show or hide lines for the second, third, and fourth scenarios (or the counterfactual), and the plots update automatically.
- Optionally, the user clicks Download Results to obtain a CSV of the first scenario or Download All Plots to download a ZIP file of PNGs.