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".
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)
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)
A helpful tool for detecting as well as resolving namespace conflicts is the conflicted
package.
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"))
If you want to uninstall one or more packages, you can do so as follows:
# uninstall one packageremove.packages("correlation")# uninstall multiple packagesremove.packages(c("correlation", "effectsize"))
R
scriptsA 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.
R
scriptsR
scriptsRStudio 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.
RStudio offers a wide range of customization options. In the following, we will briefly discuss two of them:
pane layout
appearance
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
.
R
workspaceIn 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.
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.
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.
Git: version control in code/ projects (history with snapshots (commits))
Github: platform for hosting Git repositories and collaboration
to track and merge changes in R scripts or projects with multiple collaborators
maintain version history/ backup
early publishing share work (RMarkdown)
GitHub Desktop: userfriendly interface to manage repositories
(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)
Open GitHub Desktop.
Click "File" > "Clone Repository".
Choose a GitHub repo (or paste URL), select local path, click "Clone".
Open GitHub Desktop.
Click "File" > "Clone Repository".
Choose a GitHub repo (or paste URL), select local path, click "Clone".
In RStudio: File > Open Project, navigate to the cloned folder (.Rproj file).
Now you're working in an RStudio Project linked to GitHub.
git pull
git pull
git pull
git commit -m "version control- go for launch"
git pull
git commit -m "version control- go for launch"
git push
(Use Git tab in RStudio or GitHub Desktop for commit/push)
(Use Git tab in RStudio or GitHub Desktop for commit/push)
Use .gitignore to avoid pushing large data files.
Commit often with meaningful messages.
Use branches for experimenting without breaking the main code.
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".
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 |
o | Tile View: Overview of Slides |
Esc | Back to slideshow |
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".
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)
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)
A helpful tool for detecting as well as resolving namespace conflicts is the conflicted
package.
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"))
If you want to uninstall one or more packages, you can do so as follows:
# uninstall one packageremove.packages("correlation")# uninstall multiple packagesremove.packages(c("correlation", "effectsize"))
R
scriptsA 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.
R
scriptsR
scriptsRStudio 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.
RStudio offers a wide range of customization options. In the following, we will briefly discuss two of them:
pane layout
appearance
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
.
R
workspaceIn 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.
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.
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.
Git: version control in code/ projects (history with snapshots (commits))
Github: platform for hosting Git repositories and collaboration
to track and merge changes in R scripts or projects with multiple collaborators
maintain version history/ backup
early publishing share work (RMarkdown)
GitHub Desktop: userfriendly interface to manage repositories
(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)
Open GitHub Desktop.
Click "File" > "Clone Repository".
Choose a GitHub repo (or paste URL), select local path, click "Clone".
Open GitHub Desktop.
Click "File" > "Clone Repository".
Choose a GitHub repo (or paste URL), select local path, click "Clone".
In RStudio: File > Open Project, navigate to the cloned folder (.Rproj file).
Now you're working in an RStudio Project linked to GitHub.
git pull
git pull
git pull
git commit -m "version control- go for launch"
git pull
git commit -m "version control- go for launch"
git push
(Use Git tab in RStudio or GitHub Desktop for commit/push)
(Use Git tab in RStudio or GitHub Desktop for commit/push)
Use .gitignore to avoid pushing large data files.
Commit often with meaningful messages.
Use branches for experimenting without breaking the main code.