class: center, middle, inverse, title-slide .title[ # Data Literacy: Introduction to R ] .subtitle[ ## Workflow & Github ] .author[ ### Veronika Batzdorfer ] .date[ ### 2025-05-23 ] --- layout: true --- ## Space of names ♠️ Packages are developed (and 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. You can also unload a package, but this generally is/should not be the preferred option. ``` r detach("package:dplyr", unload = TRUE) ``` --- ## 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() ``` 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: ``` 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 for the script at the bottom of the script pane. You can also use the keyboard shortcut <kbd>Ctrl + Shift + R</kbd> (*Windows* & *Linux*)/<kbd>Cmd + Shift + R</kbd> (*Mac*) to insert code sections. --- ## Advanced commenting of `R` scripts <img src="data:image/png;base64,#../img/script_sections_expanded_highlighted.png" width="100%" style="display: block; margin: auto;" /> --- ## Advanced commenting of `R` scripts <img src="data:image/png;base64,#../img/script_sections_collapsed_highlighted.png" width="100%" style="display: block; margin: auto;" /> --- ## *RStudio* projects *RStudio* projects are helpful tool for developing a [project-oriented workflow](https://rstats.wtf/project-oriented-workflow.html) 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](https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects) or in the [respective chapter in *What They Forgot to Teach You About R*](https://rstats.wtf/project-oriented-workflow.html#rstudio-projectsl). --- ## 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 --- ## *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`. <img src="data:image/png;base64,#../img/rstudio_pane_layout.png" width="40%" style="display: block; margin: auto;" /> --- ## `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 `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. --- ## Package documentation Packages on *CRAN* include (PDF) Reference Manuals (see the [*CRAN* website for the `usethis` package](https://cran.r-project.org/web/packages/usethis/index.html) 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](https://quanteda.io/) 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. --- ## Coding styles - line length (see the ["sacred 80 column rule"]( https://www.emacswiki.org/emacs/EightyColumnRule)) - indentation - variable/object naming - comments - assignment rules `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. --- ## 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.