R:小数据帧10 Mb引发错误:无法分配大小为15.4 Gb

时间:2018-03-05 16:53:29

标签: r memory-management memory-leaks knitr

我正在使用大约50K行乘20列的数据框。作为制表符分隔的文本文件大小约为10 MB。我使用R脚本(.R)对它进行了一些温和的分析,一切正常。我开始在 R Markdown 中编写报告,将所有内容打包在一个漂亮整洁的文档中。

当我运行rmarkdown::render("my_document.Rmd")命令时,会呈现一个漂亮的HTML。偶尔在 Windows10 中我会得到Error: cannot allocate vector of size 15.4 Gb或X Gb。如果我按下键盘上的 Up 箭头,并立即再次运行命令,一切正常,生活还在继续。我不知道为什么错误是间歇性的,但至少在下一次尝试相同命令时会再次起作用。我也很难相信我的原始10 MB文件生成了大小为15.4 Gb的对象,我只在dplyr::filter()dplyr::group_by() %>% summarise(n()) >%> etc.上运行基本命令,我不添加行或列到原始数据框。

如果我在Ubuntu Linux中执行相同的过程,我永远不会收到错误,而我的会话完全冻结了。 Ctrl + Alt + F1 甚至无法正常工作,我不得不重置硬盘。这更令人头疼,因为它每小时发生一次或两次。我已经打开系统监视器,发现当发生这种情况时, rsession 消耗的内存从200 MB到7 GB(基本上都是我的8 Gb RAM)。

我走哪条路?这是一个R问题吗? R Studio? Rmarkdown套餐? knitR包?潘多克问题?我刚刚更新了R(3.4.3),R Studio(1.1.423),我所有的R包,问题仍然存在。我真的不希望得到答案,但我希望得到关于从哪里开始的指导。如果根本原因确定不太可能,我会采取创可贴修复。似乎它可能是。

[编辑]我添加了我的.Rmd文件的编辑版本。但它基本上包含了所有内容,并且我的实际.Rmd文件并不比下面所见的长。如果有帮助,我还会粘贴错误消息。您会注意到我已多次添加空格,这就是您看到div style参数的原因。

---
title: "February Reporting"
date: "Dept XYZ [February 20, 2018]"
output: html_document
---

<div style="margin-bottom:35px;">
</div>

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Introduction

This report meets the requirements of *XYZ123, Body of Standards* at `Revision: 12` `Version: G`. In particular we are concerned with:

* ***2.5 Report Out***
    + *2.5.1* On a periodic basis...
        - *2.5.1.1* The weekly output...
        - *2.5.1.2* The weekly output...
        - *2.5.1.3* The weekly output...
    + *2.5.2* On a periodic basis...
        - *2.5.2.1* The annual output...
        - *2.5.2.2* The annual output...
        - *2.5.2.3* The annual output...
    + *2.5.3* However, etc.
etc.

<div style="margin-bottom:35px;">
</div>

# Data Sources - \[Verified with XYZ]
***
A basic analysis is performed but is not indicative of...
The analysis often utilizes the following variable, represented by:
$$ \text{The Eqation is} = \frac{\text{Multi word variable}}{\text{multi word variable}} $$

这就是我的R Markdown文件。错误就在这里:

  

rmarkdown ::渲染( “mark_output.Rmd”)

     

处理文件:mark_output.Rmd | .........
  |没有R代码的14%普通文本

     

| ................... |   29%标签:设置(带选项)1 $列表包括:logi FALSE

     

| ............................ |   没有R代码的43%普通文本

     

| ..................................... |   57%标签:汽车| ............................................ ..
  |没有R代码的71%普通文本

     

| .............................................. .......... |   86%标签:压力(带选项)3 $ fig.height:num 7 $的列表   fig.width:语言12 +(4/9)$ echo:logi FALSE

     

| .............................................. ................... |   没有R代码的100%普通文本

     

输出文件:mark_output.knit.md

     

“C:/ Users / stackinator / AppData / Local / Pandoc / pandoc”+ RTS -K512m -RTS   mark_output.utf8.md - 到html --from   降价+ autolink_bare_uris + ascii_identifiers + tex_math_single_backslash +智能   --output mark_output.html --email-obfuscation none - self-contained --standalone --section-divs --template“C:\ Users \ stackinator \ Documents \ Rlibs \ rmarkdown \ rmd \ h \ default.html”   --no-highlight --variable highlightjs = 1 - 变量“theme:bootstrap”--include-in-header“C:\ Users \ stackinator \ AppData \ Local \ Temp \ RtmpGi2lRl \ rmarkdown-str460774e669e.html”   --mathjax - 变量“mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML

     

创建的输出:mark_output.html错误:无法分配大小的向量   15.4 Gb

1 个答案:

答案 0 :(得分:1)

头过去看看这个stack overflow answer

简而言之,帖子解释说,在循环情况下,

  

在之前添加knitr::knit_meta(class=NULL, clean = TRUE)   rmarkdown::render(input=file, etc)似乎可以解决问题。