class: center, middle, inverse, title-slide .title[ # Data Literacy: Introduction to R ] .subtitle[ ## Workflow & Github ] .author[ ### Veronika Batzdorfer ] .date[ ### 2025-11-07 ] --- layout: true --- ## Space of names ♠️ Packages are developed/maintained by different people and teams and - according to [METACRAN](https://www.r-pkg.org/) - 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 .highlight["namespace conflict"]. --- ## 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. ``` r library(dplyr) ``` --- ## 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. ``` r 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. --- ## Avoiding/resolving namespace conflicts A helpful tool for detecting as well as resolving namespace conflicts is the [`conflicted` package](https://www.tidyverse.org/blog/2018/06/conflicted/). --- ## Updating packages You can update all packages you have installed with the following command: ``` r update.packages() ``` Another option for updating specific packages is the following: ``` r update.packages(oldPkgs = c("correlation", "effectsize")) ``` --- ## Uninstalling packages If you want to uninstall one or more packages, you can do so as follows: ``` r # uninstall one package remove.packages("correlation") # uninstall multiple packages remove.packages(c("correlation", "effectsize")) ``` --- ## 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. --- ## Advanced commenting of `R` scripts <img src="data:image/png;base64,#../img/script_sections_expanded_highlighted.png" width="100%" style="display: block; margin: auto;" /> --- ## *RStudio* projects You can create a project via: `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). --- ## Customization options in *RStudio* *RStudio* offers a wide range of [customization options](https://support.rstudio.com/hc/en-us/articles/200549016-Customizing-RStudio). In the following, we will briefly discuss two of them: - pane layout - appearance --- ## `R` workspace In addition to the `Environment` tab in *RStudio*, you can view the content of your workspace via the .highlight[`ls()`] command. You can remove objects from your workspace with the .highlight[`rm()`] function. 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. --- ## Starting with Git & GitHub - .highlight[Git]: version control in code/ projects (history with snapshots (commits)) - .highlight[Github]: platform for hosting Git repositories and collaboration --- ## 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) --- <img src="data:image/png;base64,#../img/github.jpg" width="95%" style="display: block; margin: auto;" /> [cheetsheet](https://education.github.com/git-cheat-sheet-education.pdf) [documentation](https://docs.github.com/de) .small[[Source](https://github.com/gesis-css-python/materials/blob/main/1-css/1-2-workflow.ipynb) ] --- ## Setup - [create a GitHub account](https://github.com/) - [GitHub Desktop](https://desktop.github.com): userfriendly interface to manage repositories - [install Git](https://git-scm.com/) - (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) --- ## 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 .highlight[(.Rproj file)]. - Now you're working in an RStudio Project linked to GitHub. --- ## Workflow Overview - Pull – Get the latest changes from GitHub. ``` r git pull ``` -- - Edit and Save your R scripts or R Markdown. -- - Commit – Save changes with a message (e.g., "added analysis plot"). ``` r git commit -m "version control- go for launch" ``` -- - Push – Send your changes back to GitHub. ``` r git push ``` --- ## Workflow in RStudio (Use Git tab in RStudio or GitHub Desktop for commit/push) -- <img src="data:image/png;base64,#../img/git_r.png" width="90%" style="display: block; margin: auto;" /> --- ## Tips - Use .highlight[.gitignore] to avoid pushing large data files. - Commit often with meaningful messages. - Use branches for experimenting without breaking the main code.