Combining server part of R Shiny with source() to implement calculations

Hi! I have an immigration model coded in 3 R files that should be run consecutively. It starts with file-1 to take 3 inputs (simple integers) and a couple of excel and csv files. Then it builds up the next variables and functions one over the other. The output of the final file (file-3) is a data frame and some plots of that data frame. The whole code within these 3 files is around 800 lines and includes fetching data files, creating new variables, defining functions, and calculations to create data frames.
Now I want to create a shiny app for this model so that the user can tweak the first 3 variables and see the plots to understand how different scenarios work. Every time the user changes the inputs, the model is supposed to take these new inputs and calculate the whole model to give a new data frame which is then the basis for the plots in the app. The problem is including the model within the app (sourcing the app with the model in the server part). I have created a sample file (new1.R) to represent how the model starts with 3 input variables (e, f, and g). The file works well outside of the app and gives me the final plots.

##################################
e <- 2
f <- 3
g <- 4

h <- e * f
i <- g / e

df1 <- tibble(Month = c(12,1,2,3,4,5, 6, 7, 8),
year = c(2022,2023,2023,2023,2023, 2023,2023,2023, 2023),
value = c(1,2,3,4,5,6,7,8,9)) %>%
mutate(date = as.Date(paste0("01-",Month,"-",year), format = "%d-%m-%Y")) %>%
mutate(value = value * h)

df2 <- tibble(year = c(2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022),
measure = c(23, 26, 36, 45, 57, 67, 80, 99, 120, 170)) %>%
mutate(date = as.Date(year)) %>%
mutate(measure = measure * i)

plot1 <- ggplot(df1) + geom_line(aes(date, value))
plot2 <- ggplot(df2) + geom_line(aes(year, measure))
##################################

But when I include it within the app, using the source() function, it doesn‘t work. Sometimes it cannot run the line h <- e * f and sometimes it gives me reactivity errors. To use the file in the app, I removed the first 3 lines of the file (defining e, f, and g) so that the file is fed by the user inputs. Here you can find the relevant part of the code for the app:

##################################
library(shiny)
library(dplyr)

ui <- fluidPage(
titlePanel("Test App"),
sidebarPanel(width = 4,
sliderInput("ee", label="Select ee", min = -5 , max = 5, step = 1, value = 1),
sliderInput("ff", label="Select ff", min = -10 , max = 10, step = 1, value = -2),
sliderInput("gg", label="Select gg", min = 0 , max = 10, step = 1, value = 2)
),

  mainPanel(
    fluidRow(
      plotOutput("myPlot1"),
      plotOutput("myPlot2")
    ) 
    ) 
) 

server <- function(input, output, session) {
reactive({
e <- input$ee
f <- input$ff
g <- input$gg
source("new1.R", local = TRUE)
})
output$myPlot1 <- renderPlot(plot1)
output$myplot2 <- renderPlot(plot2)
}

shinyApp(ui = ui, server = server)
##################################

So, I would be pleased if you could help me with the issue. Thanks in advance.

for my money its not a good idea to source plain scripts into a shiny app.
A good rule of thumb is to only source scripts containing nothing but function definitions; have them load into global or the shiny app's environment; make your shiny app call your functions to achieve your functionality.
Thats my advice to you.

Thanks for your reply.
So what is your recommendation to include my model and its calculations within the app?

models are data; they can be loaded with a simple command.
calculations are provided by functions; so if you don't have a script with your functions; you will need to write one.

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.