The R vs. Python language war is dead. This is an observation from Strata + Hadoop World San Jose 2016. There were no discussion among participants about the merits of one over the other. Nor was there any content about which is better in any of the sessions that I attended. In a show of acceptance of using either language for Data Science, a full-day tutorial was held for each of the two languages.

What has instead emerged is acceptance that Python is the more general purpose of the two while now also being well suited for Data Science. And that R is the statistical-domain specific of the two while also being well suited for Data Science.

What’s emerged is that the technical challenges underlying integration of these languages into Big Data are essentially the same. A key post by software engineer Wes McKinney discusses the the commonality. It’s an important post. Read it here.

The language war is dead. A takeaway is that it’s not one or the other but both. Data Scientists will need to know both. Being more fluent in Python is better. Having enough facility in R to get data into and out of the R ecosystem, being able to use and interpret results from statistical tests, and being able to use the visualization libraries, is probably enough.

Incidentally, the search interest in R is stable now for the last three years:

In a team work environment, most coding tasks performed in R use the same set of R packages. In the environment at the company where I work, there are approximately 50 R packages we use. This post discusses how all the analysts and data scientists on a team can have access to the same library of packages while also eliminating the work related to keeping packages current.

What is the problem

Loading and maintaining packages takes time, is sometimes inconvenient due to dependencies, and will result in different versions of packages in development workflows. Another problem is that the package versions used in the development workflow might be different than the versions of packages that will eventually be used when the code is put into operation or scheduled to run under a service account on cron.

A Solution

Add to your path the set of packages that are installed and maintained on the service account, (service_account). If you want a package that isn’t loaded in the service_account library, say, for example, packages for a GIS or mapping-related project, install it in your user library at first. If your code is later put into operation, the package can be added to the service_account library and made available to your code and subsequently made available and to the code of all other developers on the team.

How to use the service_account library of packages

Put the service_account library into your path. There are two ways.

1) Manual way that you have to do each time you start RStudio Server –

Execute the commands shown in this image. The first and last calls to .libPaths() are for demonstration only. You will only need to enter the second occurrence, the occurrence with the vector argument. Do this each time you start a session. You can also do this in the middle of a coding session.libpaths-to-use-service-account-libraryI just happen to be using an environment where we have Revolution Analytics Enterprise version 7.4 installed. If you’re using CRAN R 3.1.3 (or any other CRAN R version), the path name in element [3] will be different.

2) Automatic way by adding the .libPaths() to your .Rprofile file –


The above image shows my .Rprofile file. This file is in: /home/{user-alias}. This file is not automatically created as part of the R installation process or any other process. You have to create it. To create it, type the nano .Rprofile command at the command line when you are in your home directory. Add the .libPaths() command as shown, save the file, exit the editor, and restart your R session.

Note that nano is an editor. You can also use vi/vim. The nano editor is available to all users and I recommend it over vi/vim.

What to watch out for

If you install some packages in your library that are also installed in the service_account path, the packages will show twice in your list of installed packages. If you manually load packages using the GUI (by clicking the button to install the package in RStudio Server), it is easy to select package the version of the package you don’t want. But, you can decide what to do. You can have two different versions installed. A great use case for this is for testing changes in packages that your application might be sensitive to. It’s easy to load and unload packages and test code execution in this way when in interactive mode.

R loads the function from the first place that it finds it. In this post, the libraries in service_account are at the front of the path. You could change your path so that packages are searched in order of your library, service_account, then the R system library.

Key takeaway

Use by analysts and data scientists of the library of packages installed for the service account is an easy, low-cost way to improve team workflow and productivity. Doing so also eliminates the possibility of introducing package version-related bugs into code when putting code into operation.

Posted in R.

The iframe tag and the WordPress iframe plugin are how to include HTML widgets in a WordPress blog post. The iframe tag is included in HTML5. Using iframes in a WordPress blog post is how you get the JavaScript associated with a widget to run in your post.

Here’s an example of how to create an HTML widget in R using an .R file. This could also be done in an .Rmd and knitted. Since I didn’t want to create an .html page from within RStudio using knitr, I just sourced the .R file:

a <- datatable(iris)
saveWidget(a, "datatable-iris-example.html")

Next, import the .html file to your media library. Then, add the shortcode to your post. Here's how to encode it in the page when editing the blog post:

iframe seamless src="" width="100%" height="500"

Note that I had to remove the open [ and closing ] brackets to avoid the code from being executed. When you add your shortcode to your post, add back the [ as the first character and the ] as the last character in the shortcode. This is how simple it is. Here's how it looks:

I am not aware of any other way to get R-created HTML widgets (JavaScript) to execute in a blog post. If you have any suggestions, please post a comment!

