title: quit and restart a clean R session from within R (Windows 7, RGui 64-bit)
body: <p>I am trying to quit and restart R from within R.  The reason for this is that my job takes up a lot of memory, and none of the common options for cleaning R's workspace reclaim RAM taken up by R. <code>gc()</code>, <code>closeAllConnections()</code>, <code>rm(list = ls(all = TRUE))</code> clear the workspace, but when I examine the processes in the Windows Task Manager, R's usage of RAM remains the same.  The memory is reclaimed when R session is restarted. </p>
  
  <p>I have tried the suggestion from this post: </p>
  
  <p><a href="https://stackoverflow.com/questions/6313079/quit-and-restart-a-clean-r-session-from-within-r">Quit and restart a clean R session from within R?</a></p>
  
  <p>but it doesn't work on my machine.  It closes R, but doesn't open it again.  I am running R x64 3.0.2 through RGui (64-bit) on Windows 7. Perhaps it is just a simple tweak of the first line in the above post: </p>
  
  <pre><code>makeActiveBinding("refresh", function() { shell("Rgui"); q("no") }, .GlobalEnv)
  </code></pre>
  
  <p>but I am unsure how it needs to be changed. </p>
  
  <p>Here is the code.  It is not fully reproducible, because a large list of files is needed that are read in and scraped.  What eats memory is the <code>scrape.func()</code>; everything else is pretty small. In the code, I apply the scrape function to all files in one folder.  Eventually, I would like to apply to a set of folders, each with a large number of files (~ 12,000 per folder; 50+ folders).  Doing so at present is impossible, since R runs out of memory pretty quickly. </p>
  
  <pre><code>library(XML)
  library(R.utils)
  
  ## define scraper function
  scrape.func &lt;- function(file.name){
    require(XML)
  
    ## read in (zipped) html file
    txt &lt;- readLines(gunzip(file.name))
  
    ## parse html
    doc &lt;- htmlTreeParse(txt,  useInternalNodes = TRUE)
  
    ## extract information
    top.data &lt;- xpathSApply(doc, "//td[@valign='top']", xmlValue)
    id &lt;- top.data[which(top.data=="I.D.:") + 1]
    pub.date &lt;- top.data[which(top.data=="Data publicarii:") + 1]
    doc.type &lt;- top.data[which(top.data=="Tipul documentului:") + 1]
  
    ## tie into dataframe
    df &lt;- data.frame(
      id, pub.date, doc.type, stringsAsFactors=F)
    return(df)
    # clean up
    closeAllConnections()
    rm(txt)
    rm(top.data)
    rm(doc)
    gc()
  }
  
  ## where to store the scraped data
  file.create("/extract.top.data.2008.1.csv")
  
  ## extract the list of files from the target folder
  write(list.files(path = "/2008/01"), 
        file = "/list.files.2008.1.txt")
  
  ## count the number of files
  length.list &lt;- length(readLines("/list.files.2008.1.txt"))
  length.list &lt;- length.list - 1
  
  ## read in filename by filename and scrape
  for (i in 0:length.list){
    ## read in line by line
    line &lt;- scan("/list.files.2008.1.txt", '', 
                 skip = i, nlines = 1, sep = '\n', quiet = TRUE)
    ## catch the full path 
    filename &lt;- paste0("/2008/01/", as.character(line))
    ## scrape
    data &lt;- scrape.func(filename)
    ## append output to results file
    write.table(data,file = /extract.top.data.2008.1.csv", 
                append = TRUE, sep = ",", col.names = FALSE)
    ## rezip the html
    filename2 &lt;- sub(".gz","",filename)
    gzip(filename2)
  }
  </code></pre>
  
  <p>Many thanks in advance,
  Marko</p>