Tricks to manage the available memory in an R session

前端 未结 27 1484
情深已故
情深已故 2020-11-22 01:23

What tricks do people use to manage the available memory of an interactive R session? I use the functions below [based on postings by Petr Pikal and David Hinds to the r-he

相关标签:
27条回答
  • 2020-11-22 02:02

    You also can get some benefit using knitr and puting your script in Rmd chuncks.

    I usually divide the code in different chunks and select which one will save a checkpoint to cache or to a RDS file, and

    Over there you can set a chunk to be saved to "cache", or you can decide to run or not a particular chunk. In this way, in a first run you can process only "part 1", another execution you can select only "part 2", etc.

    Example:

    part1
    ```{r corpus, warning=FALSE, cache=TRUE, message=FALSE, eval=TRUE}
    corpusTw <- corpus(twitter)  # build the corpus
    ```
    part2
    ```{r trigrams, warning=FALSE, cache=TRUE, message=FALSE, eval=FALSE}
    dfmTw <- dfm(corpusTw, verbose=TRUE, removeTwitter=TRUE, ngrams=3)
    ```
    

    As a side effect, this also could save you some headaches in terms of reproducibility :)

    0 讨论(0)
  • 2020-11-22 02:05

    Ensure you record your work in a reproducible script. From time-to-time, reopen R, then source() your script. You'll clean out anything you're no longer using, and as an added benefit will have tested your code.

    0 讨论(0)
  • 2020-11-22 02:06

    To further illustrate the common strategy of frequent restarts, we can use littler which allows us to run simple expressions directly from the command-line. Here is an example I sometimes use to time different BLAS for a simple crossprod.

     r -e'N<-3*10^3; M<-matrix(rnorm(N*N),ncol=N); print(system.time(crossprod(M)))'
    

    Likewise,

     r -lMatrix -e'example(spMatrix)'
    

    loads the Matrix package (via the --packages | -l switch) and runs the examples of the spMatrix function. As r always starts 'fresh', this method is also a good test during package development.

    Last but not least r also work great for automated batch mode in scripts using the '#!/usr/bin/r' shebang-header. Rscript is an alternative where littler is unavailable (e.g. on Windows).

    0 讨论(0)
提交回复
热议问题