Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
723 views
in Technique[技术] by (71.8m points)

r - rmarkdown::render() in a loop - cannot allocate vector of size

I have a bunch of directories with Rmd files to be compiled into HTML. Each call to rmarkdown::render includes a large list of params. Everything works fine when I render the Rmd files individually.

However, when I execute a function that loops over the directories and builds the HTML files it crashes after it has completed nine (it is not a specific Rmd causing the problem, it crashes on the 9th regardless of the order).

The relevant part of the error traceback is:

Error: cannot allocate vector of size 38.4 Gb

8. knitr::knit_meta_add(old_knit_meta, attr(old_knit_meta, "knit_meta_id")) 

7. rmarkdown::render(input = RMDfile, output_file = RMDfileout, 
        output_format = output, output_dir = dir, param = params, 
        quiet = quiet)

The problem appears to be caused by this line in rmarkdown::render:

 on.exit({
    knit_meta_reset()
    if (length(old_knit_meta)) {
      knitr::knit_meta_add(old_knit_meta, attr(old_knit_meta, 
                                               "knit_meta_id"))
    }
  }, add = TRUE)

It appears to me that the params from the previous calls are being saved in the meta-data, and is getting too large after rmarkdown::render is called 9 times in the function.

rmarkdown::knitr has a knit_meta argument, but the help documentation says "(For expert use) Meta data generated by knitr" and I'm no knitr expert.

The Rmd files are completely independent from each other. Is there a way to reset the knitr meta-data for each call to rmarkdown::render?

I'm hoping someone can offer a quick fix or workaround for this. If not, I'll develop an example to reproduce the problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After digging around in rmarkdown::render code for a while I've found a solution.

Adding knitr::knit_meta(class=NULL, clean = TRUE) before rmarkdown::render(input=file, etc) seems to do the trick..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...