Chapter 13 Advanced Coding Tips

13.1 Grammar and Syntax

13.1.1 Regex expressions and symbols

str_remove(html$`Market Price`, pattern = "$") # doesn't remove the $ sign
str_remove(html$`Market Price`, pattern = "\\$") # works

13.1.2 The colon-equals (:=) operator

Sometimes when making a function you need to use the colon-equals operator, rather than just the normal <- or = assignment operators

Specifically, when you have multiple named arguments in your function…Read my question and someone’s answer on this blogpost: https://community.rstudio.com/t/help-creating-simple-function/109011/2

function(df, col, newCol_name){
  
  # drop all NA's so the function can work properly
  df_NAdropped=df |> drop_na({{col}})
  
  # apply the function, saving it in a small tibble with only the outlier column and a key to join by 
  outliers_key=df_NAdropped |> 
    dplyr::mutate(outliers=dplyr::if_else(round(abs({{col}}-median({{col}}))/(1.483*mad({{col}}, constant = 1)),2)>2.24,1,0)) |> 
    select(subject_id, outliers)
  
  # join back together
  df=df |> left_join(outliers_key, by=c("subject_id")) 
  
  num.outliers=df |> 
    dplyr::filter(outliers==1) |> 
    dplyr::count()
  
  df$outliers=replace_na(df$outliers, 999)
  df=df |> rename({{newCol_name}}:="outliers")
  
  message(paste0(num.outliers, " outlier(s) detected"))
  return(df)
}

13.1.3 User-supplied expressions or named columns in functions

This is when you have to put double braces around something…

13.1.4 When a command requires a named column or data set, but you’ve already supplied it and it’s required a second time

If you’re writing a function with a pipe but the command you’re using needs the data set defined in it, you specify it as .x , or simply just .; Here is an example:

13.2 Creating a package

https://rstudio4edu.github.io/rstudio4edu-book/data-pkg.html

13.2.1 Documenting package meta-data

https://r-pkgs.org/description.html

13.2.2 Connecting to other packages

https://kbroman.org/pkg_primer/pages/depends.html

13.2.3 Linking Git and Github

view this detailed guide by Jenny Bryan, and this YouTube video if you want the full guide; or just follow the TL;DR below.

Quick summary of steps in YouTube video:

  1. Open project folder in Windows Explorer and click in the URL bar, then type cmd to open command prompt

  2. If there are any pre-existing git files or repository info there, remove it with the following: rd .git /S/Q

  3. Tell git to create a new repo by typing: git init

  4. Then tell it to include all files in the current place by typing: git add .

  5. Commit these files with: git commit -m "Initial commit"

  6. At this point you’ve created a git and GitHub repo each; now link them with: git remote add origin [https URL of GitHub repo]

  7. Push all these changes live with: git push -u origin master

13.3 Creating a bookdown

https://www.youtube.com/watch?app=desktop&v=m5D-yoH416Y&feature=youtu.be

13.3.1 Rendering the book once it’s done

  1. Render locally with bookdown::render_book(“index.Rmd”)

  2. Use browseURL("docs/index.html") to view your book locally (or just open index.html in a browser).

  3. If it looks good, commit and push all changed files to GitHub.