1  Setting up R and Rstudio

1.1 Introduction to Installing R and RStudio

1.1.1 Install R

  1. Go to the CRAN website.
  2. At the top of the page, choose your operating system:
    • Windows: Click on “Download R for Windows” and then “base”. Click on the “Download R x.x.x for Windows” link to download the installer. Run the installer and follow the instructions.
    • macOS: Click on “Download R for (Mac) OS X”. Click on the .pkg file link to download the installer. Open the downloaded file and follow the installation instructions.
    • Linux: Follow the instructions for your specific Linux distribution on the CRAN page.

1.1.2 Install RStudio

  1. Go to the RStudio Download page.
  2. Under “Installers for Supported Platforms”, choose your operating system:
    • Windows: Click on the “RStudio-Installer.exe” link to download the installer. Run the installer and follow the instructions.
    • macOS: Click on the “RStudio-Installer.dmg” link to download the installer. Open the downloaded file and follow the installation instructions.
    • Linux: Click on the “RStudio-Installer.deb” link for Debian-based distributions or the “RStudio-Installer.rpm” link for RedHat-based distributions. Follow the instructions for your specific Linux distribution.

1.1.3 Update R and RStudio

1.1.3.1 Why Update?

Keeping R and RStudio up to date is important because updates often include:

  • Bug fixes: Resolving known issues that might affect performance and functionality.
  • New features: Adding new tools and capabilities that can make your work easier and more efficient.
  • Packages: Some packages may need newer versions of R to run.

1.1.3.2 How to Update R

  1. Visit the CRAN website.
  2. Download the latest version of R for your operating system following the installation instructions provided above.
  3. Install the new version of R. During installation, you can choose to keep your existing packages or reinstall them after the update.

1.1.3.3 How to Update RStudio

  1. Open RStudio.
  2. Go to Help > Check for Updates.
  3. If an update is available, follow the prompts to download and install the latest version.
  4. Alternatively, you can visit the RStudio Download page and download the latest version for your operating system, then run the installer.

1.1.4 Install Packages

Packages are collections of R functions, data, and compiled code in a well-defined format. They extend the functionality of R and are stored in repositories like CRAN. To use additional functionality in R, you often need to install and load packages.

1.1.4.1 How to Install Packages

To install a package, use the install.packages() function. For example, to install the ggplot2 package, you would run:

install.packages("ggplot2")

You could also add the “dependecies = TRUE” if you want to be explicit about also installing additional packages that the package depends on.

install.packages("ggplot2",  dependencies = TRUE)

Once installed, you need to load the package into your R session with the library() function:

library(ggplot2)

2 Work space

To ensure each R-session always starts fresh, with no dataset or objects loaded, ensure you uncheck the box Restore .RData and that it never asks to save the whole workspace. Go to Preferences > General. Uncheck the option Restore .RData into workspace at startup. Select Never for the option to save your workspace.

Reasons Not to Save RStudio Workspace:

  1. Reproducibility: Ensures scripts run consistently on any system.
  2. Memory Management: Prevents slow performance due to large datasets.
  3. Clarity and Control: Keeps the working environment clean and manageable.
  4. Avoiding Errors: Reduces risks of unintended variable conflicts and errors.
  5. Best Practices: Demonstrates that code is self-sufficient and robust.

More on Rstudio

A screenshot showing ChatGPT 4o screen and how many Rs there are in strawberry.
Figure 2.1: Now in RStudio, each new session starts empty, forgetting any code from prior sessions. You can recreate the environment using your quarto documents or R-scripts.

2.0.1 Installing the tidyverse Package

install.packages("tidyverse")

Then load the tidyverse package:

library(tidyverse)```

2.0.2 Getting Started with R and RStudio

Once you have installed both R and RStudio, you can open RStudio and start writing R code. RStudio provides a powerful environment for coding in R with features like syntax highlighting, code completion, and an integrated console.

Here are some additional resources to help you get started: * R Documentation: Comprehensive documentation for R functions and packages. * RStudio Tutorials: Tutorials and webinars to help you learn R and RStudio.

3 Exercises

Note

To use the exercises, you need to have a JavaScript-enabled browser.

What function loads a package that is already on your computer?

What function is used to install packages in R?

Which of the following is NOT a core package in the tidyverse?

How do you load the tidyverse package?

How do you install the tidyverse package?

What is the difference between base R and the Tidyverse

4 tangentkombinationer

book https://stat545.com/index.html

Materials for teaching https://education.rstudio.com/teach/materials/

tools for teaching https://education.rstudio.com/teach/tools/

version control and git https://peerj.com/preprints/3159/ https://happygitwithr.com/big-picture

load packages recuangular data in compute something very important write rectangular data out rectangular data in make a figure save figure as pdf or png

https://datasciencebox.org/02-exploring-data