Road Less Traveled 🛶

Productive RStudio IDE features you may not know

…and some you will

library(emoji)
library(tidyverse)


Note

I am on a MacBook so cmd = command key. If you are on a Windows machine, use ctl instead


❤️ Jeremy’s personal favorites

Output to where?

I like it to go to the console

  • ⚙️ -> Chunk Output in Console

Console is where?

I move my console to the top right quadrant: Tools->Global Options->Pane Layout

Function bodies

  • F2 or cmd+click the function name to expose function body
  • Don’t forget to select a method in the drop down if you can
  • F1 the function name for help
  • My favs because I learn best by seeing what others are already doing 🧠
# HINT: load the tibble package first

dat <- tibble::as_tibble(diamonds) 

View the data in a tab

  • cmd+click the tibble name
  • search whole table
  • filter columns


🧭 Getting around

Command Palette

It’s not unknown, but it’s so productive I cannot not mention it

  • shift+cmd+p
  • once in there, type Global Options and return
  • explore Global Options
    • keybindings, vim anyone?
    • relative line numbers
    • highlight function calls (toggle in Command Palette)

Outline is not out of line

  • ctl+shift+o to navigate your document with the outline on the right
  • Also access outline and functions at the bottom of the doc

Death by a thousand tabs

  • Go to file/function window - find function grow_city()
  • ctl+. to search file tabs

It’s ok to cheat - I don’t believe they’ve been here all along

  • Help -> Cheat Sheets
  • What? There’s one for the IDE? 🧐


💻 Code mode

Editor fun

  • shift+ctl+c to comment all highlighted lines
  • alt+up or alt+down to move lines
  • ctl+alt+down or ctl+alt+up or alt+drag to have multi-line cursor
  • cmd+i to fix indention in highlighted code
  • cmd+shift+m to insert pipe, but which pipe? See shift+cmd+p
  • in the console, what does up do? what about cmd+up?
  • double-click parentheses to highlight contents
dat |>
  ggplot2::ggplot(aes(x = carat, y = price, color = cut)) +
  geom_point(alpha = .3) +
  labs(
    title = "Diamonds: Price as a Function of Carat",
    subtitle = "by cut",
    x = NULL,
    y = NULL
  ) +
  theme_minimal()

Snippets

  • shift+tab to expand a snippet
  • type fun then shift+tab
  • don’t stop now, do if and for as well
  • shinyapp then shift+tab
# try fun, for, if, and shinyapp here



# try demoplot here
  • Tools -> Edit Code Snippets to see them all and make your own


🏥 Search and Rescue

  • find and replace across files
  • shift+cmd+f and search for good_pets, then replace
  • but wait, let’s only look in .R files, so use custom filter


🪲 Bugs

Breakpoints and browser()

Let’s jump over to the my_func.R file for this piece

  • step through the body of a function
  • see variable values along the way
  • choose which environment to see


🆒 Visual editor


📦 Advanced

⛓️ Connections

The IDE installs Posit’s pro drivers for you

In the Connections pane

  • click New Connection
  • chose a driver
  • the IDE installs it before helping you start the connection
  • if the install fails, update packages and try again 🔌


Requires writing packages

Templates

Project Teamplates
An Rmd example

Addins

RStudio Addins



So Much More

Have you tried background jobs yet?

Tip: you can activate one from inside an R file - Source -> Source as Background Job

RStudio User Guide!


code

References

Little, Alexander G., Isabella Loughland, and Frank Seebacher. 2020. “What Do Warming Waters Mean for Fish Physiology and Fisheries?” Journal of Fish Biology 97 (2): 328–40. https://doi.org/10.1111/jfb.14402.
Melnychuk, Michael C., Emily Peterson, Matthew Elliott, and Ray Hilborn. 2016. “Fisheries Management Impacts on Target Species Status.” Proceedings of the National Academy of Sciences 114 (1): 178–83. https://doi.org/10.1073/pnas.1609915114.