编织器:开始一个新的R会话以清除RAM [英] knitr: starting a fresh R session to clear RAM

查看:66
本文介绍了编织器:开始一个新的R会话以清除RAM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时会处理许多对象,由于块之间的内存问题,重新开始会很高兴.考虑以下示例:

I sometimes work with lots of objects and it would be nice to have a fresh start because of memory issues between chunks. Consider the following example:

警告:我有8GB的RAM.如果您没有很多,这可能会把它全部吃光.

warning: I have 8GB of RAM. If you don't have much, this might eat it all up.

<<chunk1>>=
a <- 1:200000000
@
<<chunk2>>=
b <- 1:200000000
@
<<chunk3>>=
c <- 1:200000000
@

在这种情况下,解决方案是:

The solution in this case is:

<<chunk1>>=
a <- 1:200000000
@
<<chunk2>>=
rm(a)
gc()
b <- 1:200000000
@
<<chunk3>>=
rm(b)
gc()
c <- 1:200000000
@

但是,在我的示例中(由于它依赖于大型数据集而可以发布),即使在删除所有对象并运行gc()之后,R也不会清除所有内存(仅部分内存) .原因在?gc中找到:

However, in my example (which I can post because it relies on a large dataset), even after I remove all of the objects and run gc(), R does not clear all of the memory (only some). The reason is found in ?gc:

However, it can be useful to call ‘gc’ after a large object has
been removed, as this may prompt R to return memory to the
operating system.

请注意重要单词may. R在很多情况下都这样指定may,因此它不是错误.

Note the important word may. R has a lot of situations where it specifies may like this and so it is not a bug.

是否有一个大块选项可以使我knitr开始一个新的R会话?

Is there a chunk option according to which I can have knitr start a new R session?

推荐答案

我的建议是为每个主要任务创建一个单独的.Rnw,将它们编织到.tex文件中,然后使用\includeparent.Rnw文件中以构建完整项目.通过makefile控制项目的建设.

My recommendation would to create an individual .Rnw for each of the major tasks, knit them to .tex files and then use \include or \input in a parent.Rnw file to build the full project. Control the building of the project via a makefile.

但是,要解决此特定问题,请为每个块使用一个新的R会话,您可以使用R包

However, to address this specific question, using a fresh R session for each chunk, you could use the R package subprocess to spawn a R session, run the needed code, extract the results, and then kill the spawned session.

.Rnw文件的简单示例

A simple example .Rnw file

\documentclass{article}
\usepackage{fullpage}
\begin{document}

<<include = FALSE>>=
knitr::opts_chunk$set(collapse = FALSE)
@

<<>>=
library(subprocess)

# define a function to identify the R binary
R_binary <- function () {
  R_exe <- ifelse (tolower(.Platform$OS.type) == "windows", "R.exe", "R")
  return(file.path(R.home("bin"), R_exe))
}
@


<<>>=
# Start a subprocess running vanilla R.
subR <- subprocess::spawn_process(R_binary(), c("--vanilla --quiet"))
Sys.sleep(2) # wait for the process to spawn

# write to the process
subprocess::process_write(subR, "y <- rnorm(100, mean = 2)\n")
subprocess::process_write(subR,  "summary(y)\n")

# read from the process
subprocess::process_read(subR, PIPE_STDOUT)

# kill the process before moving on.
subprocess::process_kill(subR)
@


<<>>=
print(sessionInfo(), local = FALSE)
@

\end{document}

生成以下pdf文件:

这篇关于编织器:开始一个新的R会话以清除RAM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