可以将模型传递到闪亮的应用程序中输出吗?

Possible to pass a model to output in a shiny app?

我想 select 一个功能和模型(从侧边栏下拉菜单),并能够将模型传递到特定的输出,在那里我打印模型的摘要并显示模型的拟合程度以图形方式。我目前在 server.R 中有一个反应函数,它检查哪个 input$model 是 selected,然后拟合模型并 returns 它。当我尝试从 output$evaluation 调用这个反应函数时,我得到了错误。我不知道该怎么做。

# server.R

#...

fitter <- reactive({

  df_clean <- dataset() # another reactive function that selects the dataset to be used
  rownames(df_clean) <- df_clean$timestamp
  df_clean$timestamp <- NULL

  if (input$Model == 'Linear'){
    fit <- lm(input$Response ~., data=df_clean)
  }
  #... more if statements checking for other model types

  return(fit)

})

# Model Evaluation

output$Evaluation <- renderPrint({

  summary(fitter())

})

您可以使用 as.formula.

lm 调用中的字符串转换为 formula
library(shiny)

shinyApp(
    shinyUI(
      fluidPage(
        inputPanel(
            selectInput("Model", "Model:", choices=c("Linear", "Other")),
            selectInput("Response", "Response:", choices=c("mpg", "disp"))
        ),
        tableOutput("Evaluation")
      )
    ),
    shinyServer(function(input, output, session) {
        fitter <- reactive({
            df_clean <- mtcars

            if (input$Model == 'Linear'){
                fit <- lm(as.formula(paste(input$Response, "~.")), data=df_clean)
            }
            return(fit)
        })
        output$Evaluation <- renderTable({
            summary(fitter())
        })
    })
)