Using source() in Shiny

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?

Your first call to install.packages has the "i" capitalized.

Fixed - it crept in as I copy/pasted my code into here. However, the issue with source() still persists.

Error in file(filename, "r", encoding = encoding) : 
  cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
  cannot open file 'rpert.R': No such file or directory

It seems that you wrote code that requires rpert.R to be a file in the path you set as your working directory and it isnt.

So move it to there ? Or repoint your working directory ?

1 Like

You can place any .R files your app is dependent upon in a folder R/ alongside your app. They will be automatically sourced when the app is started.

As of Shiny version 1.3.2.9001, any .R files found in an R/ directory adjacent to your app will be automatically loaded when your app starts. Just like R packages, only the files at the top level of R/ are considered; nested directories are ignored. Files in this directory are sourced in alphabetical order and any variables, functions, or modules they create are available to be used in your app.R , ui.R , or server.R files.

https://shiny.posit.co/r/articles/build/app-formats/index.html#the-r-directory

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.