I am building a Shiny app that translates the following R code:
# Set working directory
setwd("/path/to/files")
# Install required packages
# install.packages("mc2d) # for 2D Monte Carlo simulations
# install.packages("truncnorm") # for truncated Normal distribution
# install.packages("yaml") # for importing YAML files
# Load required packages
library(mc2d)
library(truncnorm)
library(yaml)
# Import functions
source("rpert.R")
source("read_params.R")
source("run_compartmental_model.R")
# Specify required arguments
file_path <- "/another/path/to/files"
file_type <- "yaml" # this will always be the case
seed_value <- NULL # set random seed for reproducibility - can be an integer of any length
# Import scenario parameter file
read_params(file_path = file_path, file_type = file_type)
# Run model
run_compartmental_model()
Here is my attempt, but the code is buggy:
# Install and load required packages if not already installed
if (!requireNamespace("shiny", quietly = TRUE)) {
install.packages("shiny")
}
if (!requireNamespace("mc2d", quietly = TRUE)) {
install.packages("mc2d")
}
if (!requireNamespace("truncnorm", quietly = TRUE)) {
install.packages("truncnorm")
}
if (!requireNamespace("yaml", quietly = TRUE)) {
install.packages("yaml")
}
library(shiny)
library(mc2d)
library(truncnorm)
library(yaml)
# Import GBADs functions
source("rpert.R")
source("read_params.R")
source("run_compartmental_model.R")
# Define the UI
ui <- fluidPage(
fileInput("file", "Choose YAML File"),
actionButton("runButton", "Run Model")
)
# Define the server
server <- function(input, output) {
observeEvent(input$runButton, {
# Check if a file is selected
if (is.null(input$file)) {
return(invisible())
}
# Set working directory to the location of the selected file
setwd(dirname(input$file$datapath))
# Read parameters from the selected file
read_params(file_path = input$file$datapath, file_type = "yaml")
# Run the compartmental model
run_compartmental_model()
})
}
# Create a shiny app
shinyApp(ui, server)
I believe the issue arise with source()
.
Any thoughts on how to get this working?