R Shiny Sources Twice

This might be intended behavior but I've been coding in Shiny for 2 years and have never seen any mention of this. Essentially, if you make a Shiny app and have "R" as a folder, all scripts sourced from that folder will run twice whenever the app runs and will be sourced before everything else.

# app.R
library(shiny)

source("R/src1.R")
source("dog/src2.R")
source("R/src3.R")

ui <- fluidPage(
    titlePanel(""),
    sidebarLayout(
        sidebarPanel(),
        mainPanel()
    )
)

server <- function(input, output) { }
shinyApp(ui = ui, server = server)
# R/src1.R
print("src1")
# dog/src2.R
print("src2")
# R/src3.R
print("src3")

I'm really curious whether anyone else has encountered this behavior ... it took almost a whole evening for me to diagnose and really cut into my development time. Here was my output:

[1] "src1"
[1] "src3"
[1] "src1"
[1] "src2"
[1] "src3"

Yes this is intended behaviour. Please see the section

The R/ directory

here:

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.

1 Like

This topic was automatically closed 7 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.