Introduction to R, Posit, and R Markdown
R is a powerful language for statistical computing and graphics, used extensively for data analysis and visualization. Its open-source nature, supported by a vast community, offers extensive packages for various applications. While R operates through a text interface, graphical support significantly enhances usability.
Introduction to Posit (Formerly RStudio)
Posit, previously known as RStudio, enhances R’s functionality with a user-friendly graphical interface. It serves as an essential tool for this course, facilitating the use of R through additional features and integrations.
Working with Posit Cloud and Desktop
Posit Cloud
Access Posit in your browser via Posit Cloud for immediate start-up without installation. Recommended steps after login:
- Create a Course Folder: Organize your work by creating a new folder in the Files window.
- Configure R Markdown: Under Tools -> Global Options -> R Markdown, deselect “Show output inline for cleaner workflow.”
Posit Desktop
For a more responsive experience, especially for courses with high computational demands, installing Posit on your local machine is advisable. This allows for direct file storage and integration with GitHub for version control and collaboration.
Installation and Setup
- Download R and Posit:
- R: CRAN
- Posit: Posit Products Use default options for hassle-free installation.
- Update: Ensure the latest versions by comparing your current R version (
getRversion()
) with the CRAN site and checking Posit updates viaHelp > Check for updates
.
Verification
Launch Posit and test by executing x <- 2 * 4
in the Console. If x
outputs 8
, your setup is correct.
R Markdown and Posit Overview
R Markdown documents, using the .Rmd
extension, integrate executable code with narrative text, supporting output in diverse formats like HTML, PDF, or Word. Key components include:
- YAML Header: Defines document metadata and desired output format.
- Narrative Text: Incorporates executable code chunks into your analysis, employing Markdown for rich text formatting.
- Code Chunks: Facilitate the execution of code (e.g., R, Python) within your document, promoting seamless data analysis and visual representation.
For comprehensive guidance on formatting, visit the R Markdown documentation.
Note: The evolution to Posit represents a significant leap forward, introducing enhanced interfaces and functionalities that enrich the data science and document creation process.
LaTeX Installation for PDF Output in R Markdown
To knit R Markdown documents to PDF, a LaTeX compiler is required. For users of the maize server, no installation is necessary. For those working locally with Posit (formerly RStudio), installing a LaTeX compiler is essential.
Recommended LaTeX Installers
- Mac: MacTeX (3.2GB)
- Windows: MiKTeX (190MB)
- R Package Alternative: For a lightweight solution,
tinytex
can be installed directly from R.
install.packages("tinytex")
tinytex::install_tinytex() # To automatically install TinyTeX
Updating Posit and R
To ensure optimal performance and access to the latest features, keeping Posit and R up-to-date is crucial. This step is unnecessary for maize server users.
- Check R Version: Use
getRversion()
and compare with the latest at CRAN. Update if necessary. - Update Posit: Navigate to
Help > Check for updates
in Posit and follow any instructions to update.
Managing Files
Locally on Posit
- Open files via File > Open File.
On Maize Server
- Use Upload in the Files tab to add .Rmd files to your server space. Select and open uploaded files for editing.
Running Code and Knitting
- Execute code lines with Run or use the play button for chunks.
- Regularly knit your document to track progress and catch errors early. Knitting options include PDF, HTML, and Word formats.
Remember: Regular updates and proper LaTeX installation are key to a smooth document creation process in R Markdown with PDF output.
- Maize users: You may also need to allow for “pop-up” in your web browser when knitting documents.
Few More Instructions
The default setting in Rstudio when you are running chunks is that the “output” (numbers, graphs) are shown inline within the Markdown Rmd. If you prefer to have your plots appear on the right of the console and not below the chunk, then change the settings as follows:
- Select Tools > Global Options.
- Click the R Markdown section and uncheck (if needed) the option Show output inline for all R Markdown documents.
- Click OK.
Now try running R chunks in the .Rmd file to see the difference. You can recheck this box if you prefer the default setting.
VPN
If you plan to do any work off campus this term, you need to install Carleton’s VPN. This will allow you to access the maize server (if needed).
Installing the GlobalProtect VPN
Follow the directions here to install VPN.