【问题标题】:Possible to pass a model to output in a shiny app?可以将模型传递给闪亮的应用程序中的输出吗?
【发布时间】:2015-08-07 17:02:54
【问题描述】:

我想选择一个特征和模型(从侧边栏下拉菜单中),并能够将模型传递到一个特定的输出,我打印模型的摘要并以图形方式显示模型的拟合程度。我目前在 server.R 中有一个反应函数,它检查选择了哪个 input$model,然后拟合模型并返回它。当我尝试从 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())

})

【问题讨论】:

    标签: r shiny rstudio


    【解决方案1】:

    您可以使用as.formulalm 调用中的字符串转换为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())
            })
        })
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-20
      • 2015-12-28
      • 2016-11-15
      • 2018-11-09
      • 1970-01-01
      • 2021-04-07
      相关资源
      最近更新 更多