class: title-slide, center, middle # R Tips & Tricks --- # 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;" /> ] -- My ligature font of choice is [JetBrains Mono](https://www.jetbrains.com/lp/mono/), but there are [many](https://betterwebtype.com/articles/2020/02/13/5-monospaced-fonts-with-cool-coding-ligatures/) to choose from. --- # 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;" /> --- # Use a Different Theme 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. -- I created (and use) the [outRun](https://github.com/camkay/outRun) theme, but I encourage you to find a theme that suits your personal style. --- # 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, I use my package [{merlinmann}](https://github.com/camkay/merlinmann) to deliver me a random piece of wisdom from Merlin Mann every time I start 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. -- For more information on using these packages, I would recommend reading [the Customise your RStudio Startup Message tutorial](https://damien-datasci-blog.netlify.app/post/2020-12-31-pimp-your-r-startup-message/) from Damien Dupré. --- # 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.128 sec elapsed ``` ```r # radix sort tic() # start timer x <- sort(rnorm(1e6), method = "radix") toc() # end timer ``` ``` ## 0.056 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") 118.6954 120.3789 126.2745 120.8405 ## uq max neval ## 124.8556 163.6604 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.05397 53.16027 53.48548 53.7057 ## uq max neval ## 53.86633 54.50757 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) ``` --- # Paste an Object -- Sometimes you will want to copy-and-paste an object to another script. -- One way to do this is using `dput()` from Base R, which creates an ASCII text representation of an R object. -- ```r rodrigo <- data.frame(number = c(1, 5, 6), title = c("Brutal", "Deja Vu", "Good 4 U"), length = c(2.38, 3.58, 2.97)) rodrigo ``` ``` ## number title length ## 1 1 Brutal 2.38 ## 2 5 Deja Vu 3.58 ## 3 6 Good 4 U 2.97 ``` ```r dput(rodrigo) ``` ``` ## structure(list(number = c(1, 5, 6), title = c("Brutal", "Deja Vu", ## "Good 4 U"), length = c(2.38, 3.58, 2.97)), class = "data.frame", row.names = c(NA, ## -3L)) ``` --- # 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(mtcars, aes(x = mpg)) + geom_histogram(fill = "#15758c", color = "white") + transition_states(cyl) + labs(title = "Cylinders: {closest_state}", y = "Count") + theme_bw() ``` <img src="10-slides_files/figure-html/unnamed-chunk-34-1.gif" style="display: block; margin: auto;" /> --- # Other Resources -- Check out the [resources](https://uorbootcamp2022.netlify.app/resources/) page on the [UO R Bootcamp](https://uorbootcamp2022.netlify.app) website for additional readings, websites, tutorials, and classes! --- class: inverse, center, middle # Q & A
−
+
05
:
00