我正在构建一个闪亮的应用程序,它可以可视化复杂模型的输出。该模型是在一个脚本,这是由闪亮的应用程序来源。我想用selectInput更改模型计算的一个输入,并相应地绘制输出模型。
下面是模型源脚本和应用程序的简化版本。
模型。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)
我想从应用程序中更改变量selected_data的值。
应用程序。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)
你可以在app中看到。R,我试图用selectInput更新全局变量selected_data,用于在模型中计算model_output。R,它来源于app。R.我希望model_output根据selectInput更新和反映。
我真的希望你能帮忙。谢谢你也许你正在寻找这个
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)