Tag Archives: R

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"
[13] "NTUSER.DAT"
[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))
[1]

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))
[0]

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"
[12] "NTUSER.DAT"
[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

Environments and how to apply them

Adventures with R

I’ve been learning a lot these days. It’s been coming fast and heavy and I have not been able to document much of it. One of the areas I kind of focused on was Environments. I’ve glanced at (but not finished) the chapter Hadley Wickham wrote in his book ‘Advanced R’. One thing that stood out clearly was that environments are data structures just like any other in the language. I will pause there for now.

What I rushed out to post is something I just saw in a post in R Bloggers. The main gist was about loading .RData files safely and this is one area where environments can be put to good use.

Rather than loading the objects from that file into the Workspace (or global environment), just create a new environment with new.env() and load the saved objects into into it by passing it as…

View original post 86 more words

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.packages("devtools")

library(devtools)

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

find_rtools()

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.

Cheers!

Leave a comment

Filed under Computers & Internet

How I created an R function (for the first time)

calculator-scientific

I know you will probably laugh at me when you read this, especially if you’re a techie, but recently I took my R growth to a new level by creating a proper function. Actually I was experimenting a little with the apply family of functions when it occurred to me that I should attempt to build a function for sapply

First I created a vector of random numbers and created a (probably) meaningless mathematical function

# Create a vector "vec" of random numbers
# and a mathematical function "funny_no"
(vec  <-  round(rnorm(1200, mean = 16, sd = 2)))
funny_no  <-  function(x) sqrt(x)/2 + 3*log10(x) 

 

When I ran it, it worked very well. So I thought to myself “What if I wanted to prevent this function from accepting negative numbers? Well, after a little tinkering, I came up with this:

# Add conditional statements to "funny_no"
funny_no  <-  function(x) {
    if (x>=0) {
      sqrt(x)/2 +3*log10(x)
   }
    else {
      stop("Cannot use negative numbers")
   }
}

And when I ran the following lines of code I saw it was purr-fect!

vec <- round(rnorm(1200, mean = 16, sd = 2))
sapply(vec, funny_no) 

# The next line inserts negative values and function
# throws an error saying "Cannot use negative numbers"
vec[vec == 15]  <-  -15
sapply(vec, funny_no)

So there you have it. Time for some coffee!

Leave a comment

Filed under Computers & Internet

Tales from the R Side

Credit: 'R' by Lorenzo Lorenzi (1772-1850)

Credit: ‘R’ by Lorenzo Lorenzi (1772-1850)

It appears there’s been a little lull on this page and it’s been like that for a good reason. I suddenly found myself caught smack in the middle of doing a course on C++, trying to make progress as I understudy Hadley Wickham via his book, Advanced R, and a Data Science course from Columbia University. I really didn’t have much of a holiday!

Why I thought this was worth sharing is because I am hoping – really hoping – that some of my colleagues and compatriots would consider channelling some of their energies in this direction. I remember vividly how, in the late 90s, I tried to convince fellow doctors to join me in attending FREE computer classes, all to no avail. So, to see the health sector lagging behind in the application of ICTs in my country is not at all surprising. I don’t know, but I have this feeling that this area of knowledge – data science – is going to be very important in the next 5 – 10 years.

First, we’re in the middle of a ‘data boom’. Societies are now literally inundated with data and humans practically littering the data-sphere with their numbers – the proverbial 1s and 0s. This is something we cannot ignore, whether we are job-seekers or entrepreneurs.

Secondly, whether you like it or not, somebody somewhere is taking your data, storing it and using it for something. The prospect of not being able to swim when the world is certain to be flooded, is indeed a grim one.

Thirdly, some of us have this affinity for numbers but don’t really know how to translate this into something practical, something real! Well, welcome to the age of ‘data products’, where you’re either buying or you’re selling. Period.

There are a few more things to say on this but I’m yet to wrap my mind around it. Frankly, the whole thing is dizzying and the speed at which the world is going with this is scary. Many of us should be determined not to be left behind.

So, while I’m schlepping C++ syntax, or trying to figure out the rules that govern R functions (lazy evaluation really stumped me for a bit), I can already see some interesting times ahead of us.

Yes, I think I should go and watch a movie now…

Leave a comment

Filed under Computers & Internet