Tag Archives: coding

Quick Tip on Deleting Directories in R

When trying to delete a directory, one can encounter some unexpected problems. The function for carrying out this operation is unlink, which accepts the director name as its first argument; other arguments are recursive (a logical vector or length 1 indicating whether we want to delete subdirectories, and force, also logical, which tries to override file permissions in most cases. It returns 0 when successful and 1 when not.

But there is a gotcha to using the function. First let’s list the contents of the HOME directory

> list.files()
 [1] "3D Objects"
 [2] "AppData"
 [3] "Contacts"
 [4] "Desktop"
 [5] "Documents"
 [6] "Downloads"
 [7] "Favorites"
 [8] "IntelGraphicsProfiles"
 [9] "Links"
[10] "MicrosoftEdgeBackups"
[11] "Music"
[12] "New folder"
[14] "ntuser.dat.LOG1"
[15] "ntuser.dat.LOG2"
[16] "NTUSER.DAT{a70b1724-6bc8-11e8-a408-d0bf9c58c5d2}.TM.blf"
[17] "NTUSER.DAT{a70b1724-6bc8-11e8-a408-d0bf9c58c5d2}.TMContainer00000000000000000001.regtrans-ms"
[18] "NTUSER.DAT{a70b1724-6bc8-11e8-a408-d0bf9c58c5d2}.TMContainer00000000000000000002.regtrans-ms"
[19] "ntuser.ini"
[20] "OneDrive"
[21] "Pictures"
[22] "R"
[23] "Saved Games"
[24] "Searches"
[25] "source"
[26] "Videos"

Let’s say we want to delete the ‘New folder’ directory

> (unlink('New folder/', recursive = TRUE, force = TRUE))

It fails!

Even when you study the help file, the source of this failure is not apparent.

Well, it turns out that the function does not recognize the trailing slash that indicates that we are dealing with a directory. This is always added when you use tab completion for the directory name.

So, when we type

# Remove trailing slash in directory name
> (unlink('New folder', recursive = TRUE, force = TRUE))

The function succeeds, as evidenced by listing the directory contents

> dir()
[1] "3D Objects"
[2] "AppData"
[3] "Contacts"
[4] "Desktop"
[5] "Documents"
[6] "Downloads"
[7] "Favorites"
[8] "IntelGraphicsProfiles"
[9] "Links"
[10] "MicrosoftEdgeBackups"
[11] "Music"
[13] "ntuser.dat.LOG1"
[14] "ntuser.dat.LOG2"
[15] "NTUSER.DAT{a70b1724-6bc8-11e8-a408-d0bf9c58c5d2}.TM.blf"
[16] "NTUSER.DAT{a70b1724-6bc8-11e8-a408-d0bf9c58c5d2}.TMContainer00000000000000000001.regtrans-ms"
[17] "NTUSER.DAT{a70b1724-6bc8-11e8-a408-d0bf9c58c5d2}.TMContainer00000000000000000002.regtrans-ms"
[18] "ntuser.ini"
[19] "OneDrive"
[20] "Pictures"
[21] "R"
[22] "Saved Games"
[23] "Searches"
[24] "source"
[25] "Videos"

Watch out for this!

Leave a comment

Filed under Computers & Internet

R Packages: Solving a problem using devtools in Windows

In the introduction to his book R packages, Hadley Wickham provides a neat function for making sure that everything is set for writing your own R extensionsby simply running the devtools::has_devel(), which, if all goes well, should evaluate to TRUE.

This did not work out for me and I had to fix this problem on 2 different occasions so I felt I need to share this info in case there are others also stumped by this hurdle.

The fix I found – after a full sweaty day – was in this conversation on GitHub and I would like to break it down very quickly:

  1. Make sure you have installed Rtools from CRAN
  2. Make sure that Rtools/bin as well as Rtools/MinGW/x64/ are added to your system PATH (if you don’t know how, click here)
  3. In addition, it is recommended that you install LATEX (the link is also found on the Rtools page mentioned on No. 1)
  4. Run the following lines of code



install_github("hadley/devtools")    # to get the latest 'pre-CRAN' package updates


has_devel()    # output should be TRUE

Like I said, I had this problem on 2 different machines (Windows 7 and 10) and the same fix worked on both of them.


Leave a comment

Filed under Computers & Internet