You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Shiny中构建多元线性回归时回归摘要不显示的问题求助

问题定位与修复方案

核心问题为R语言大小写敏感,你UI端定义的输入输出ID与server端调用的名称大小写不匹配,具体不匹配项如下:

  • UI定义自变量输入ID为全小写indvar,server端错误调用为input$indVar
  • UI定义因变量输入ID为全小写depvar,server端错误调用为input$depVar
  • UI定义自变量打印输出ID为全小写indprint,server端错误绑定为output$indPrint
  • UI定义因变量打印输出ID为全小写depprint,server端错误绑定为output$depPrint

额外优化建议:lm()函数遇到空值会直接报错,建议在调用时增加na.omit()处理数据集,避免用户选到带缺失值的字段时应用异常。

修复后的完整代码如下:

library(shiny)
covid <- read.csv("D:/R/EuropeIndia.csv")
# 可选优化:提前处理空值
covid <- na.omit(covid)
# Define UI for application 
ui <- fluidPage(
  titlePanel("COVID疫苗接种数据回归分析工具"),
  sidebarLayout(
    sidebarPanel(
      helpText("选择自变量"),
      selectInput(inputId = "indvar", label = "自变量", multiple = TRUE,
                  choices = list("total_cases","new_cases", "total_vaccinations", "people_fully_vaccinated", "total_boosters","stringency_index", "population_density", "aged_65_older","gdp_per_capita","exterme_poverty", "cardiovasc_death_rate", "diabetes_prevalence", "handwashing_facilities", "life_expectancy","human_development_index")),
      helpText("选择因变量"),
      selectInput(inputId = "depvar", label = "因变量", multiple = FALSE,
                  choices = list("total_deaths","new_deaths","new_cases"))
    ),
    mainPanel(
      verbatimTextOutput(outputId = "regsum"),
      verbatimTextOutput(outputId = "indprint"),
      verbatimTextOutput(outputId = "depprint")
    )
  )
  
)
server <- function(input, output) {
  
  lm1 <- reactive({lm(reformulate(input$indvar, input$depvar), data = covid)})
  
  output$depprint <- renderPrint({input$depvar})
  output$indprint <- renderPrint({input$indvar})
  output$regsum <- renderPrint({summary(lm1())})
  
}

# 运行应用
shinyApp(ui = ui, server = server)

内容的提问来源于stack exchange,提问作者Hari Chandana Bollimpalli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 11:54:10