Installing Software
Introduction
The goal is to create the capability on one’s computer to conduct and communicate reproducible data analysis using modern, open-source, and current software tools.
Learning Outcomes: After completion, students can create a functioning environment for literate programming by installing:
- Base R
- The RStudio Integrated Development Environment (IDE)
- Select R “packages” of software
References:
- RStudio IDE Cheat Sheet. (May 2024)
- Quarto Guide
A.1 Install the Software
A.1.1 Install Base R
R is free open-source software capable of operating on most computers.
- To install R, use your browser to go to the Comprehensive R Archive Network (CRAN) site: https://cran.r-project.org/
- Choose the appropriate operating system.
- If asked, follow the instructions to “choose your preferred
CRAN mirror
”. - Select a site close to your location, e.g., USA Hoobly Classifieds, Pittsburgh, PA, https://cran.mirrors.hoobly.com/
- If asked, follow the instructions to “choose your preferred
- Download the latest version for your computer operating system (OS), e.g. R version 4.4.1 2024-06-14, Race for Your Life) (or higher).
- For Macs, download the install file and install as usual for a new application.
- If Windows, choose Base and download the file. For old systems you may need to follow the installation instructions.
- You should now be able to find an R application with the logo on your computer.
- We will not use that interface (except for rare troubleshooting).
- We will use Posit’s much more user-friendly and capable RStudio Desktop Integrated Development Environment (IDE). It is also free.
A.1.2 Install the Free RStudio Desktop
Posit’s RStudio Desktop is a free highly capable Integrated Development Environment (IDE) designed to greatly increase the ease and efficiency of programming in R.
- Go to the RStudio download site at: https://posit.co/download/rstudio-desktop/
- Click on
Download RStudio Desktop ...
. It may auto-detect your type of computer.- If asked, get the free desktop version (not the pro).
- Your version should be at least Version: 2024.04.2 build 764 (June 2024) or higher.
- Install as usual for your computer.
- You should now see an Application for RStudio IDE on your computer.
- Add the RStudio Application to your Mac Dock or Windows Task Bar.
A.1.2.1 Quarto
Quarto is a multi-language capability from Posit for creating many types of documents, web sites, presentations etc.. Quarto has many new features and can handle older R Markdown (.Rmd) files.
- See Tutorial: Hello, Quarto! and select the RStudio option.
Recent RStudio versions already include the Command Line Interface (CLI) version of Quarto so it should now be installed on your computer.
- If for some reason you need to re-install Quarto, go to Quarto’s Get Started page or install the {quarto} package.
A.1.2.2 \(\LaTeX\)
\(\LaTeX\) is a high-quality typesetting system designed for the production of technical and scientific documents that is used in many communities.
You will use \(\LaTeX\) code to create mathematical formulas as required in the text portions of your files.
Quarto comes with the American Mathematical Society (AMS) version of \(\LaTeX\).
A.2 Install the R package for R Markdown
Quarto depends on the R Markdown language for describing how to format the output from your documents so you must install the R {rmarkdown} package.
- The {rmarkdown} package is a bundle of software (think of it as an add-in or app) that adds the ability to use the R Markdown language to format your documents.
You can use RStudio to install the {rmarkdown} from CRAN.
Open RStudio by clicking on in your dock or task bar.
It should look something like this :
Go to the RStudio
Packages
tab pane, select theInstall
tab, enterrmarkdown
and select `Install.- This will generate the command in the Console pane to install the package.
You can install using the the RStudio
Console Pane
.The blinking cursor at the bottom left, to the right of the “greater-than” or “right arrow
>
, is called the prompt.You can type in R code after the prompt and then hit the enter (return) to execute the code. - If you make a mistake, use the up-arrow key to cycle through previous commands till you get the one you want so you can correct it.
Type
install.packages("rmarkdown")
to the right of the cursor and then hit enter or return tell RStudio to execute the command.
- R is case-sensitive so be careful about spelling.
- R is also sensitive about quotes so use the
"
or'
when required. - If you copy and paste from a document, e.g., a PDF document, into R, the copied code may use “Smart Quotes” which are not the same as the “Straight Quotes” used in R. If there is an error, you may need to edit the file and replace the smart quotes with straight quotes.
A.2.1 Check the Software Installations.
Open RStudio
- You should see the version of R in the console pane.
You can create a new file using the New Files
button or the File/New File
on the top main menu.
- Click on the plus sign and select
Quarto document
. - RStudio creates a new file with a few lines of default code with the extension
.qmd
and opens it as a tab in theSource
pane.- The
Source
pane is where you edit your documents (source code) files.
- The
- To convert your
.qmd
file to the specified output format, click on the Render button and it will start the conversion process.
- You will need to save the file to your computer - you can leave it as
untitled.qmd
- You will need to save the file to your computer - you can leave it as
- You should see some code scrolling by in the
Background Jobs
tab and then a rendered (converted) document should appear in theViewer
tab pane.
If the document appears, then your R, RStudio, Quarto, and RMarkdown are working!
- If it does not appear, there should be error messages in the
Console
orBackground Jobs
panes and you can try re-installing the software.
A.3 Configure RStudio
RStudio is a powerful IDE so there are many options for configuring it to your needs.
You can adjust your configuration using the Main menu item for Tools\ Global Options...
.
Suggest considering the following:
A.3.1 General: Basic Tab
- R Sessions
- If this is the only course or work for which you are using R this semester, change the
Default working directory
to the directory on your computer you have created for this course. If you have not created a director, now is a good time to do so. - Check
Restore most recently opened project at startup
.
- If this is the only course or work for which you are using R this semester, change the
- Save Workspace
- By default RStudio will automatically save your workspace (a bunch of leftover stuff) when you exit it and restore it when you reopen.
- This might streamline your work but it is bad for reproducibility as you might unintentionally be using the results from code you ran in a previous session (which you no longer can view and may not remember so you can’t reproduce).
- To change this option.
- Uncheck
Restore .RData into workspace at startup
. - Change
Save workspace to .RData on exit
toNever
.
- Uncheck
- By default RStudio will automatically save your workspace (a bunch of leftover stuff) when you exit it and restore it when you reopen.
A.3.2 Code: Editing Tab
- Check (turn on) the following if not already on:
Insert matching parens/quotes
Use native pipe operator
- Un-check (turn off) the following if already on:
- Soft-wrap R Source Files
- Under
Execution
selectCtrl+Enter executes
to beMulti-line R Statement
A.3.3 Code: Display Tab
- Check (turn on) the following if not already on:
Use Rainbow Parentheses
A.3.4 Appearance
- Change Font Size to one you prefer. You can always adjust on the fly with
CMD +
orCMD -
(or CTRL on Windows)
A.3.5 Pane Layout
- You can adjust the panes and Pane tabs to support your coding preferences.
- Some people prefer to have the source on the left and the Console on the right
- Others prefer to have the Source on the Left and the Console on the bottom.
- It is purely a personal choice and has no effect on how the IDE operates.
A.3.6 R Markdown
- Basic Tab
- Check (turn on) the following if not already on:
Soft-wrap R Markdwon files
- Advanced
- Check (turn on) the following if not already on:
Use rainbow fenced divs
- Visual: General
- Check
Use visual editor by default for new documents
if you like using the visual editor as your default.
- Check
You can adjust your configuration at any time although you may need to restart RStudio for the new configuration to take effect.
A.4 RStudio Projects
RStudio Projects are a wonderful feature of the RStudio IDE to simplify organizing and working with multiple files in a defined piece of work, e.g., the data and source code files for a homework assignment.
An RStudio Project
is consists of a single folder or directory on your computer and all of the sub-folders/sub-directories underneath it.
- If you create a Homework 1 folder, you can turn it into an RStudio project.
When you open an RStudio Project, RStudio will automatically set up your console working directory, your files pane working directory, and your terminal pane prompt to be the folder of the RStudio project.
- If you are working with a Git Repository, RStudio Projects should align with the repository and can help you do common Git and GitHub functions using the Git Pane.
You can create new projects or turn an existing directory into a project using RStudio.
See RStudio Projects for more help on creating and using RStudio Projects for more efficient operation with R.
A.5 Conclusion
- You should now be able to install the software you need to:
- Start an RStudio session by clicking on .
- Use RStudio to create and save a Quarto file to a folder on your computer.
- Use RStudio to render/convert your file to PDF or HTML formatted output document in a folder on your computer.