R Cheatsheets Search Table

r cheatsheets DT package r markdown

interactive table of cheatsheet tags and links

Sam Parmar https://parmsam.github.io/
03-12-2021

Quick post: I’ve had challenges finding the right R cheatsheet at the right time. Here is the official RStudio cheatsheets webpage, where you can subscribe to cheatsheet updates. Thought it would be useful to have a quick search table to lookup cheatsheets from the RStudio Github repo. The PDFs are useful to get a refresher on a package or learn more about a package.

Used httr package with Github’s API (based on Stackoverflow post) to get a list of relevant files in the repo. Did some data wrangling to get language, PDF download and preview hyperlinks. Then manually added a column with PDF titles (to the CSV file) to enable easier searching, since oftentimes the file name does not match cheatsheet title.

DT::datatable({
    # https://stackoverflow.com/questions/25485216/how-to-get-list-files-from-a-github-repository-folder-using-r
    library(httr)
    library(stringr)
    library(dplyr)
    #note that there is a default public rate limit; authenticated users get higher rate limit
    #didnt set that up here
    tryCatch(
      expr = {
      csheet_data <- readr::read_csv("https://raw.githubusercontent.com/parmsam/R-cheatsheets-explorer/main/csheet_data.csv")
      },
      error = {
      req <- GET("https://api.github.com/repos/rstudio/cheatsheets/git/trees/master?recursive=1")
      stop_for_status(req)
      filelist <- unlist(lapply(content(req)$tree, "[", "path"), use.names = F)
      
      cheatsheet_list <- filelist[filelist %>% str_detect("pdf")]
      
      base_url <- "https://github.com/rstudio/cheatsheets/raw/master/"
      
      base_gh_url <-"https://github.com/rstudio/cheatsheets/blob/master/"
  
      csheet_data <- cheatsheet_list %>% 
        as_data_frame() %>% 
        rename(`PDF Links` = value)
      
      # readr::write_csv(csheet_data, "csheet_data.csv")
    })
    
    csheet_data2 <- csheet_data %>% 
      cbind(Tags = cheatsheet_list) %>% 
      mutate(Tags = str_replace_all(Tags, "/",", ")) %>%
      mutate(Tags = str_replace_all(Tags, "([a-z])([A-Z])","\\1 \\2")) %>%
      mutate(Tags = str_replace_all(Tags, "[-_]"," ")) %>%
      mutate(Tags = str_remove_all(Tags, "\\.pdf")) %>%
      mutate(`PDF Links` = str_c(base_url, cheatsheet_list)) %>%
      mutate(`Github Links` = str_c(base_gh_url, cheatsheet_list)) %>%
      # mutate(`Embedded` = str_c('<iframe src=', `Github Links`, '></iframe>')) %>%
      mutate(`Github Links` = str_c("<a href='", `Github Links`, "'>Preview here</a>")) %>%
      mutate(`PDF Links` = str_c("<a href='", `PDF Links`, "'>Download here</a>"))
    
    csheet_data3 <- csheet_data2 %>% mutate(Language = str_extract(Tags, "translations, .*,") ) %>% 
      mutate(Language = str_remove(Language, "translations, ")) %>% 
      mutate(Language = str_remove(Language, "|(, [a-zA-Z ,]*)")) %>%
      mutate(Language = ifelse(is.na(Language), "english",  str_remove(Language, ",") )) %>%
      relocate(Tags, Language, `PDF Title`)
      
    csheet_data3}, 
    escape = FALSE)

Distill is a publication format for scientific and technical writing, native to the web.

Learn more about using Distill at https://rstudio.github.io/distill.

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Citation

For attribution, please cite this work as

Parmar (2021, March 12). Data Breadcrumbs: R Cheatsheets Search Table. Retrieved from https://databreadcrumbs.com/posts/2021-03-12-r-cheatsheets-search-table/

BibTeX citation

@misc{parmar2021r,
  author = {Parmar, Sam},
  title = {Data Breadcrumbs: R Cheatsheets Search Table},
  url = {https://databreadcrumbs.com/posts/2021-03-12-r-cheatsheets-search-table/},
  year = {2021}
}