Don’t Forget the “utils” Package in R

Articles From:


Blogger,, and Senior Data Scientist

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.csvwrite.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.

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.


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.

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

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

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.



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)

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)


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”:


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 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("", "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:


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.


That’s it for this post! Please check out my other R posts by clicking here

Originally posted on blog.

Leave a Reply

Note that all comments are held for moderation before publishing.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from and is being posted with permission from The views expressed in this material are solely those of the author and/or and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.