Exam Notes

R Package Installation

To start, install the remotes package to enable package installation from GitHub. This package simplifies the installation of R packages directly from GitHub repositories, which is particularly useful for installing packages that are not yet available on CRAN (Comprehensive R Archive Network):

install.packages("remotes")

Then, install the ENVX R package using:

remotes::install_github("ENVXStatistics/ENVXStats")

If prompted about RTools, install it if you are a Windows user. RTools provides essential tools required to build R packages from source. If issues persist, online tutorials will be provided as an alternative. If a prompt to cram packages only appears, select '2' or choose 'None' to skip RTools installation.

Project Setup and Library Loading

Create a new R project specifically for tutorials (e.g., "ENVX tutorials"). Using an R project helps manage dependencies and working directories, making your work reproducible. Load the ENVX package using:

library(ENVX)

to access the tutorials. List available tutorials with:

list_tutorials()

Start a specific tutorial (e.g., tutorial 2) using:

tutorial("2")

File Paths and Working Directory

An R project file (.RProj) sets the working directory, simplifying file path management. By setting a working directory, you can use relative paths to reference files within your project, making your code more portable and easier to share. Store data files within the project directory. Relative paths are easier to manage within a project. For example, instead of using an absolute path like "C:/Users/YourName/Documents/R/MyProject/data/file.csv", you can use a relative path like "data/file.csv" if the file is in the data subfolder of your project.

Data Import

To import data (e.g., an Excel file), use the readxl package. Install it if necessary. The readxl package simplifies reading data from Excel files into R.

install.packages("readxl")
library(readxl)

Then use the read_excel() function, specifying the file path:

data <- read_excel("path/to/your/file.xlsx")

Ensure that the file path is correct and that the file exists in the specified location. Assigning the imported data to a variable, such as data, allows you to manipulate and analyze the data within R.

Functions and Documentation

Functions are essential in R. They perform specific tasks and can be reused throughout your code. Use ?function_name to access documentation. This command opens the help page for the specified function, providing details on its usage, arguments, and return values. To save the output of a function, assign it to an object:

Smith <- read_excel("agent_smith.xlsx")

Quarto Document Workflow

Always render your Quarto document frequently (or use “render on save”) to ensure code works correctly. Rendering your document allows you to preview the output and identify any errors or inconsistencies in your code or narrative. Use STR to display the structure of an R object. This function provides a concise overview of the object's type, dimensions, and contents, which is useful for debugging and understanding your data.

Help Resources

Utilize the ? command followed by a function name to access documentation. The documentation provides details on function usage, arguments, and return values.

Additional Notes

  • YAML Header: Use the YAML header in Quarto documents to include options that modify the document's structure, add a title, subtitle, author, and enable table of contents (TOC). The YAML header is a section at the beginning of a Quarto document that uses YAML syntax to specify metadata and document settings.

  • Code Chunks: Use code chunks in Quarto documents to embed and run R code. Code chunks are enclosed in triple backticks with the r language specified (e.g., ``{r}).

  • GitHub Copilot: Consider using GitHub Copilot for code assistance (enable it after week 4 to avoid stunting the learning process). GitHub Copilot is an AI-powered code completion tool that can help you write code more efficiently, but it's important to develop your coding skills first before relying on it.