0

I am building a shiny application which visualises the output of a complex model. The model is in script model.R and the inputs of the model in inputs.R script. The scripts are sourced by the shiny app. I want to change one input with selectInput in inputs.R so that the model.R and the shiny app.R update accordingly.

Below is a simplied version of the model, inputs source scripts and the app.

inputs.R

yellow_data <- c (10,28,14,40)
green_data <- c(20,40,50,90)
red_data <- c(50,50,50,80)

third_data <- c(30,32,3100,500)


data_model <- data.frame(yellow_data, green_data, red_data)

selected_data <- "yellow_data"

model.R

source("inputs.R")

model_calculation1 <- union(data_model[[selected_data]] * 200, third_data)

model_calculation2 <- lapply(list(a <- c(1.2,4.5,5,64,5,6,7,10), b <- c(3,5,45,3,2,1,6,8)), "*",model_calculation1)
  
model_calculation3 <- lapply(model_calculation2, "*", (model_calculation1)^2 * 3)
  
model_calculation4 <- mapply("+",model_calculation3[1], model_calculation2[1])
  
model_calculation5 <- mean(model_calculation4)
  
model_calculation6 <- model_calculation5 + model_calculation4
  
model_calculation7 <- model_calculation6 / 10000
  
#from 8........... until 999

model_calculation999 <- model_calculation7 / 2000 #here I use model_calculation7 to simplify but it would be model_calculation998


model_output <- union(model_calculation999 * 200, third_data)

I want to change the value of the variable selected_data from the app.

app.R

source("inputs.R")
source("model.R")

ui <- fluidPage(
  selectInput(inputId = "select_data", label = "SELECT DATA", choices = c("Yellow" = "yellow_data",
                                                                          "Green" = "green_data",
                                                                          "Red" = "red_data")),  
  plotlyOutput(outputId = "plot_model_output")
)

server <- function(input, output, session) {
  
  
  output$plot_model_output <- renderPlotly({ 
    
    selected_data_Change <- reactive({
      switch(input$select_data,
             "Yellow" = "yellow_data",
             "Green" = "green_data",
             "Red" = "red_data") 
    })
    
    selected_data <<- selected_data_Change()
    
    
    fig <- plot_ly(x = ~c(1:12), y = ~model_output, name = 'model output', type = 'scatter', mode ='lines')
    fig
    
  })
}

shinyApp(ui, server)

I tried to update the variable as above with reactivity but it does not work. I also tried with moving the source("") from outside the function to within the function Renderplotly.

If I insert all the model.R script within the function with the appropriate amendement model_calculation1 <- union(data_model[[input$select_data]] * 200, third_data), the code would work but the script of the app would be too large because the app and the model script are much longer than this simplified version.

I want to change the value of selected_data from app.R without rewriting all inputs.R and model.R in the app.R.

I hope you can help. Thank you

jhonccc
  • 59
  • 5
  • You can use modules for this kind of thing, modules will allow you to return a reactive result while reusing your code – Bruno Oct 18 '21 at 22:19
  • Can you rewrite model.R to a function? That way you can `source('model.R')`, load inputs directly in the app and used `select_var` with the `model_function()` in a reactive. – George Oct 19 '21 at 03:24
  • Thanks, following your hints I found this collection of resources on modules which I hope you can find useful https://beta.rstudioconnect.com/content/2816/Shiny-Modules.html and an explanatory answer on reactive here https://stackoverflow.com/questions/53016404/advantages-of-reactive-vs-observe-vs-observeevent However, I am new to the topic which is still not clear to me. Could you show me what would be the most efficient way to do it in this MRE? – jhonccc Oct 19 '21 at 21:49

0 Answers0