Webtopia
Webtopia

Reputation: 156

R Shiny Dashboard - Loading Scripts using source('file.R')

Introduction

I have created an R shiny dashboard app that is quickly getting quite complex. I have over 1300 lines of code all sitting in app.R and it works. I'm using RStudio.

My application has a sidebar and tabs and rather than using modules I dynamically grab the siderbar and tab IDs to generate a unique identifier when plotting graphs etc.

I'm trying to reorganise it to be more manageable and split it into tasks for other programmers but I'm running into errors.

Working Code

My original code has a number of library statements and sets the working directory to the code location.

rm(list = ls())
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
getwd()

I then have a range of functions that sit outside the ui/server functions so are only loaded once (not reactive). These are all called from within the server by setting the reactive values and calling the functions from within something like a renderPlot. Some of them are nested, so a function in server calls a function just in regular app.R which in turn calls another one. Eg.

# Start of month calculation
som <- function(x) {
  toReturn <- as.Date(format(x, "%Y-%m-01"))
  return(toReturn)
}

start_fc <- function(){
  fc_start_date <- som(today())
  return(fc_start_date)
}

then in server something like this (code incomplete)

server <- function(input, output, session) {
  RV <- reactiveValues()
  observe({
    RV$selection <- input[[input$sidebar]]
  #  cat("Selected:",RV$selection,"\r")
  })
  
.......
    cat(paste0("modelType: ",input[[paste0(RV$selection,"-modeltype")]]," \n"))
    vline1 <- decimal_date(start_pred(input[[paste0(RV$selection,"-modeltype")]],input[[paste0(RV$selection,"-modelrange")]][1]))
    vline2 <- decimal_date(start_fc())
.......

Problem Code

So now when I take all my functions and put them into different .R files I get errors indicating the functions haven't been loaded. If I load the source files by highlighting them and Alt-Enter running them so they are loaded into memory then click on Run App the code works. But if I rely on Run App to load those source files, and the functions within them, the functions can't be found.

source('./functionsGeneral.R')
source('./functionsQuote.R')
source('./functionsNewBusiness.R')

source('./ui.R')
source('./server.R')

shinyApp(ui, server)

where ui.R is

source('./header.R')
source('./sidebar.R')
source('./body.R')

source('./functionsUI.R')

ui <- dashboardPage( 
  header,
  sidebar,
  body
)

Finally the questions

In what order does R Shiny Dashboard run the code. Why does it fail when I put the exact same inline code into another file and reference it with source('./functions.R')? Does it not load into memory during a shiny app session? What am I missing?

Any help on this would be greatly appreciated.

Thanks, Travis

Upvotes: 1

Views: 519

Answers (1)

Webtopia
Webtopia

Reputation: 156

Ok I've discovered the easiest way is to create a subfolder called R and to place the preload code into that folder. From shiny version 1.5 all this code in the R folder is loaded first automatically.

Upvotes: 0

Related Questions