The functions in the R readr package load text data into R much faster than the load functions included in the R Utils Package. Adopting the use of the functions in this package will improve the workflow and productivity of the solo analyst and for whole teams of analysts. This post provides an example of how to read .csv files using a function from the package, compares the time cost of one of these functions to a customary way of reading text files, and shows an example of the warnings and problems the package’s functions produce.

(Edit: I just re-ran the code in the below gist again while finishing up this post. The user time for this execution of read_delim() was only 17 seconds.)


The data used in this comparison was distributed over 24 files. The total number of data records was 3,020,761. Total size of the data was 1.2GB. A customary way to read data into a R data structure is to use read.table():

A customary method of reading files using read_table()
A customary method of reading files using read_table()

This reads the data into a data frame. This approach and the system time is shown in the adjacent image. The user time to read these 24 files using read.table() was 267 seconds. This was the baseline that the read function in the readr package was compared to.

R readr package code example and results

The readr package has different functions. (The link to the RStudio blog post announcing the package is included at the end of this post. Check out their announcement for a more complete discussion of the package” functions and features.) I used just one of these in my example. Here’s R code that uses read_delim(). This code can be used to read any number of .csv files that are in a directory. The regular expression will cause all files ending in .csv to be included in the processing:

The user time to read this same set of data using the function read_delim() was about 29 seconds:

Reading .csv files using read_delim()
Reading .csv files using read_delim()

Warnings and problems

It’s a talkative package. An aspect of using the package that I did not expect was its use of warnings. This section displays some of the messages and warnings I encountered during the development of my solution above and how I investigated the warnings. Here is the first indication that a possible problem was encountered:

Example of a warning message when reading a file with R package readr
Example of warning message when reading a file with R package readr

Typing the statement warnings() at the console produced this listing:

The warning messages from an execution of a function in readr package
Warning messages from an execution of read_delim()

Here are some of the specifics. Notice the syntax. I loaded my data into a list of data frames. To look at the problems for the just one of the files, use problems() with the object name as a parameter passed in during the call:

The first 25, possible problems displayed to console
The first 25 problems displayed to console

I don’t recommend ignoring the warnings and problems. If you have to operationalize code, and your process repeats each day, for example, but with fresh data, and maybe you are able to consume data that is curated and saved in an EDW, it’s probably ok to not raise an alert and fully research problems. But, it depends. For one-off and ad hoc data analysis, it’s worth spending the time review the warnings and messages. I mention this in comparison to the customary method of reading data using read.table() or similar functions. Now, analysts have three tools to make sure their data is properly loaded…visual inspection, summaries and explorations, and now these warnings and problem listings.

Key takeaway

Hardly any training time is required to be able to use and understand the functions in the readr package. In the example above, using a function from the package to read a set of files was approximately an order of magnitude faster than reading the same set of files using a customary method. This is a big win for workflow and analyst productivity.

The announcement for the package and more information on how to use the other functions and features is located on the RStudio blog.

This post describes how to use the R dplyr package to calculate percentages. A data set from the U.S Census Bureau was used. Three tests check that the calculation using dplyr was accurate. The code incorporate the pipe operator %>% that was introduced into R in 2014 via the magrittr package.

The general question is: What is the percentage a measure value associated with an instance of a sub group of data represents of the sum total of the measure values of all instances within the same sub group? This general question statement is accurate but not easy to understand. An example in SQL makes it clear. This statement computes the percentage of an individual’s salary over the total salary within his department:

Select depname, empno, salary, salary/sum(salary) over (partition by depname) from empsalary

It’s the same as a windows function in SQL.

Calculation in dplyr

Here’s the code:

Census data set

The data set is publicly available; the URL is listed in the read.table() on line 10 of the gist. I selected this data set because it has characteristics that made it useful; it contains data that can be grouped, it contains a calculation that is already available in the data set that I was able to compare my results to. Additionally, I didn’t want Puerto Rico in the results so was able to use the filter() feature to remove Puerto Rico prior to executing the calculation. The aim was to make sure I knew how to use dplyr.


Here’s a screen shot of the results for region 4, the West:

R dplyr calculate percentage
Region 4 of result data set

The field I calculated was the pct18Plus, the right-most column in above table. (This is a screen shot of the result and the column header isn’t included.)

The result for Wyoming is interesting. The percentage calculated is so small because the population of Wyoming is small compared to population of California, not because the 18+ population in Wyoming is a small percentage of Wyoming’s population.

Key takeaway

The R dplyr package just works. It’s easier than using base R to complete the same tasks. It’s not arcane. It’s elegant to read and use, and it’s decreased my development time.