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

开发Shiny应用调用MIRT包实现IRT分析的表格与可视化报错求助

Shiny应用报错求助:上传CSV后图表和表格提示Error: argument is of length zero

我是Shiny新手,正在构建仪表盘应用,需要实现三个功能:

  • 接收用户上传的CSV数据文件——此功能正常,应用内显示“上传完成”提示;
  • 使用mirt包基于上传的数据生成可视化图表;
  • 使用mirt包基于上传的数据生成表格输出。

但上传CSV数据文件后,图表和表格输出均出现报错:Error: argument is of length zero

以下是我的代码:

ui <- fluidPage(
   titlePanel(p("Let's Analyze with IRT!", style = "color:teal")),
  sidebarLayout(
    sidebarPanel(
     h3("Upload Your Data"), 
      h5(em("Remember your data should be a matrix with dichotomous responses of 0 or 1 stored in a .csv file")),
     fileInput(inputId = "filedata",
          label = "Choose csv file",
          accept = c(".csv")), 
                 p("Made with", a("Shiny",
                 href = "http://shiny.rstudio.com")),
                 img(src = base64enc::dataURI(file = xxx, 
                                              mime = "image/png"))),
  mainPanel("ICC Plot and Item Parameter Table",
           tableOutput("irtPT")
            )
  )
)

server <- function(input, output){
  mydata <- reactive({
    req(input$filedata)
    read.csv(input$filedata$datapath,  header = FALSE)
  })

  output$irtPT <- renderTable({
    unimodel <- mirt.model("latent = ")

    modT2 <- mirt::mirt(data = mydata,
               model = unimodel,
               itemtype = "Rasch",
               SE = TRUE)

    coef.rasch3 <- coef(modT2, IRTpars=TRUE, simplify=TRUE)

    items.rasch3 <- as.data.frame(coef.rasch3$items)

    IR3e <- items.rasch3 %>% select(-c("g", "u"))

    colnames(IR3e)[1] <- "Item Discrimination Parameter"
    colnames(IR3e)[2] <- "Item Location Parameter"

    print(IR3e)
  }, rownames = TRUE)
 }

shinyApp(ui = ui, server = server)

错误原因与解决方案

核心问题1:Reactive对象调用错误

mydata是响应式对象,必须通过mydata()的形式调用才能获取实际数据,直接传递mydata给mirt()相当于传入了对象本身而非数据,导致函数接收到空值,触发报错。

核心问题2:IRT模型定义不完整

mirt.model("latent = ")未指定潜变量对应的项目,Rasch模型要求明确所有项目归属于同一个潜变量,需根据数据列数(项目数)动态生成模型公式。

修正后的完整代码

ui <- fluidPage(
  titlePanel(p("Let's Analyze with IRT!", style = "color:teal")),
  sidebarLayout(
    sidebarPanel(
      h3("Upload Your Data"), 
      h5(em("Remember your data should be a matrix with dichotomous responses of 0 or 1 stored in a .csv file")),
      fileInput(inputId = "filedata",
                label = "Choose csv file",
                accept = c(".csv")), 
      p("Made with", "Shiny"),
      # 替换xxx为你的本地图片路径,或直接删除该行
      img(src = base64enc::dataURI(file = "your_image_path.png", 
                                   mime = "image/png"))
    ),
    mainPanel(
      h3("ICC Plot and Item Parameter Table"),
      tableOutput("irtPT"),
      plotOutput("irtPlot") # 新增图表输出控件
    )
  )
)

server <- function(input, output){
  mydata <- reactive({
    req(input$filedata)
    # 将数据转为矩阵,符合mirt输入要求
    data <- read.csv(input$filedata$datapath, header = FALSE)
    as.matrix(data)
  })

  # 修正后的表格输出
  output$irtPT <- renderTable({
    # 自动获取项目数,动态生成IRT模型
    item_count <- ncol(mydata())
    unimodel <- mirt.model(paste0("latent = 1-", item_count))
    
    # 调用响应式对象必须加括号
    modT2 <- mirt::mirt(data = mydata(),
                        model = unimodel,
                        itemtype = "Rasch",
                        SE = TRUE)

    coef.rasch3 <- coef(modT2, IRTpars=TRUE, simplify=TRUE)
    items.rasch3 <- as.data.frame(coef.rasch3$items)
    # 用dplyr::select避免命名冲突
    IR3e <- items.rasch3 %>% dplyr::select(-c("g", "u"))
    
    colnames(IR3e)[1] <- "Item Discrimination Parameter"
    colnames(IR3e)[2] <- "Item Location Parameter"
    
    IR3e
  }, rownames = TRUE)
  
  # 新增ICC可视化图表
  output$irtPlot <- renderPlot({
    item_count <- ncol(mydata())
    unimodel <- mirt.model(paste0("latent = 1-", item_count))
    modT2 <- mirt::mirt(data = mydata(),
                        model = unimodel,
                        itemtype = "Rasch",
                        SE = TRUE)
    # 绘制项目特征曲线(ICC)
    plot(modT2, type = "trace")
  })
}

shinyApp(ui = ui, server = server)

额外注意事项

  • 确保上传的CSV是纯二分(0/1)数据矩阵,无表头、无非数值列;
  • 运行代码前需加载必要包:library(shiny)、library(mirt)、library(dplyr);
  • 图片路径需替换为实际本地路径,不需要图片可直接删除img标签。

内容的提问来源于stack exchange,提问作者CNM

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 03:25:44