You are currently browsing the category archive for the ‘Packages’ category.
Packrat is now available on CRAN, with version 0.4.1-1! Packrat is an R package that helps you manage your project’s R package dependencies in an isolated, reproducible and portable way.
Install packrat from CRAN with:
In particular, this release provides better support for local repositories. Local repositories are just folders containing package sources (currently as folders themselves).
One can now specify local repositories on a per-project basis by using:
packrat::set_opts(local.repos = <pathToRepo>)
and then using
to install that package from the local repository.
There is also experimental support for a global ‘cache’ of packages, which can be used across projects. If you wish to enable this feature, you can use (note that it is disabled by default):
packrat::set_opts(use.cache = TRUE)
in each project where you would utilize the cache.
By doing so, if one project installs or uses e.g. Shiny 0.10.1 for CRAN, and another version uses that same package, packrat will look up the installed version of that package in the cache — this should greatly speed up project initialization for new projects that use projects overlapping with other packrat projects with large, overlapping dependency chains.
In addition, this release provides a number of usability improvements and bug fixes for Windows.
Please visit rstudio.github.io/packrat for more information and a guide to getting started with Packrat.
httr 0.5 is now available on CRAN. The httr packages makes it easy to talk to web APIs from R. Learn more in the quick start vignette.
This release is mostly bug fixes and minor improvements, but there is one major new feature: you can now save response bodies directly to disk.
library(httr) # Download the latest version of rstudio for windows url <- "http://download1.rstudio.org/RStudio-0.98.1049.exe" GET(url, write_disk(basename(url)), progress())
There is also some preliminary support for HTTP caching (see
rerequest()). See the release notes for complete details.
R Markdown is a framework for writing versatile, reproducible reports from R. With R Markdown, you write a simple plain text report and then render it to create polished output. You can:
- Transform your file into a pdf, html, or Microsoft Word document—even a slideshow—at the click of a button.
- Embed R code into your report. When you render the file, R will run the code and insert its results into your report. Use this feature to add graphs and tables to your report: if your data ever changes, you can update your figures by re-rendering the report.
- Make interactive documents and slideshows. Your report becomes interactive when you embed Shiny code.
Shiny v0.10.1 has been released to CRAN. You can either install it from a CRAN mirror, or update it if you have installed a previous version.
install.packages('shiny', repos = 'http://cran.rstudio.com') # or update your installed packages # update.packages(ask = FALSE, repos = 'http://cran.rstudio.com')
The most prominent change in this patch release is that we added full Unicode support on Windows. Shiny apps running on Windows must use the UTF-8 encoding for ui.R and server.R (also the optional global.R, README.md, and DESCRIPTION) if they contain non-ASCII characters. See this article for details and examples: http://shiny.rstudio.com/articles/unicode.html
Please note although we require UTF-8 for the app components, UTF-8 is not a general requirement for any other files. If you read/write text files in an app, you are free to use any encoding you want, e.g. you can
readLines('foo.txt', encoding = 'Windows-1252'). The article above has explained it in detail.
Other changes include:
runGitHub()also allows the
'username/repo'syntax now, which is equivalent to
runGitHub('repo', 'username'). (#427)
navbarPage()now accepts a
windowTitleparameter to set the web browser page title to something other than the title displayed in the navbar.
- Added an
inline = TRUE, these outputs will be put in
span()instead of the default
div(). This occurs automatically when these outputs are created via the inline expressions (e.g.
`r renderText(expr)`) in R Markdown documents. See an R Markdown example at http://shiny.rstudio.com/gallery/inline-output.html (#512)
- Added support for option groups in the select/selectize inputs. When the
selectizeInput()is a list of sub-lists and any sub-list is of length greater than 1, the HTML tag
<optgroup>will be used. See an example at here (#542)
httr 0.4 is now available on CRAN. The httr packages makes it easy to talk to web APIs from R.
cookies()functions to extract headers and cookies from responses.
status_code()returns HTTP status codes.
PATCH()) now have an
encodeargument that determine how the
bodyis encoded. Valid values are “multipart”, “form” or “json”, and the
multipartargument is now deprecated.
GET(..., progress())will display a progress bar, useful if you’re doing large uploads or downloads.
verbose()gives you considerably more control over degree of verbosity, and defaults have been selected to be more helpful for the most common cases.
queryparameters are now dropped automatically.
There are number of other minor improvements and bug fixes, as described by the release notes.
I’ve released four new data packages to CRAN: babynames, fueleconomy, nasaweather and nycflights13. The goal of these packages is to provide some interesting, and relatively large, datasets to demonstrate various data analysis challenges in R. The package source code (on github, linked above) is fully reproducible so that you can see some data tidying in action, or make your own modifications to the data.
Below, I’ve listed the primary dataset found in each package. Most packages also include a number of supplementary datasets that provide additional information. Check out the docs for more details.
babynames::babynames: US baby name data for each year from 1880 to 2013, the number of children of each sex given each name. All names used 5 or more times are included. 1,792,091 rows, 5 columns (year, sex, name, n, prop). (Source: Social security administration).
fueleconomy::vehicles: Fuel economy data for all cars sold in the US from 1984 to 2015. 33,442 rows, 12 variables. (Source: Environmental protection agency)
nasaweather::atmos: Data from the 2006 ASA data expo. Contains monthly atmospheric measurements from Jan 1995 to Dec 2000 on 24 x 24 grid over Central America. 41,472 observations, 11 variables. (Source: ASA data expo)
nycflights13::flights: This package contains information about all flights that departed from NYC (i.e., EWR, JFK and LGA) in 2013: 336,776 flights with 16 variables. To help understand what causes delays, it also includes a number of other useful datasets:
airlines. (Source: Bureau of transportation statistics)
NB: since the datasets are large, I’ve tagged each data frame with the
tbl_df class. If you don’t use dplyr, this has no effect. If you do use dplyr, this ensures that you won’t accidentally print thousands of rows of data. Instead, you’ll just see the first 10 rows and as many columns as will fit on screen. This makes interactive exploration much easier.
library(dplyr) library(nycflights13) flights #> Source: local data frame [336,776 x 16] #> #> year month day dep_time dep_delay arr_time arr_delay carrier tailnum #> 1 2013 1 1 517 2 830 11 UA N14228 #> 2 2013 1 1 533 4 850 20 UA N24211 #> 3 2013 1 1 542 2 923 33 AA N619AA #> 4 2013 1 1 544 -1 1004 -18 B6 N804JB #> 5 2013 1 1 554 -6 812 -25 DL N668DN #> 6 2013 1 1 554 -4 740 12 UA N39463 #> 7 2013 1 1 555 -5 913 19 B6 N516JB #> 8 2013 1 1 557 -3 709 -14 EV N829AS #> 9 2013 1 1 557 -3 838 -8 B6 N593JB #> 10 2013 1 1 558 -2 753 8 AA N3ALAA #> .. ... ... ... ... ... ... ... ... ... #> Variables not shown: flight (int), origin (chr), dest (chr), air_time #> (dbl), distance (dbl), hour (dbl), minute (dbl)
We’re excited to announce a new release of Packrat, a tool for making R projects more isolated and reproducible by managing their package dependencies.
This release brings a number of exciting features to Packrat that significantly improve the user experience:
- Automatic snapshots ensure that new packages installed in your project library are automatically tracked by Packrat.
- Bundle and share your projects with packrat::bundle() and packrat::unbundle() — whether you want to freeze an analysis, or exchange it for collaboration with colleagues.
- Packrat mode can now be turned on and off at will, allowing you to navigate between different Packrat projects in a single R session. Use packrat::on() to activate Packrat in the current directory, and packrat::off() to turn it off.
- Local repositories (ie, directories containing R package sources) can now be specified for projects, allowing local source packages to be used in a Packrat project alongside CRAN, BioConductor and GitHub packages (see this and more with ?"packrat-options").
You can install the latest version of Packrat from GitHub with:
Packrat will be coming to CRAN soon as well.
If you try it, we’d love to get your feedback. Leave a comment here or post in the packrat-discuss Google group.
tidyr is new package that makes it easy to “tidy” your data. Tidy data is data that’s easy to work with: it’s easy to munge (with dplyr), visualise (with ggplot2 or ggvis) and model (with R’s hundreds of modelling packages). The two most important properties of tidy data are:
- Each column is a variable.
- Each row is an observation.
Arranging your data in this way makes it easier to work with because you have a consistent way of referring to variables (as column names) and observations (as row indices). When use tidy data and tidy tools, you spend less time worrying about how to feed the output from one function into the input of another, and more time answering your questions about the data.
To tidy messy data, you first identify the variables in your dataset, then use the tools provided by tidyr to move them into columns. tidyr provides three main functions for tidying your messy data:
gather() takes multiple columns, and gathers them into key-value pairs: it makes “wide” data longer. Other names for gather include melt (reshape2), pivot (spreadsheets) and fold (databases). Here’s an example how you might use
gather() on a made-up dataset. In this experiment we’ve given three people two different drugs and recorded their heart rate:
library(tidyr) library(dplyr) messy <- data.frame( name = c("Wilbur", "Petunia", "Gregory"), a = c(67, 80, 64), b = c(56, 90, 50) ) messy #> name a b #> 1 Wilbur 67 56 #> 2 Petunia 80 90 #> 3 Gregory 64 50
We have three variables (name, drug and heartrate), but only name is currently in a column. We use
gather() to gather the a and b columns into key-value pairs of drug and heartrate:
messy %>% gather(drug, heartrate, a:b) #> name drug heartrate #> 1 Wilbur a 67 #> 2 Petunia a 80 #> 3 Gregory a 64 #> 4 Wilbur b 56 #> 5 Petunia b 90 #> 6 Gregory b 50
Sometimes two variables are clumped together in one column.
separate() allows you to tease them apart (
extract() works similarly but uses regexp groups instead of a splitting pattern or position). Take this example from stackoverflow (modified slightly for brevity). We have some measurements of how much time people spend on their phones, measured at two locations (work and home), at two times. Each person has been randomly assigned to either treatment or control.
set.seed(10) messy <- data.frame( id = 1:4, trt = sample(rep(c('control', 'treatment'), each = 2)), work.T1 = runif(4), home.T1 = runif(4), work.T2 = runif(4), home.T2 = runif(4) )
To tidy this data, we first use
gather() to turn columns
home.T2 into a key-value pair of key and time. (Only the first eight rows are shown to save space.)
tidier <- messy %>% gather(key, time, -id, -trt) tidier %>% head(8) #> id trt key time #> 1 1 treatment work.T1 0.08514 #> 2 2 control work.T1 0.22544 #> 3 3 treatment work.T1 0.27453 #> 4 4 control work.T1 0.27231 #> 5 1 treatment home.T1 0.61583 #> 6 2 control home.T1 0.42967 #> 7 3 treatment home.T1 0.65166 #> 8 4 control home.T1 0.56774
Next we use
separate() to split the key into location and time, using a regular expression to describe the character that separates them.
tidy <- tidier %>% separate(key, into = c("location", "time"), sep = "\\.") tidy %>% head(8) #> id trt location time time #> 1 1 treatment work T1 0.08514 #> 2 2 control work T1 0.22544 #> 3 3 treatment work T1 0.27453 #> 4 4 control work T1 0.27231 #> 5 1 treatment home T1 0.61583 #> 6 2 control home T1 0.42967 #> 7 3 treatment home T1 0.65166 #> 8 4 control home T1 0.56774
The last tool,
spread(), takes two columns (a key-value pair) and spreads them in to multiple columns, making “long” data wider. Spread is known by other names in other places: it’s cast in reshape2, unpivot in spreadsheets and unfold in databases.
spread() is used when you have variables that form rows instead of columns. You need
spread() less frequently than
separate() so to learn more, check out the documentation and the demos.
Just as reshape2 did less than reshape, tidyr does less than reshape2. It’s designed specifically for tidying data, not general reshaping. In particular, existing methods only work for data frames, and tidyr never aggregates. This makes each function in tidyr simpler: each function does one thing well. For more complicated operations you can string together multiple simple tidyr and dplyr functions with
You can learn more about the underlying principles in my tidy data paper. To see more examples of data tidying, read the vignette,
vignette("tidy-data"), or check out the demos,
demo(package = "tidyr"). Alternatively, check out some of the great stackoverflow answers that use tidyr. Keep up-to-date with development at http://github.com/hadley/tidyr, report bugs at http://github.com/hadley/tidyr/issues and get help with data manipulation challenges at https://groups.google.com/group/manipulatr. If you ask a question specifically about tidyr on stackoverflow, please tag it with tidyr and I’ll make sure to read it.
The RStudio team recently rolled out new capabilities in RStudio, shiny, ggvis, dplyr, knitr, R Markdown, and packrat. The “Essential Tools for Data Science with R” free webinar series is the perfect place to learn more about the power of these R packages from the authors themselves.
Click to learn more and register for one or more webinar sessions. You must register for each separately. If you miss a live webinar or want to review them, recorded versions will be available to registrants within 30 days.
The Grammar and Graphics of Data Science
Live! Wednesday, July 30 at 11am Eastern Time US Click to register
- dplyr: a grammar of data manipulation – Hadley Wickham
- ggvis: Interactive graphics in R – Winston Chang
Live! Wednesday, August 13 at 11am Eastern Time US Click to register
- The Next Generation of R Markdown – Jeff Allen
- Knitr Ninja – Yihui Xie
- Packrat – A Dependency Management System for R – J.J. Allaire & Kevin Ushey
Live! Wednesday, September 3 at 11am Eastern Time US Click to register
- Embedding Shiny Apps in R Markdown documents – Garrett Grolemund
- Shiny: R made interactive – Joe Cheng
Our first public release of ggvis, version 0.3, is now available on CRAN. What is ggvis? It’s a new package for data visualization. Like ggplot2, it is built on concepts from the grammar of graphics, but it also adds interactivity, a new data pipeline, and it renders in a web browser. Our goal is to make an interface that’s flexible, so that you can compose new kinds of visualizations, yet simple, so that it’s accessible to all R users.
Update: there was an issue affecting interactive plots in version 0.3. Version 0.3.0.1 fixes the issue. The updated source package is now on CRAN, and Windows and Mac binary packages will be available shortly.
ggvis integrates with Shiny, so you can use dynamic, interactive ggvis graphics in Shiny applications. We hope that the combination of ggvis and Shiny will make it easy for you to create applications for interactive data exploration and presentation. ggvis plots are inherently reactive and they render in the browser, so they can take advantage of the capabilities provided by modern web browsers. You can use Shiny’s interactive components for interactivity as well as more direct forms of interaction with the plot, such as hovering, clicking, and brushing.
And don’t worry — ggvis isn’t only meant to be used with Shiny and interactive documents. Because the RStudio IDE is also a web browser, ggvis plots can display in the IDE, like any other R graphics:
There’s much more to come with ggvis. To learn more, visit the ggvis website.
Please note that ggvis is still young, and lacks a number of important features from ggplot2. But we’re working hard on ggvis and expect many improvements in the months to come.