class: title-slide, center, middle .top-left[ <img src="images/uo-logo2.png" width="100%" /> ] .top-right[ <img src="images/psy-logo.png" width="100%" /> ] # R Tips & Tricks UO R Bootcamp 2025 --- # Use a Ligature Font -- One (superficial) way to improve your code is to use a **ligature font**. Ligatures are special characters composed of two (or more) characters. -- .pull-left[ **Without Ligatures** <img src="images/rtips_ligature1.png" width="80%" style="display: block; margin: auto auto auto 0;" /> ] .pull-right[ **With Ligatures** <img src="images/rtips_ligature2.png" width="80%" style="display: block; margin: auto auto auto 0;" /> ] --- # Use a Ligature Font To start using a ligature font, all you need to do is install the font on your [Mac](https://support.apple.com/en-us/HT201749) or [PC](https://support.microsoft.com/en-us/office/add-a-font-b7c5f17c-4426-4b53-967f-455339c564c1) and then choose the font in RStudio's preferences pane: <img src="images/rtips_pref_fonts.png" width="40%" style="display: block; margin: auto;" /> --- # Use a Different Theme -- Another superficial way to improve your code is to change RStudio's default theme. -- As with fonts, you can change the theme in RStudio's preferences pane. <img src="images/rtips_pref_theme.png" width="40%" style="display: block; margin: auto;" /> -- You can also [download](https://www.rstudio.com/blog/rstudio-ide-custom-theme-support/) and [create](https://tmtheme-editor.herokuapp.com/#!/editor/theme/Monokai) custom themes. --- # Enable Rainbow Parentheses -- **Rainbow Parentheses** colour matches opening parentheses with closing parentheses. -- .pull-left[ **Without Rainbow Parentheses** <img src="images/rtips_paren.png" width="80%" style="display: block; margin: auto auto auto 0;" /> ] .pull-right[ **With Rainbow Parentheses** <img src="images/rtips_paren2.png" width="80%" style="display: block; margin: auto auto auto 0;" /> ] --- # Enable Rainbow Parentheses You can enable Rainbow Parentheses by clicking the checkbox beside Rainbow Parentheses in RStudio's preferences pane. <img src="images/rtips_pref_paren.png" width="40%" style="display: block; margin: auto;" /> --- # Enable Spell-Checking -- **Real-time spell-checking** will check your spelling in... well... real time. -- .pull-left[ **Without Real-Time Spell-Checking** <img src="images/rtips_spellcheck1.png" width="100%" style="display: block; margin: auto auto auto 0;" /> ] .pull-right[ **With Real-Time Spell-Checking** <img src="images/rtips_spellcheck2.png" width="100%" style="display: block; margin: auto auto auto 0;" /> ] --- # Enable Spell-Checking You can enable real-time spell-checking by clicking the checkbox beside real-time spell-checking in RStudio's preferences pane. <img src="images/rtips_pref_spellcheck.png" width="40%" style="display: block; margin: auto;" /> --- # Start-Up Messages -- You can set R to provide a custom start-up message every time you start or restart R. -- For example, Cam (previous RBootcamp instructor) uses his package [{merlinmann}](https://github.com/camkay/merlinmann) to deliver a random piece of wisdom from Merlin Mann every time he starts or restart R: <img src="images/rtips_startup.png" width="50%" style="display: block; margin: auto;" /> --- # Start-Up Messages Other custom start-up message options include: - Mitch Hedberg jokes from the [{mitchhedberg}](https://github.com/mkearney/mitchhedberg) package. - Chuck Norris jokes from the [{norris}](https://github.com/chriscardillo/norris) package. - Dad jokes from the [{dadjokeapi}](https://github.com/jhollist/dadjokeapi) package. - The Office quotes from the [{dundermifflin}](https://cran.r-project.org/web/packages/dundermifflin/dundermifflin.pdf) package. - The Good Place quotes from the [{goodshirt}](https://github.com/adam-gruer/goodshirt) package. - General statistics, data visualization, and science quotes from the [{statquotes}](https://cran.r-project.org/web/packages/statquotes/statquotes.pdf) package. - Random facts about Carl Friedrich Gauss from the [{gaussfacts}](https://cran.r-project.org/web/packages/gaussfacts/) package. - Praise from the [{praise}](https://cran.r-project.org/web/packages/praise/praise.pdf) package. - Fortunes from the [{fortunes}](https://cran.r-project.org/web/packages/fortunes/fortunes.pdf) package. -- --- # Multicursor Selection -- Holding down Option (<key>⌥</key>) on Mac or Alt (<key>Alt</Key>) on PC while clicking and dragging the cursor allows you to create a cursor across multiple lines: <img src="images/rtips_multicursor1.gif" width="50%" style="display: block; margin: auto;" /> --- # Multicursor Selection Holding down Option and Command (<key>⌥</key> + <key>⌘</key>) on Mac or Alt and Control (<key>Alt</Key> + <key>Ctrl</key>) on PC while clicking at different locations in your script allows you to create multiple cursors with different horizontal locations. <img src="images/rtips_multicursor2.gif" width="60%" style="display: block; margin: auto;" /> --- # Comment Chunks of Code -- You can comment out multiple lines of code by highlighting them and then clicking Shift, Command, and C (<key>⇧</key> + <key>⌘</key> + <key>C</key>) on Mac or Control, Shift, and C (<key>⇧</key> + <key>Ctrl</key> + <key>C</key>) on PC. <img src="images/rtips_comment.gif" width="40%" style="display: block; margin: auto;" /> --- # Search Through Code History -- You can search for previously evaluated lines of code by pressing the up arrow (<key>↑</key>) on your keyboard. <img src="images/rtips_up_arrow.gif" width="60%" style="display: block; margin: auto;" /> --- # Search the Help System -- The question mark (`?`) is very useful for looking up the help documentation for a given function (e.g., `?mean`) but, sometimes, the function you are looking for is in a package that is not loaded. In those cases, you can use two question marks (`??`) to search all of the help documentation. For example, typing`??if_else` in the console returns: <img src="images/rtips_help2.png" width="40%" style="display: block; margin: auto;" /> --- # Assignment Pipe -- We've talked about regular pipes (`%>%`), but the `{magrittr}` package also has a number of alternative pipes available.footnote[*Note.* These are not loaded as part of the `{tidyverse}` package; you will need to load the `{magrittr}` package separately to use them.]. -- One useful alternative pipe is the **asignment pipe** (`%<>%`). The assignment pipe pipes an object forward---as a normal pipe does---but it assigns the result of the pipe to the original object. --- # Assignment Pipe We've talked about regular pipes (`%>%`), but the `{magrittr}` package also has a number of alternative pipes available. One useful alternative pipe is the **asignment pipe** (`%<>%`). The assignment pipe pipes an object forward---as a normal pipe does---but it assigns the result of the pipe to the original object. For example: .pull-left[ ``` r x <- c(1, 2, 3, 4) x <- x %>% mean() %>% round() x ``` ``` ## [1] 2 ``` ] .pull-right[ ``` r x <- c(1, 2, 3, 4) x %<>% mean() %>% round() x ``` ``` ## [1] 2 ``` ] --- # Pipes Redux -- Thus far, we've used pipes only in cases where we want to pass an object to the first argument in subsequent functions. However, sometimes, we will want to pass an object to the second, third, or fourth (etc.) argument. -- Take, for example, the `gsub()` function in Base R. `gsub()` replaces some set of characters in a string with another set of characters: ``` r the_best_star_trek <- "Star Trek: The Next Generation" the_best_star_trek <- gsub(pattern = "The Next Generation", replacement = "Voyager", x = the_best_star_trek) the_best_star_trek ``` ``` ## [1] "Star Trek: Voyager" ``` -- What if we wanted to pipe `the_best_star_trek` to the `gsub()` function? --- # Pipes Redux We can use the dot placeholder (`.`). Essentially, the dot placeholder tells `{magrittr}` where to pipe the information. For example: ``` r the_best_star_trek <- "Star Trek: The Next Generation" the_best_star_trek %>% gsub(pattern = "The Next Generation", replacement = "Voyager", x = .) ``` ``` ## [1] "Star Trek: Voyager" ``` --- # Time Your Code -- There are multiple different ways to accomplish most tasks in R and, many times, you will want to choose the way that is the quickest. But how can you tell? You can use the `tic()` and `toc()` functions from the `{tictoc}` package to time your code: ``` r # shell sort tic() # start timer x <- sort(rnorm(1e6), method = "shell") toc() # end timer ``` ``` ## 0.124 sec elapsed ``` ``` r # radix sort tic() # start timer x <- sort(rnorm(1e6), method = "radix") toc() # end timer ``` ``` ## 0.055 sec elapsed ``` --- # Time Your Code You can also use the `microbenchmark()` function from the `{microbenchmark}` package to time your code. The `microbenchmark()` function runs each snippet of code multiple times and averages together the resulting durations. This results in a better estimate of the time it took to run the code. ``` r # shell sort microbenchmark(x <- sort(rnorm(1e6), method = "shell"), times = 10L) ``` ``` ## Warning in microbenchmark(x <- sort(rnorm(1e+06), method = "shell"), times = ## 10L): less accurate nanosecond times to avoid potential integer overflows ``` ``` ## Unit: milliseconds ## expr min lq mean median ## x <- sort(rnorm(1e+06), method = "shell") 116.6978 118.5329 127.0218 119.739 ## uq max neval ## 120.7544 159.4109 10 ``` --- # Time Your Code You can also use the `microbenchmark()` function from the `{microbenchmark}` package to time your code. The `microbenchmark()` function runs each snippet of code multiple times and averages together the resulting durations. This results in a better estimate of the time it took to run the code. ``` r # radix sort microbenchmark(x <- sort(rnorm(1e6), method = "radix"), times = 10L) ``` ``` ## Unit: milliseconds ## expr min lq mean median ## x <- sort(rnorm(1e+06), method = "radix") 52.34613 52.95429 58.16905 54.00989 ## uq max neval ## 55.73815 93.19706 10 ``` --- # Notification Chimes -- Sometimes code will take a long time to run, and you will start doing something else. It can be useful to be notified when the code finishes evaluating. You can use the `beep()` function from the `{beepr}` package to play a chime when your code has finished evaluating. ``` r x <- sort(rnorm(5e7), method = "shell") beep(sound = 2) ``` --- # TRUE/FALSE Are Just Numbers -- Under the hood, `TRUE` and `FALSE` are really just numbers. Specifically, `TRUE` is 1 and `FALSE` is 0. -- ``` r duck_wins_21_22 <- c(T, T, F, T, F, F, T, T, F, F, T, F, T, T, T, T, T, T, F, T, T, T, T, F, T, F, F, T, F, F, F, T, F, T, F) ``` -- As such, you can count the number of `TRUE`s in a vector using `sum()`. -- ``` r sum(duck_wins_21_22) ``` ``` ## [1] 20 ``` --- # TRUE/FALSE Are Just Numbers Under the hood, `TRUE` and `FALSE` are really just numbers. Specifically, `TRUE` is 1 and `FALSE` is 0. ``` r duck_wins_21_22 <- c(T, T, F, T, F, F, T, T, F, F, T, F, T, T, T, T, T, T, F, T, T, T, T, F, T, F, F, T, F, F, F, T, F, T, F) ``` You can also count the number of `FALSE`s using a combination of `!` and `sum()`. -- ``` r sum(!duck_wins_21_22) ``` ``` ## [1] 15 ``` --- # TRUE/FALSE Are Just Numbers Under the hood, `TRUE` and `FALSE` are really just numbers. Specifically, `TRUE` is 1 and `FALSE` is 0. ``` r duck_wins_21_22 <- c(T, T, F, T, F, F, T, T, F, F, T, F, T, T, T, T, T, T, F, T, T, T, T, F, T, F, F, T, F, F, F, T, F, T, F) ``` You can also calculate the proportion of `TRUE`s in a vector with `mean()`. -- ``` r mean(duck_wins_21_22) ``` ``` ## [1] 0.5714286 ``` --- # Parse Numbers -- You can use the `parse_number()` function from the `{readr}` package to strip any non-numeric characters in a vector. ``` r afc_richmond <- c("24 Sam Obisanya", "Roy Kent 6", "Jamie 9 Tartt") parse_number(afc_richmond) ``` ``` ## [1] 24 6 9 ``` --- # Create Interactive Charts -- You can use the `ggplotly()` function from the `{plotly}` package to create interactive plots. ``` r plot <- ggplot(mtcars, aes(x = mpg)) + geom_histogram(fill = "#15758c", color = "white") + labs(y = "Count") + theme_bw() ggplotly(plot) ```
--- # Create Animated Plots -- You can use the `{gganimate}` package to create animated plots. ``` r ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) + geom_point(alpha = 0.7, show.legend = FALSE) + scale_colour_manual(values = country_colors) + scale_size(range = c(2, 12)) + scale_x_log10() + facet_wrap(~continent) + labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') + transition_time(year) + ease_aes('linear') ``` <img src="images/gganimate.gif" width="40%" style="display: block; margin: auto;" /> --- # Other Resources -- Check out the [resources](https://uorbootcamp2024.netlify.app/resources/) page on the [UO R Bootcamp](https://uorbootcamp2024.netlify.app/) website for additional readings, websites, tutorials, and classes! --- class: yourturn, center, middle # Q & A
−
+
05
:
00