Skip to content

Instantly share code, notes, and snippets.

@Sejmou
Created April 23, 2022 14:15
Show Gist options
  • Save Sejmou/dbac11b5cb74aac64672aab47b77ce50 to your computer and use it in GitHub Desktop.
Save Sejmou/dbac11b5cb74aac64672aab47b77ce50 to your computer and use it in GitHub Desktop.
RStudio code snippets
snippet imfn
```{r, echo=F}
# This is a custom function I wrote for showing images
# It is more robust than embedding images in R markdown directly
# With imgs embedded in R markdown, compilation of whole document fails if img missing
# This function also displays the image if possible and just logs an error otherwise
# Under the hood, knitr::include_graphics() is used, which also accepts multiple image paths as parameter
# Therefore, with this function, side-by-side images are also supported
# We then need to pass a vector of paths in the function call and add fig.show='hold' to the code cell
# Be sure to also set out.width accordingly! (still per image)
# details: https://stackoverflow.com/a/54870008/13727176
showimg <- function(paths, add = FALSE) {
invalid_imgs <- FALSE
for (path in paths) {
if (!file.exists(path)) {
message(paste0("cannot show image: file '", path, "' not found"))
invalid_imgs <- TRUE
}
}
if (!invalid_imgs) {
knitr::include_graphics(paths)
}
}
```
snippet imadd
```{r, echo=F, out.width='80%', fig.align='center'}
showimg("./${1:file_name}")
```
snippet pm
```{r}
if (!require(pacman)) install.packages('pacman')
library(pacman)
```
snippet pme
**Note:** I use the [`pacman`](https://www.rdocumentation.org/packages/pacman/versions/0.5.1) R package for dealing with libraries wherever possible. This is more convenient compared to the regular `require()`, `install.packages()` and `library()` calls. We can just use `pacman`'S `p_load()` function whenever we need a package. It will import all requested libraries for us and try to install them too, in case they are missing. An additional advantage of `p_load()` is that it accepts multiple packages as input, contrary to `library()`, where the import for each library has to be done with a separate function call. Let's import `pacman` (or install it, if it is missing):
```{r}
if (!require(pacman)) install.packages('pacman')
library(pacman)
```
snippet r
```{r}
${1:code}
```
snippet rne
```{r, eval=FALSE}
${1:code}
```
snippet nonr
```
${1:code}
```
snippet _
&nbsp;
snippet opts
```{r, setup, include=FALSE}
knitr::opts_chunk\$set(
comment = ''
)
```
snippet p_load
```{r}
p_load(${1:packages})
```
snippet p_loade
Now, we use `pacman` to import the required packages:
```{r}
p_load(${1:packages})
```
snippet c
`${1:inline_code}` $2
snippet l
[`${1:link_text}`](${2:link}) $3
snippet lc
[`${1:code}`](${2:link}) $3
snippet fn
`${1:function_name}()` $2
snippet fnf
`${1:function_name}()` function $2
snippet fnp
`${1:package_name}`'s `${2:function_name}()` function $3
snippet nodata
**Note**: In some instances, datasets were not made available by DataCamp (no link to them was given), so some code cells cannot be run locally in a meaningful way. All affected datasets are explicitly mentioned. Every code cell using them is not executed locally when compiling the PDF locally, the code output from DataCamp is copied into the document instead.
snippet nodata2
**Note**: The `${1:dataset}` dataset used in the following code examples is not made available by DataCamp. The code cells therefore cannot be executed locally in a meaningful way. Execution is skipped, and the code output from the R session on DataCamp is added instead.
snippet nodataslides
**Note**: The `${1:dataset}` dataset used in the following code examples is not made available by DataCamp. The code cells therefore cannot be executed locally. The same code output as demonstrated in the course video slides is shown.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment