+ - 0:00:00
Notes for current slide
Notes for next slide

Data Literacy: Introduction to R

Workflow & Github

Veronika Batzdorfer

2025-05-23

1 / 29

Space of names ♠️

Packages are developed (and maintained) by different people and teams and - according to METACRAN - there are currently more than 18,400 packages available on CRAN. Also, there are clear limits for creativity in naming functions. Hence, different packages can use the same function names. This can create what is called a "namespace conflict".

2 / 29

Masking 😷

If you load a package and some of its functions have the same names as those from packages you loaded before in your current R session, it "masks" these functions.

library(dplyr)
3 / 29

Avoiding/resolving namespace conflicts

The order in which packages are loaded (in a session) matters as the masking of functions happens consecutively. You can, however, still access masked functions.

stats::filter() # package_name::function_name()

This is also a way of accessing functions from an installed package without loading it with the library() command.

You can also unload a package, but this generally is/should not be the preferred option.

detach("package:dplyr", unload = TRUE)
4 / 29

Avoiding/resolving namespace conflicts

A helpful tool for detecting as well as resolving namespace conflicts is the conflicted package.

5 / 29

Updating packages

You can update all packages you have installed with the following command:

update.packages()

If you want to update individual packages, the easiest way is to install them again (the usual way).

Another option for updating specific packages is the following:

update.packages(oldPkgs = c("correlation", "effectsize"))
6 / 29

Uninstalling packages

If you want to uninstall one or more packages, you can do so as follows:

# uninstall one package
remove.packages("correlation")
# uninstall multiple packages
remove.packages(c("correlation", "effectsize"))
7 / 29

Advanced commenting of R scripts

A neat trick in RStudio is that if you end a comment preceding a block of code with #### (or more # or ---- or more dashes), you create a section in your script that can be collapsed and expanded in the RStudio script pane. This also creates a small interactive table of contents for the script at the bottom of the script pane.

You can also use the keyboard shortcut Ctrl + Shift + R (Windows & Linux)/Cmd + Shift + R (Mac) to insert code sections.

8 / 29

Advanced commenting of R scripts

9 / 29

Advanced commenting of R scripts

10 / 29

RStudio projects

RStudio projects are helpful tool for developing a project-oriented workflow that can enhance reproducibility.

You can create a project via the RStudio menu: File -> New Project. RStudio projects are associated with .Rproj files that contain some specific settings for the project. If you double-click on a .Rproj file, this opens a new instance of RStudio with the working directory and file browser set to the location of that file (the repository/folder for this workshop contains an .Rproj file, if you want to try this out).

Explaining RStudio projects in detail is beyond the scope of this course, but there are good tutorials available, e.g., on the RStudio support site or in the respective chapter in What They Forgot to Teach You About R.

11 / 29

Customization options in RStudio

RStudio offers a wide range of customization options. In the following, we will briefly discuss two of them:

  • pane layout

  • appearance

12 / 29

RStudio pane layout

You can adjust the size, minimize, and maximize the different panes in RStudio . You can also customize the pane layout in RStudio by via Tools -> Global Options -> Pane Layout.

13 / 29

R workspace

In addition to the Environment tab in RStudio, you can view the content of your workspace via the ls() command.

You can remove objects from your workspace with the rm() function. Note, however, that this cannot be undone, so you have to create the object again in case you need it (again).

Unless you change the default settings in RStudio as suggested in the "Getting Started" session, R will save the workspace in a file called .RData in your current working directory and restore the workspace when you restart it.

14 / 29

Package documentation

Packages on CRAN include (PDF) Reference Manuals (see the CRAN website for the usethis package for an example) that include descriptions of all functions included in the package.

Some packages also provide their own websites with detailed additional documentation (the quanteda package for text analysis is a good example here).

If packages are hosted on GitHub, the associated repository normally also includes some documentation for the package. If you encounter an issue while using a package that is hosted on GitHub, you can also check out the Issues section in the GitHub repository for that package.

15 / 29

Coding styles

R is pretty flexible in styling (compared, e.g., to Python, which has strict rules for indentation) which makes it even more necessary to think about these conventions.

16 / 29

Starting with Git & GitHub

  • Git: version control in code/ projects (history with snapshots (commits))

  • Github: platform for hosting Git repositories and collaboration

17 / 29

Why use Github with R?

  • to track and merge changes in R scripts or projects with multiple collaborators

  • maintain version history/ backup

  • early publishing share work (RMarkdown)

18 / 29

Setup

  • create a GitHub account

  • GitHub Desktop: userfriendly interface to manage repositories

  • install Git

  • (in case doesnt work straight away: Configure Git: go to Tools > Global Options > Git/SVN and make sure that the box Git executable points to your Git executable)

20 / 29

Integration with R

Step 1: Clone a Repository with GitHub Desktop
  • Open GitHub Desktop.

  • Click "File" > "Clone Repository".

  • Choose a GitHub repo (or paste URL), select local path, click "Clone".

21 / 29

Integration with R

Step 1: Clone a Repository with GitHub Desktop
  • Open GitHub Desktop.

  • Click "File" > "Clone Repository".

  • Choose a GitHub repo (or paste URL), select local path, click "Clone".

    Step 2: Open the Project in RStudio
  • In RStudio: File > Open Project, navigate to the cloned folder (.Rproj file).

  • Now you're working in an RStudio Project linked to GitHub.

22 / 29

Workflow Overview

  • Pull – Get the latest changes from GitHub.
git pull
23 / 29

Workflow Overview

  • Pull – Get the latest changes from GitHub.
git pull
  • Edit and Save your R scripts or R Markdown.
24 / 29

Workflow Overview

  • Pull – Get the latest changes from GitHub.
git pull
  • Edit and Save your R scripts or R Markdown.
  • Commit – Save changes with a message (e.g., "added analysis plot").
git commit -m "version control- go for launch"
25 / 29

Workflow Overview

  • Pull – Get the latest changes from GitHub.
git pull
  • Edit and Save your R scripts or R Markdown.
  • Commit – Save changes with a message (e.g., "added analysis plot").
git commit -m "version control- go for launch"
  • Push – Send your changes back to GitHub.
git push
26 / 29

Workflow in RStudio

(Use Git tab in RStudio or GitHub Desktop for commit/push)

27 / 29

Workflow in RStudio

(Use Git tab in RStudio or GitHub Desktop for commit/push)

28 / 29

Tips

  • Use .gitignore to avoid pushing large data files.

  • Commit often with meaningful messages.

  • Use branches for experimenting without breaking the main code.

29 / 29

Space of names ♠️

Packages are developed (and maintained) by different people and teams and - according to METACRAN - there are currently more than 18,400 packages available on CRAN. Also, there are clear limits for creativity in naming functions. Hence, different packages can use the same function names. This can create what is called a "namespace conflict".

2 / 29
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
oTile View: Overview of Slides
Esc Back to slideshow