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

如何解决Windows下Shiny应用的C stack usage超限错误?

解决Shiny应用C栈溢出问题(Windows系统)

问题描述

  • Shiny应用在控制台单独运行正常,但清理R环境后执行runApp()时,抛出错误:Error: C stack usage 15924528 is too close to the limit
  • 针对Linux系统的解决方案ulimit -s unlimited在Windows的RStudio中无效
  • 怀疑是gamlss包的getTGD()函数引发递归问题,已尝试将相关代码设为响应式,但仍报错
  • 当前C栈信息:
Cstack_info()

     size    current  direction eval_depth 
  15922790      23160          1          2 

完整代码

library(shiny)
library(gamlss)

da <- readRDS(file="data/mydata.RDS")

gmod <- gamlss::gamlss(Outcome_transf ~cs(Age, by = Education_dich) + Education_dich, family= BE, data=da)


ui <- fluidPage(
  
  titlePanel('Title of app'),
  
  navbarPage("Choose a tab:",
             tabPanel("Title first tab",
                      sidebarLayout(
                        sidebarPanel(numericInput("Outcome", "Outcome measure", min = 15, max = 70, value = 70.00, step=0.01),
                                     numericInput("Age", "Age", min = 0, max = 110, value = 55, step=1),
                                     selectInput("Education_dich", "Educational level", choices = c('Low: Dutch Verhage scale 5 of lower'=0, 'Medium/High: Dutch Verhage scale 6 or 7'=1) )),
                        mainPanel(textOutput("txtOutput") )
                      ),
             ),
             
             
             tabPanel("Title second tab",
                      fluidRow( hr() ) ))
  )


server <- function(input, output, session) {

  
  output$txtOutput = renderText({ 

          input_df <- tryCatch(
            reactive({
            data.frame(Age = input$Age,
                       Education_dich = input$Education_dich,
                       Outcome_transf = input$Outcome/100 ) }),
              error = function(err){ cat('Error in input_df', err$message, '\n') })
    
                  NORM <- tryCatch(
                    reactive({getTGD(gmod, newdata=input_df())$resid}),
                    error = function(err) {
                      cat('Error with getTGD().', err$message, "\n") })
    
                  paste0("The norm score is: ", round(NORM(),2))
                  
    
  })
  

}



shinyApp(ui = ui, server = server)


rm(list = ls())
runApp('M:/users/stackoverflow/')

解决方案

1. 修正响应式对象的错误嵌套

你在renderText()内部嵌套创建reactive()对象,会触发不必要的响应式依赖循环,这是引发栈溢出的核心原因之一。直接在渲染函数内生成数据即可,无需额外的reactive()包装:

server <- function(input, output, session) {
  output$txtOutput = renderText({ 
    # 直接生成输入数据,无需嵌套reactive
    input_df <- tryCatch(
      data.frame(
        Age = input$Age,
        Education_dich = as.integer(input$Education_dich),  # 确保类型匹配模型要求
        Outcome_transf = input$Outcome/100
      ),
      error = function(err){ cat('Error in input_df', err$message, '\n') }
    )
    
    NORM <- tryCatch(
      getTGD(gmod, newdata=input_df)$resid,
      error = function(err) {
        cat('Error with getTGD().', err$message, "\n")
        return(NA)  # 出错时返回NA避免后续报错
      }
    )
    
    if(!is.na(NORM)){
      paste0("The norm score is: ", round(NORM,2))
    } else {
      "无法计算标准分,请检查输入"
    }
  })
}

2. 删除错误的环境清理代码

最后一行的rm(list = ls())会删除包括gmod在内的所有对象,导致getTGD()找不到模型,进而引发递归报错。直接删除该行代码即可。

3. 调整Windows系统的R栈大小

Windows下无法使用ulimit,可通过修改RStudio启动参数增大栈空间:

  • 打开RStudio,依次点击Tools -> Global Options -> General -> Advanced
  • 在"R command line options"中添加--max-ppsize=500000(增大指针保护栈)或--max-stack-size=20000000(直接设置栈大小,单位为字节)
  • 重启RStudio后重新运行应用

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 20:15:39