Base R is an extremely powerful tool. However…
The syntax editor in Base R is just a simple text editor.
Rstudio is relatively more efficient and easy to use.
There are actually many options:
RStudio offers:
RStudio offers:
RStudio is also an excellent tool for reproducible research.
Everyone who uses RStudio benefits from easier scripting:
It even makes working at the console viable.
Knowing a few shortcuts can save a lot of time in the long run.
Examples (Windows and Linux):
Run current line: Ctrl+Entr
Copy up/down: Ctrl+Shft+up/down
Move up/down: Shft+up/down
Run everything: Ctrl+Shft+Entr
Insert section: Ctrl+Shft+R
Select Window: Ctrl+1:9
Multicursor: Ctrl + Alt + select
(Copy) Move lines: (shift+) alt + arrow
Clear console: Ctrl + L
Restart R: Ctrl + Shft + F10
Scripting/Console Window: Ctrl+1/Ctrl+2
Rerun previous: Ctrl + Shft + P
Run everything before: Ctrl + Alt + B
Run everything before: Ctrl + Alt + E
Knit: Ctrl + Shft + K
Knowing just a dozen shortcuts could save a lot of time.
You can customize them however you want.
Mac users: most of these would use Cmd and Option rather than Ctrl and Alt
Snippets allow one to insert code of a certain form for commonly used functions.
You only have to type the first couple letters, the form of the rest of the code will fill out, then you can tab your way through the rest of it.
Good for:
RStudio will note problems in your code in the margin.
This works beyond just R scripts too!
Customize various aspects of how RStudio
Can apply to all RStudio sessions or for a particular project.
Start with the looks:
While you may certainly want to change things such as the look, to not save the workspace automatically etc., the main point is simply to be aware of what you can change.
Projects provide a self-contained ecosystem to work in.
You can easily jump between multiple projects.
File/New Project
We’ll talk about version control later.
All tabs opened will remain open when you revisit the project.
You can have multiple projects running at the same time
Help you get more organized.
Help you get more reproducible.
R is fast becoming a general programming environment
Markdown is a markup language.
Now one can intermingle R with markdown, html, css, JavaScript, \(\LaTeX\) and others resulting in a variety of products.
Rstudio and Rmarkdown make it easy to construct:
Literate programming!
Reproducible research!
R chunks are interspersed throughout the Rmd file.
Rmarkdown documents combine:
Once ready, knit the document into the format your want.
An Rmarkdown workshop will be given with more details.
Notebooks provide a different way to interact with R.
Previously one could save out some files as a ‘notebook’.
Now one can start an interactive R script.
Output is visible at the point where the code is written.
File/New File/R Noteboook…
You may still want to use (or prefer) the console
The viewer will allow for previews of the final document.
Notebooks are a different flavor of Rmarkdown document.
Good for sharing your work.
See also, Jupyter.
Along with the Plots pane, RStudio provides a Viewer pane.
Anything interactive will be displayed there.
ggplot2 is the most widely used package for visualization in R.
However, it does not produce interactive plots by nature.
Many packages use various Javascript libraries to provide interactive graphics.
Many utilize these packages via the htmlwidgets R package.
Some packages to note:
Works in your presentations too.
Shiny is a framework that can essentially allow you to build an interactive website.
Most of the more recently developed visualization packages will work specifically within the shiny and rmarkdown settings.
RStudio lets you take a deeper look at your data more easily.
Accessible interactivity.
Interactive graphics are more fun too!
Just a couple visualization packages can go a very long way.
One can create functions that can be used within RStudio with a click or keystroke.
These special functions are called addins.
Increase your productivity and efficiency when scripting.
They can be anything.
Addins are in some sense nothing more than R functions that you can call interactively.
likeR()
I like R!
However, they can possibly do quite a bit.
Debugging is merely finding and fixing problematic code.
Debugging is an essential part of creating functions.
If you are doing anything more than twice, write a function!
Debugging can allow one to spot issues
RStudio can even help you get started transforming existing code to a function
There are numerous facilities within R to help you debug your code.
Break Points
browser
debug
debugonce
traceback
RStudio makes the process pretty easy.
There are commands that allow you to work through debugging:
Next (n)/Return: runs the next line
Step into (s): if the next line is a new function, it enters into the function
Careful with this one; you can get pretty far into other functions
Finish (f): finishes the function
Continue (c)
: stops debugging and runs the function
Stop (Q): stops debugging and does not run the function
Each of these also has a button in the debugging menu
Code profiling allows one to see what parts of the code take most of the processing time and resources (memory)
Like debugging, there have always been tools in base R for this, but RStudio makes it easy to profile any code.
Furthermore, it doesn’t have to be an explicit function.
Debugging and profiling are important parts of advanced programming.
One should desire to make code as general and reproducible as possible.
RStudio makes the process more interactive and flexible.
RStudio makes package development easier too.
“Create package based on source files”
RStudio will automatically start you out with the following:
DESCRIPTION: Just like every R package
A ‘man’ folder: Contains .Rd files for each function
An ‘R’ folder: Contains your functions.
The roxygen2 package helps to properly format your documentation files.
After you have all of your files ready, build the package.
Packages tend to have a lot happening in them.
To help you make sure that the package has everything it needs, run the check function from devtools on it.
It will check package quality across many dimensions:
RStudio has tools that make package creation a straight-forward process.
You should not be afraid to create your own packages
RStudio can integrate version control into your project.
At its most basic, it is just a way to manage changes.
Especially useful when collaborating.
Git works on a distributed model
Tools that use Git to share code on the web
Public vs. private repositories
GitHub is a web-based hosting services for Git repositories.
Also a social Network for software and other developers.
RStudio makes it easy to commit, push, pull, revert, etc.
If you need other things, you can access the Git shell directly
RStudio can integrate version control into your projects.
You have nothing to lose by keeping track of changes to files.
This is especially useful when collaborating.
RStudio wants everything to be easy for us as R users.
They provide a series of cheat sheets as reference material.
RStudio even has a cheatsheet for using RStudio!
Provides a high-level overview for many of the things discussed here.
Data Visualization
Data Wrangling
R Markdown
Package Development
Shiny
RStudio can enhance and accelerate your R experience.
Not just for scripting!
It can serve you from the beginning of a project to whatever ends you take it.
*Special thanks to Seth Berry @ Notre Dame for loads of help with the content!