I am building a shiny application which visualises the output of a complex model. The model is in a script which is sourced by the shiny app. I want to change one input of the model calculation with selectInput and plot the output model accordingly.
Below is a simplied version of the model's source script and the app.
model.R
#data of the model
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)
#the variable that should be updated by selectInput in the app.R
selected_data <- "yellow_data"
#the model output which should change according to changes in selected_data
model_output <- union(data_model[[selected_data]] * 200, third_data)
I want to change the value of the variable selected_data from the app.
app.R
library(shiny)
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:8), y = ~model_output, name = 'model output', type = 'scatter', mode ='lines')
fig
})
}
shinyApp(ui, server)
As you can see in app.R, I tried to update with selectInput the global variable selected_data which is used to compute model_output in model.R, which is sourced in app.R.
I would like the model_output to update and reflect according to selectInput.
I really hope you can help. Thank you
Perhaps you are looking for this
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({
#the model output which should change according to changes in selected_data
model_output <- c(data_model[,as.character(input$select_data)]*200, third_data)
fig <- plot_ly(x = ~c(1:8), y = ~model_output, name = 'model output', type = 'scatter', mode ='lines')
fig
})
}
shinyApp(ui, server)