If you find any typos, errors, or places where the text may be improved, please let us know by providing feedback either in the feedback survey (given during class) or by using GitLab.
- For a quick reference to the functions and tools taught in this course, check out our cheatsheet!
Are technical issues with your computer preventing you from completing the pre-course tasks or doing the course sessions?
A last-ditch solution may be to use the RStudio Cloud platform to do the course. What is it?
RStudio Cloud is a cloud computing service hosted by RStudio. It runs RStudio in your web browser with full functionality, including built-in integration of R Projects and Git. It looks slightly different from a local RStudio session, and it treats R Projects and Git a little different, but the overall R user experience is virtually identical. For what we need in this course, we are fine with their free service, which only requires a little work to register and set up.
Setting up RStudio Cloud
- Register a free account: Go to the RStudio Cloud website and complete the sign-up process for a free account.
- Log in to the account. After logging in, we start on the “Personal Workspace” page. Here we can create new R Projects (which we’ll do next), set up additional workspaces (which we don’t need for this course) or read the R guides and cheatsheets available in the “Learn” section of the menu panel (we can explore those after the course, there’s a lot of good content):
- Start a new R Project: Click the “New Project” button. To practice using it, click the right half of the button and you get the option to create a project by cloning a GitHub repository (more on that later):
- Throttle CPU and memory usage down: For this course, we don’t need a lot of computer power, so we can get by with just the minimum memory and CPU settings. With these settings, we get 30 hours of run-time per month, which should be plenty for this course. To do so, click the gear-icon in the top right of the screen, then the “Resources” tab, toggle both sliders to the far left, and click “Apply Changes”:
- Set up access to GitHub: Finally, we need to allow access to private GitHub repositories, which we’ll be using in this course. Click on your user name in the top-right corner, and then the “Authentication” tab. This will take us to the authentication options, where we tick both boxes at the bottom for enabling GitHub, including the one labeled “Private repo access also enabled”:
- Setting up Git on RStudio Cloud projects: Git cannot be set up inside the project using packages, so prodigenr and
usethis::use_git()will not work. However, it can easily be done through the RStudio menu interface:
Tools -> Version Control -> Project Setup -> Version Control System: Git
- Now you’re good to go! You should have no issues completing the pre-course tasks and have a course experience similar to those using a local installation.
- There are just a few more differences between local and RStudio Cloud sessions to be aware of:
Uploading files to RStudio Cloud projects: To make files available to RStudio Cloud projects, we have to upload the files to it. Thankfully, this is very easy! In RStudio Cloud, the “Files” tab of Panel D (Figure A.1) contains a new icon “Upload File”, which allows us to easily upload a file from our local computer (or several files within a zip file) to the RStudio Cloud folder we’re currently in:
R projects and folder structure in RStudio Cloud: Since we start every R session inside of a project on RStudio Cloud, RStudio in the cloud session doesn’t let us set up projects from inside of the R session. The prodigenr package can still be used through the console to set up the basic folder structure of our projects. However, the root folder of any project is fixed to
/cloud/project/, so when setting up our folder structure with prodigenr, we should have this in mind (e.g.
prodigenr::setup_project("LearningR")creates the prodigenr folders inside
/cloud/project/LearningRand avoids unnecessarily long file paths).
- Cloning a GitHub remote repository in RStudio Cloud: Can be done when setting up a new project in the RStudio Cloud workspace (“New Project from Git Repo”), or from the terminal, but it cannot be done through the RStudio main menu interface.
- Uploading files to RStudio Cloud projects: To make files available to RStudio Cloud projects, we have to upload the files to it. Thankfully, this is very easy! In RStudio Cloud, the “Files” tab of Panel D (Figure A.1) contains a new icon “Upload File”, which allows us to easily upload a file from our local computer (or several files within a zip file) to the RStudio Cloud folder we’re currently in:
Block quotes are used when you want to emphasize a block of text,
usually for quoting someone.
You create a block quote by putting a
> at the beginning of the line,
and as with the lists and headers,
it needs empty lines before and after the text.
So it looks like this:
> Block quote
Footnotes are added by enclosing a number or word in square brackets (
and beginning with an uptick (
^). It looks like this:
[^1] or this[^note]. Footnote [^1]: Footnote content [^note]: Another footnote
Now, if you scroll down to the bottom of the page, you will see these footnotes.
While you can insert tables using Markdown too,
it isn’t recommended to do that for complicated or large tables.
Tables are created by separating columns with
with the table header being separated by a line that looks like
A simple example is:
| | Fun | Serious | |:--|----:|--------:| | **Happy** | 1234 | 5678 | | **Sad** | 123 | 456 |
|:---| tell the table to left-align or right-align the values
in the column. Center-align is
So you can probably imagine,
doing this for larger or even slightly more complicated tables is not practical.
A good alternative approach is to create the table in a spreadsheet,
importing that table into R within a code chunk,
knitr::kable() to create the table after that.
You can modify ggplot2 colour schemes using many other pre-defined palettes by installing new R packages, including scientific journal colour palettes (ggsci) and even a Wes Anderson (wesanderson) or a Stubio Ghibli (ghibli) colour palette! Also check out the Data Visualization book in the Resources
chapter for more information and learning on visualizing data.
Free online books:
- R for Data Science: Excellent open and online resource for using R for data analysis and data science.
- Fundamentals of Data Visualization: Excellent online resource for using ggplot2 and R graphics. The book mostly focuses on concepts and theory of how to visualize, rather than the practicalities (i.e. no coding involved).
- ModernDive: Statistical Inference via Data Science: Great book on using statistics and data science methods in R.
- Happy Git and GitHub for the useR (highly recommended): Specifically useful is the chapter on Daily Workflows using Git.
- Data Visualization: A practical introduction: A book that goes into practical as well as conceptual detail on how and why to make certain graphs, given your data.
- Online book for R Markdown: The go-to reference for learning and using R Markdown.
- Course material for a statistics class: Excellent course material for teaching statistics and R.
- RStudio cheatsheets: Multiple, high-quality cheatsheets you can print off to use as a handy reference.
- Tidyverse style guide: To learn about how to write well-styled code in R.
- Good enough practicies in scientific computing: An article listing and describing some practices to use when writing code.
- Best practices in scientific computing.
- Case study of reproducible methodds in Bioinformatics: (Kim, Poline, and Dumas 2018).
- Organizing R Source Code.
- Hands-on tutorial for learning Git, in a web-based terminal.
- Simpler, first-steps guide to using Git.
- RStudio tutorial on using R Markdown.
- Markdown syntax guide.
- Pandoc Markdown Manual (R Markdown uses pandoc).
- Adding citations in R Markdown.
Interactive sites or resources for hands-on learning:
- Video on using Git in RStudio.
- StackOverflow for tidyr.
- StackOverflow for dplyr.
- StackOverflow for ggplot2.
- Tip: Combine auto-completion with
::to find new functions and documentation on the functions (e.g. try typing
base::and then hitting Tab to show a list of all functions found in base R).
|bookdown||Authoring Books and Technical Documents with R Markdown||Output formats and utilities for authoring books and technical documents with R Markdown.|
|broom||Convert Statistical Objects into Tidy Tibbles||Summarizes key information about statistical objects in tidy tibbles. This makes it easy to report results, create plots and consistently work with large numbers of models at once. Broom provides three verbs that each provide different types of information about a model. tidy() summarizes information about model components such as coefficients of a regression. glance() reports information about an entire model, such as goodness of fit measures like AIC and BIC. augment() adds information about individual observations to a dataset, such as fitted values or influence measures.|
||Fast aggregation of large data (e.g. 100GB in RAM), fast ordered joins, fast add/modify/delete of columns by group using no copies at all, list columns, friendly and fast character-separated-value read/write. Offers a natural and flexible syntax, for faster development.|
|datapasta||R Tools for Data Copy-Pasta||RStudio addins and R functions that make copy-pasting vectors and tables to text painless.|
|dplyr||A Grammar of Data Manipulation||A fast, consistent tool for working with data frame like objects, both in memory and out of memory.|
|forcats||Tools for Working with Categorical Variables (Factors)||Helpers for reordering factor levels (including moving specified levels to front, ordering by first appearance, reversing, and randomly shuffling), and tools for modifying factor levels (including collapsing rare levels into other, ‘anonymising’, and manually ‘recoding’).|
|fs||Cross-Platform File System Operations Based on ‘libuv’||A cross-platform interface to file system operations, built on top of the ‘libuv’ C library.|
|ggplot2||Create Elegant Data Visualisations Using the Grammar of Graphics||A system for ‘declaratively’ creating graphics, based on "The Grammar of Graphics". You provide the data, tell ‘ggplot2’ how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details.|
|glue||Interpreted String Literals||An implementation of interpreted string literals, inspired by Python’s Literal String Interpolation <https://www.python.org/dev/peps/pep-0498/>; and Docstrings <https://www.python.org/dev/peps/pep-0257/>; and Julia’s Triple-Quoted String Literals <https://docs.julialang.org/en/v1.3/manual/strings/#Triple-Quoted-String-Literals-1>;.|
|googledrive||An Interface to Google Drive||Manage Google Drive files from R.|
|haven||Import and Export ‘SPSS’, ‘Stata’ and ‘SAS’ Files||Import foreign statistical formats into R via the embedded ‘ReadStat’ C library, <https://github.com/WizardMac/ReadStat>;.|
|here||A Simpler Way to Find Your Files||Constructs paths to your project’s files. The ‘here()’ function uses a reasonable heuristics to find your project’s files, based on the current working directory at the time when the package is loaded. Use it as a drop-in replacement for ‘file.path()’, it will always locate the files relative to your project root.|
|janitor||Simple Tools for Examining and Cleaning Dirty Data||The main janitor functions can: perfectly format data.frame column names; provide quick counts of variable combinations (i.e., frequency tables and crosstabs); and isolate duplicate records. Other janitor functions nicely format the tabulation results. These tabulate-and-report functions approximate popular features of SPSS and Microsoft Excel. This package follows the principles of the "tidyverse" and works well with the pipe function %>%. janitor was built with beginning-to-intermediate R users in mind and is optimized for user-friendliness. Advanced R users can already do everything covered here, but with janitor they can do it faster and save their thinking for the fun stuff.|
|knitr||A General-Purpose Package for Dynamic Report Generation in R||Provides a general-purpose tool for dynamic report generation in R using Literate Programming techniques.|
|lubridate||Make Dealing with Dates a Little Easier||Functions to work with date-times and time-spans: fast and user friendly parsing of date-time data, extraction and updating of components of a date-time (years, months, days, hours, minutes, and seconds), algebraic manipulation on date-time and time-span objects. The ‘lubridate’ package has a consistent and memorable syntax that makes working with dates easy and fun. Parts of the ‘CCTZ’ source code, released under the Apache 2.0 License, are included in this package. See <https://github.com/google/cctz>; for more details.|
|patchwork||The Composer of Plots||The ‘ggplot2’ package provides a strong API for sequentially building up a plot, but does not concern itself with composition of multiple plots. ‘patchwork’ is a package that expands the API to allow for arbitrarily complex composition of plots by, among others, providing mathematical operators for combining multiple plots. Other packages that try to address this need (but with a different approach) are ‘gridExtra’ and ‘cowplot’.|
|purrr||Functional Programming Tools||A complete and consistent functional programming toolkit for R.|
|readr||Read Rectangular Text Data||The goal of ‘readr’ is to provide a fast and friendly way to read rectangular data (like ‘csv’, ‘tsv’, and ‘fwf’). It is designed to flexibly parse many types of data found in the wild, while still cleanly failing when data unexpectedly changes.|
|readxl||Read Excel Files||Import excel files into R. Supports ‘.xls’ via the embedded ‘libxls’ C library <https://github.com/libxls/libxls>; and ‘.xlsx’ via the embedded ‘RapidXML’ C++ library <http://rapidxml.sourceforge.net>;. Works on Windows, Mac and Linux without external dependencies.|
|rio||A Swiss-Army Knife for Data I/O||Streamlined data import and export by making assumptions that the user is probably willing to make: ‘import()’ and ‘export()’ determine the data structure from the file extension, reasonable defaults are used for data import and export (e.g., ‘stringsAsFactors=FALSE’), web-based import is natively supported (including from SSL/HTTPS), compressed files can be read directly without explicit decompression, and fast import packages are used where appropriate. An additional convenience function, ‘convert()’, provides a simple method for converting between file types.|
|rmarkdown||Dynamic Documents for R||Convert R Markdown documents into a variety of formats.|
|stringr||Simple, Consistent Wrappers for Common String Operations||A consistent, simple and easy to use set of wrappers around the fantastic ‘stringi’ package. All function and argument names (and positions) are consistent, all functions deal with "NA"’s and zero length vectors in the same way, and the output from one function is easy to feed into the input of another.|
|tibble||Simple Data Frames||Provides a ‘tbl_df’ class (the ‘tibble’) that provides stricter checking and better formatting than the traditional data frame.|
|tidyr||Tidy Messy Data||Tools to help to create tidy data, where each column is a variable, each row is an observation, and each cell contains a single value. ‘tidyr’ contains tools for changing the shape (pivoting) and hierarchy (nesting and ‘unnesting’) of a dataset, turning deeply nested lists into rectangular data frames (‘rectangling’), and extracting values out of string columns. It also includes tools for working with missing values (both implicit and explicit).|
|tidyverse||Easily Install and Load the ‘Tidyverse’||The ‘tidyverse’ is a set of packages that work in harmony because they share common data representations and ‘API’ design. This package is designed to make it easy to install and load multiple ‘tidyverse’ packages in a single step. Learn more about the ‘tidyverse’ at <https://tidyverse.org>;.|
|usethis||Automate Package and Project Setup||Automate package and project setup tasks that are otherwise performed manually. This includes setting up unit testing, test coverage, continuous integration, Git, ‘GitHub’, licenses, ‘Rcpp’, ‘RStudio’ projects, and more.|
|vroom||Read and Write Rectangular Text Data Quickly||The goal of ‘vroom’ is to read and write data (like ‘csv’, ‘tsv’ and ‘fwf’) quickly. When reading it uses a quick initial indexing step, then reads the values lazily , so only the data you actually use needs to be read. The writer formats the data in parallel and writes to disk asynchronously from formatting.|
Q: What do I do if I don’t have admin rights and can’t install the newest versions of programs?
- A: Depending on your institutional policies and infrastructure, you may not be able to download newer versions of R and RStudio. Firstly, you should contact your IT support person to see whether you are able to access an updated version. While you could attempt to use the older version of these programs available to you, you may notice that some things either look quite different or not work at all. One solution is to use the RStudio Cloud platform, as per the instructions in the section Setting up RStudio Cloud.
Q: Why did my output have so much red text when I tried to install packages?
- A: Rest assured that this is simply the way that R informs you that packages are being installed, even though it may look scary. If there really was an error, the output would likely begin with a message saying “Error: …” or “Warning: …”, to differentiate between informational compared to error messages.
Q: Why did I receive a bunch of error messages when I tried to install packages?
- A: For some Windows users, you may need to install Rtools.
Q: Help! My code isn’t working, even though I’ve followed all the instructions.