Getting Started with Positron
You can never look at the data too much. – Mark Engerman
The world confronts us. Make decisions we must.
Installing R and Positron
We use R via Positron. R is to Positron as a car’s engine is to its dashboard.
More precisely, R is a programming language that runs computations, while Positron is an integrated development environment (IDE) that provides an interface with many convenient features. Just as having access to a speedometer, rearview mirrors, and a navigation system makes driving much easier, using Positron’s interface makes using R much easier.
Download and install R and Positron on your computer.
Download and install R. If you are using a Mac, make sure to use the correct installation, depending on whether you are using newer Apple silicon “M1-3 Macs” (first option) or older “Intel Macs” (second option). You can look up the chip used in your Mac by checking “About This Mac” under the Apple menu.
If you are using Windows, you must also install RTools. Download the latest version from here. Currently, the Rtools44 installer is what you want. The link is in the seventh paragraph of this page.
Using R and Positron
Much as we don’t drive a car by interacting directly with the engine but rather by interacting with elements on the car’s dashboard, we won’t be using R directly. Instead, we will work through Positron’s interface. After you install R and Positron on your computer, you’ll have two new programs (also called applications) you can open. Always work in Positron and not directly in the R application.
Open up Positron.
Look at the Console tab in the Panel at the lower left. At start up, the Console gives you some information about your version of R. The Console is where you can type and run R code. For example, if you type 1 + 1
and hit Enter
, the Console returns 2.
We often use the phrase “run the following code.” This means that you should type or copy-and-paste the code into the Console and then hit the Enter
key. (Note that Return
is the name of a Mac key and Enter
the name of the corresponding Windows key. We use Enter
throughout the Primer.)
The right side of the Positron window is the Secondary Side Bar.
In the Session tab, there is a Variables listing fpor the current R session. There are no variables yet. Let’s change that. Go to your Console and type:
x <- 5
This code assigns the value 5 to an object, x
. <-
is the operator used to assign values to objects in R. Now, hit Enter
and you should see a variable x
equal to 5 in under Variables. You must always hit Enter
after typing a command, otherwise Positron will not realize that you want R to execute the command.
The Secomdary Side Bar is on the right side of the Positron window. By default, it contains different tabs in the top half and a display area for plots in the bottom half.
In the Console, type plot(1:10)
and hit Enter
.
The far left of the Positron window is the Activity Bar. The area in which the Welcome screen is currently located is the Editor. Once you start writing code, the Editor is the location in which your files will appear.
Package installation
R packages, also known as libraries, extend the power of R by providing additional functions and data.
R is like a new phone. While it has a certain amount of features when you use it for the first time, it doesn’t have everything. R packages are like the apps you download onto your phone.
Consider an analogy to Instagram. If you have a new phone and want to share a photo with friends. You need to:
- Install the app: Since your phone is new and does not include the Instagram app, you need to download the app. You do this only once. (You might need to do this again in the future when there is an update to the app.)
- Open the app: After you’ve installed Instagram, you need to open it. You need to do this every time you use the app.
The process is very similar for an R package. You need to:
Install the package: This is like installing an app on your phone. Most packages are not installed by default when you put R and Positron on your computer. Thus if you want to use a package for the first time, you need to install it. Once you’ve installed a package, you likely won’t install it again unless you want to update it to a newer version.
Load the package: “Loading” a package is like opening an app on your phone. Packages are not loaded by default when you start Positron. You need to load each package you want to use every time you restart Positron.
Just for this chapter, execute this command in your R Console. Do not do this if you are working in the cloud using, for example, Posit Workbench.
options("pkgType" = "binary")
This helps to ensure that package installation will go more smoothly.
Let’s install a useful package. At the Console within Positron, type:
install.packages("remotes")
And press Enter
on your keyboard. You must include the quotation marks around the name of the package. A package can depend on other packages, which will be automatically installed if needed.
One tricky aspect of this process is that R will occasionally ask you:
Do you want to install from sources the packages which
need compilation? (Yes/no/cancel)
Unless you have a good reason not to, always answer “no” to this question.
R packages generally live in one of two places:
CRAN (rhymes with “clan”) for more mature, popular packages. Use
install.packages()
, as you did above.Github for more experimental, less stable packages. Use
remotes::install_github()
.
Package loading
After you install a package, you need to “load” it by using the library()
command. To load the remotes package, run the following code in the Console.
After running this code, a blinking cursor should appear next to the >
symbol. (The >
is the “prompt.”) This means you were successful and the remotes package is now loaded and ready to use. However, you might get a red “error message” which reads:
Error in library(remotes) : there is no package called ‘remotes’
This error message means that you haven’t successfully installed the package. If you get this error message, make sure to install the remotes package before proceeding.
For historical reasons, packages are also known as libraries, which is why the relevant command for loading them is library()
.
R will occasionally ask you if you want to install some packages. You almost always want to, otherwise R would not be asking you.
Let’s install another package:
install.packages("tutorial.helpers")
This package provides infrastructure for doing tutorials, a common approach for learning R.
Let’s install another package:
install.packages("tidyverse")
This might take awhile since the “Tidyverse” includes so many useful packages.
Package use
You have to load each package you want to use every time you start Positron. If you don’t load a package before attempting to use one of its features, you will see an error message like:
Error: could not find function
This is a different error message than the one you just saw about a package not having been installed yet. R is telling you that you are trying to use a function in a package that has not yet been loaded. R doesn’t know where to “find” the function you want to use.
Let’s install two other packages: r4ds.tutorials and primer.tutorials. Copy and paste the following to the R Console:
library(remotes)
install_github("PPBDS/r4ds.tutorials")
This should install easily. There is a version of r4ds.tutorials available on CRAN, but we want to get the latest version, often called the “development” version.
Next, copy and paste the following to the R Console:
remotes::install_github("PPBDS/primer.tutorials")
Instead of loading the remotes package explicitly, we used the double colon notation — ::
— to refer directly to the install_github()
function which resides in the remotes package.
If the download fails, try
options("timeout" = 600)
This gives the download more time to complete. Try it again.
Some other new packages will be installed as well, most importantly primer.data, which includes most of the data we use in the Primer. It may take a few minutes. If something gets messed up, it is often useful to read the error messages and see if a specific package is at fault. If so, use the remove.packages()
function to remove the problematic package and then install it again.
If primer.tutorials does not install correctly, don’t worry about it. This package is much less important than r4ds.tutorials.
Tutorials
There are many tutorials available in the r4ds.tutorials and primer.tutorials packages. In order to access these tutorials, you should run library(r4ds.tutorials)
or library(primer.tutorials)
in the R Console.
Because Positron is still evolving, there is no easy way to start a tutorial. Instead, we must issue a command like this from the Console:
learnr::run_tutorial("tutorials-in-positron", package = "tutorial.helpers")
You may notice that the Console will show output as the tutorial is starting up. This is because Positron is running the code to create the tutorial.
Your work will automatically be saved between Positron sessions. You can complete the tutorial in multiple sittings.
Complete the “Tutorials in Positron” tutorial from the tutorial.helpers package.
Once you have done so, or if you already know R, you can start to learn about data science.
Summary
You should have done the following:
Installed the latest versions of R and Positron.
Installed, from CRAN, the remotes, tutorial.helpers and tidyverse packages:
# Only run this options() line if NOT on Cloud platform
# options("pkgType" = "binary").
install.packages("remotes")
install.packages("tidyverse")
install.packages("tutorial.helpers")
- Installed, from Github, the r4ds.tutorials and primer.tutorials packages:
remotes::install_github("PPBDS/r4ds.tutorials")
remotes::install_github("PPBDS/primer.tutorials")
- Completed the “Tutorials in Positron” tutorial from the tutorial.helpers package.
Let’s get started.