Learning Objectives
- Describe the purpose of RStudio’s script, console, environment, and file/plot/help windows.
- Create an R project.
- Organize files and directories for a set of analyses as an R Project.
- Use the built-in RStudio help interface to search for more information on R functions.
Jupyter (formerly IPython), is a multi-lingual REPL.
Azure Notebooks is a free service that provides Jupyter notebooks along with supporting packages for R, Python and F# as a service. This means you can just login and get going since no installation/setup is necessary.
What can you do with a Jupyter Notebooks
Our end goal is not just to “do stuff” but to do it in a way that anyone can easily and exactly replicate our workflow and results. For more information or help on using a Jupyter notebook http://nbviewer.jupyter.org/github/ipython/ipython/blob/3.x/examples/Notebook/Index.ipynb.
You will need to have created a Microsoft account to use Azure notebooks if you did not already please go here https://notebooks.azure.com/ and signup for an account.
Now you have Jupyter notebook to interact with for todays workshop.
There are three main ways of interacting with R: using the R-shell, Jupyter notebooks or by using script files (plain text files that contain your code).
The R shell is the place where R is waiting for you to tell it what to do, and where it will show the results of a command. You can type commands directly into the shell, but they will be forgotten when you close the session. It is better to enter the commands in a Jypter notebook or script file, and save the script. This way, you have a complete record of what you did, you can easily show others how you did it and you can do it again later on if needed.
If R shell is ready to accept commands, the R console shows a >
prompt. If it
receives a command (by typing, copy-pasting or sent from the script editor using
Ctrl-Enter
), R will try to execute it, and when ready, show the results and
come back with a new >
-prompt to wait for new commands.
If R is still waiting for you to enter more data because it isn’t complete yet,
the console will show a +
prompt. It means that you haven’t finished entering
a complete command. This is because you have not ‘closed’ a parenthesis or
quotation.
R is a versatile, open source programming/scripting language that’s useful both for statistics but also data science. Inspired by the programming language S.
You should separate the original data (raw data) from intermediate datasets that
you may create for the need of a particular analysis. For instance, you may want
to create a data/
directory within your working directory that stores the raw
data, and have a data_output/
directory for intermediate datasets and a
figure_output/
directory for the plots you will generate.
For Azure Notebooks all data uploaded is with the notebook so there is no folder structure - other implementations of Jupyter notebooks might access a file system.
If you need help with a specific function, let’s say barplot()
, you can type:
?barplot
If you just need to remind yourself of the names of the arguments, you can use:
args(lm)
If the function is part of a package that is installed on your computer but don’t remember which one, you can type:
??geom_point
If you are looking for a function to do a particular task, you can use
help.search()
(but only looks through the installed packages):
help.search("kruskal")
If you can’t find what you are looking for, you can use the rdocumention.org website that search through the help files across all packages available.
Start by googling the error message. However, this doesn’t always work very well because often, package developers rely on the error catching provided by R. You end up with general error messages that might not be very helpful to diagnose a problem (e.g. “subscript out of bounds”).
However, you should check stackoverflow. Search using the [r]
tag. Most
questions have already been answered, but the challenge is to use the right
words in the search to find the answers:
http://stackoverflow.com/questions/tagged/r
The Introduction to R can also be dense for people with little programming experience but it is a good place to understand the underpinnings of the R language.
The R FAQ is dense and technical but it is full of useful information.
The key to get help from someone is for them to grasp your problem rapidly. You should make it as easy as possible to pinpoint where the issue might be.
Try to use the correct words to describe your problem. For instance, a package is not the same thing as a library. Most people will understand what you meant, but others have really strong feelings about the difference in meaning. The key point is that it can make things confusing for people trying to help you. Be as precise as possible when describing your problem
If possible, try to reduce what doesn’t work to a simple reproducible
example. If you can reproduce the problem using a very small data.frame
instead of your 50,000 rows and 10,000 columns one, provide the small one with
the description of your problem. When appropriate, try to generalize what you
are doing so even people who are not in your field can understand the question.
To share an object with someone else, if it’s relatively small, you can use the
function dput()
. It will output R code that can be used to recreate the exact same
object as the one in memory:
dput(head(iris)) # iris is an example data.frame that comes with R
If the object is larger, provide either the raw file (i.e., your CSV file) with
your script up to the point of the error (and after removing everything that is
not relevant to your issue). Alternatively, in particular if your questions is
not related to a data.frame
, you can save any R object to a file:
saveRDS(iris, file="/tmp/iris.rds")
The content of this file is however not human readable and cannot be posted directly on stackoverflow. It can however be sent to someone by email who can read it with this command:
some_data <- readRDS(file="~/Downloads/iris.rds")
Last, but certainly not least, always include the output of sessionInfo()
as it provides critical information about your platform, the versions of R and
the packages that you are using, and other information that can be very helpful
to understand your problem.
sessionInfo()
packageDescription("name-of-package")
. You may also want
to try to email the author of the package directly.