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

使用texreg::knitreg编织Rmarkdown时遇readLines连接错误求助

解决R Markdown中使用knitreg时出现的readLines连接错误

我编写了如下简单的R Markdown文件:

---
title: "My R Markdown Example"
output: html_document
---
library(texreg)
model <- lm(mpg ~ cyl, data = mtcars)
knitreg(list(model), caption = "knitreg")

单独运行代码时一切正常,htmlreg等其他texreg命令也能正常工作,但执行knit操作时,knitreg会抛出readLines连接错误,错误信息如下:

Error in `readLines()`:
! cannot open the connection
Backtrace:
 1. texreg::knitreg(list(model), caption = "knitreg")
 2. rmarkdown::all_output_formats(knitr::current_input())
 3. rmarkdown:::enumerate_output_formats(input, output_yaml = output_yaml)
 4. rmarkdown:::read_utf8(input)
 5. base::readLines(con, warn = FALSE)
Execution halted

问题原因及解决方案

这个问题是因为knitreg在自动检测输出格式时,调用rmarkdown::all_output_formats()无法正确获取当前Rmd文件的路径,导致连接打开失败。目前有两种可行的解决办法:

  • 手动指定输出格式
    调用knitreg时直接通过format参数明确指定输出格式,跳过自动检测步骤:

    # 针对HTML输出
    knitreg(list(model), caption = "knitreg", format = "html")
    
    # 针对PDF输出(Latex格式)
    knitreg(list(model), caption = "knitreg", format = "latex")
    
  • 更新texreg包
    该问题在texreg的后续版本中已经被官方修复,直接更新包即可解决:

    install.packages("texreg")
    

    若需要最新开发版,可通过devtools安装:

    devtools::install_github("leifeld/texreg")
    

内容的提问来源于stack exchange,提问作者John-Henry

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 20:02:34