Don’t forget the “utils” package in R

[This article was first published on R – Open Source Automation, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

utils package in R


With thousands of powerful packages, it’s easy to glaze over the libraries that come preinstalled with R. Thus, this post will talk about some of the cool functions in the utils package, which comes with a standard installation of R. While utils comes with several familiar functions, like read.csv, write.csv, and help, it also contains over 200 other functions.

readClipboard and writeClipboard

One of my favorite duo of functions from utils is readCLipboard and writeClipboard. If you’re doing some manipulation to get a quick answer between R and Excel, these functions can come in handy. readClipboard reads in whatever is currently on the Clipboard.

For example, let’s copy a column of cells from Excel.

readClipboard in R from Excel

We can now run readClipboard() in R. The result of running this command is a vector containing the column of cells we just copied. Each cell corresponds to an element in the vector.

readClipboard result in R

Similarly, if we want to write a vector of elements to the clipboard, we can the writeClipboard command:


test <- c("write", "to", "clipboard")

writeClipboard(test)

Now, the vector test has been copied to the clipboard. If you paste the result in Excel, you’ll see a column of cells corresponding to the vector you just copied.

writeClipboard result in R

combn

The combn function is useful for getting the possible combinations of an input vector. For instance, let’s say we want to get all of the possible 2-element combinations of a vector, we could do this:


food <- c("apple", "grape", "orange", "pear", "peach", "banana")

combn(food, 2)

combn get all combinations of a vector's elements in R

In general, the first parameter of combn is the vector of elements you want to get possible combinations from. The second parameter is the number of elements you want in each combination. So if you need to get all possible 3-element or 4-element combinations, you would just need to change this number to three or four.


combn(food, 3)

combn(food, 4)

We can also add a parameter called simplify to make the function return a list of each combination, rather than giving back a matrix output like above.


combn(food, 3, simplify = FALSE)

fileSnapshot

The fileSnapshot function is one R’s collection of file manipulation functions. To learn more about file manipulation and getting information on files in R, check out this post.

fileSnapshot will list and provide details about the files in a directory. This function returns a list of objects.


# get file snapshot of current directory
snapshot <- fileSnapshot()

# or file snapshot of another directory
snapshot <- fileSnapshot("C:/some/other/directory")

fileSnapshot returns a list, which here we will just call “snapshot”. The most useful piece of information can be garnered from this by referencing “info”:


snapshot$info

Here, snapshot$info is a data frame showing information about the files in the input folder parameter. Its headers include:

  • size ==> size of file
  • isdir ==> is file a directory? ==> TRUE or FALSE
  • mode ==> the file permissions in octal
  • mtime ==> last modified time stamp
  • ctime ==> time stamp created
  • atime ==> time stamp last accessed
  • exe ==> type of executable (or “no” if not an executable)
  • download.file

    download.file does just what it sounds like – downloads a file from the internet to the destination provided in the function’s input. The first parameter is the URL of the file you wish to download. The second parameter is the name you want to give to the downloaded file. Below, we download a file and call it “census_data.csv”.

    
    download.file("https://www2.census.gov/programs-surveys/popest/datasets/2010/2010-eval-estimates/cc-est2010-alldata.csv", "census_data.csv")
    
    

    How to modify an object on the fly with the “fix” function

    The utils package also has the ability to modify objects on the fly with the fix function. For instance, let’s say you define a function interactively, and you want to make some modification.

    
    some_func <- function(num)
    {
        3 * num + 1
    
    }
    
    

    Now, let’s modify the function with fix:

    
    fix(some_func)
    
    

    fix function in r

    When you call fix, it comes up with an editor allowing you to modify the definition of the function. You can also call fix to modify a vector or data frame.

    
    fix(iris)
    
    

    fix data frame in r

    That’s it for this post! Please check out my other R posts by clicking here. Also, if you live in the NYC area and are interested in in-person open source coding workshops, please check out my meetup here.

    The post Don’t forget the “utils” package in R appeared first on Open Source Automation.

    To leave a comment for the author, please follow the link and comment on their blog: R – Open Source Automation.

    R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

    Never miss an update!
    Subscribe to R-bloggers to receive
    e-mails with the latest R posts.
    (You will not see this message again.)

    Click here to close (This popup will not appear again)