yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.39k stars 878 forks source link

Misleading line numbers for error when using child document #1945

Closed cderv closed 3 years ago

cderv commented 3 years ago

This was reported first in https://github.com/rstudio/rmarkdown/issues/1623

dir.create(tmp_dir <- tempfile())
owd <- setwd(tmp_dir)
xfun::write_utf8(
  c(
    "---",
    "title: test",
    "---",
    "",
    "```{r, child = 'child.Rmd'}",
    "```"),
  "test.Rmd")

xfun::file_string("test.Rmd")
#> ---
#> title: test
#> ---
#> 
#> ```{r, child = 'child.Rmd'}
#> ```

xfun::write_utf8(
  c("---",
    "title: test",
    "---",
    "",
    "```{r chunk4}",
    "stop('error occurs here...')",
    "```"
  ),
  "child.Rmd")

xfun::file_string("child.Rmd")
#> ---
#> title: test
#> ---
#> 
#> ```{r chunk4}
#> stop('error occurs here...')
#> ```

# fail on error
knitr::opts_chunk$set(error = FALSE)

# Knitting the main doc reports wrong
knitr::knit("test.Rmd")
#> processing file: test.Rmd
#> Quitting from lines 3-4 (./child.Rmd) 
#> Quitting from lines 3-4 (./child.Rmd)
#> Error in eval(expr, envir, enclos): error occurs here...

# Knitting only on doc reports ok
knitr::knit("child.Rmd")
#> processing file: child.Rmd
#> Quitting from lines 6-7 (child.Rmd)
#> Error in eval(expr, envir, enclos): error occurs here...
grep("stop", xfun::read_utf8("child.Rmd"))
#> [1] 6

setwd(owd)
unlink(tmp_dir, recursive = TRUE)
github-actions[bot] commented 3 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.