Skip to content

GitHub repository to store various tricks and tips for R

Notifications You must be signed in to change notification settings

arisp99/RTidbits

Repository files navigation

RTidbits

Package Development

Useful Websites

The following list of sites are particularly handy in package development:

Package Creation

The following will setup a basic package and import the pipe (%>%) from magrittr.

usethis::create_package()
usethis::use_package_doc()
usethis::use_namespace()
usethis::use_tidy_description()
usethis::use_pipe()
usethis::use_news_md()

We can also add a couple more components to the package using the following.

usethis::use_spell_check()
usethis::use_readme_rmd()
usethis::use_mit_license("Aris Paschalidis")
usethis::use_lifecycle_badge("Experimental")

CI and Testing

usethis::use_testthat()
usethis::use_github_actions()

# Note can add more specific actions using
usethis::use_github_action("check-standard")
usethis::use_github_action("check-release")

# Adding a badge to the README
usethis::use_github_action()

# We can also use travis, but we recommend using github actions
# usethis::use_travis()

Data

If we would like our package to have data, we can use the following two commands.

# Creates the file we use to generate our data
usethis::use_data_raw()

# Stores our data
usethis::use_data()

# We can also store internal data
usethis::use_data(internal = TRUE)

Github

# Setting up github
usethis::use_github()
usethis::use_github_labels()
usethis::use_tidy_issue_template()
usethis::use_github_links()

Pull Requests

# Create a PR
usethis::pr_init()

# Push a PR
usethis::pr_push()

# Finish a PR
usethis::pr_finish()

Badges

Badges are written in R Markdown with the following structure:

[![Badge Name]](url to badge image)(url when click on badge)

Some examples:

Build Status

Lifecycle: stable

License: MIT

The package badger has a nice list of badges that can be added.

Below are several useful functions to automatically add badges.

usethis::use_lifecycle_badge()
usethis::use_travis_badge()
usethis::use_github_actions_badge()

Logos

The package hexSticker is very neat for making logos. An example is provided below:

hexSticker::sticker("logo_background.jpg", package = "RTidbits", 
                    filename = "logo.png", 
                    s_x = 0.96, s_y = 0.9, s_width = 1.4,
                    p_size = 6.5, p_x = 1, p_y = 1.6, p_color = "#ed7980",
                    h_color = "#f6ca32", h_fill = "#18478c", 
                    white_around_sticker = T)

The website TinEye may also come in handy for color matching.

In order to properly format and store the logo in the right place, the following may come in handy

usethis::use_logo("~/Desktop/logo.png")

Back to the top

R Markdown

Useful Websites

Yaml Headers

HTML

---
html_document:
  highlight: pygments
  toc: true
  toc_float: true
  number_sections: true
---

PrettyDoc

---
prettydoc::html_pretty:
  theme: hpstr
  highlight: vignette
  toc: true
  number_sections: true
---

PDF

---
pdf_document:
  highlight: pygments
  toc: true
  toc_depth: 2
  number_sections: true
---

In order to specify multiple outputs, it is as easy as just having both the html and pdf code above, for example. If however, specific instructions are not given for one of the outputs, the user must include output_type: default.

If multiple outputs are specified, and the user does not specify which, the first output will be built. In order to build all the outputs at once, the following must be used

rmarkdown::render("path/to/markdown", output_format = "all")

Figures

  • Figures can be included using the knitr package. This technique is neat as it allows you to decide whether the image should be included or not. For example, you could have the options eval = include_figures and in the beginning of your markdown file define include_figures to be TRUE or FALSE.

    knitr::include_graphics("path/to/image")

    Note that html documents will not accept pdf files; the image must be a png file.

  • Another method to including figures is to include the figure as part of the markdown text You can do this with the following: markdown ![figure name](path/to/figure.png)

  • Often you may need to rescale or adjust the size of the image. The important chunk options are fig.width and fig.height. For more details, see this useful guide.

Back to the top

ggplot2

Useful Packages and Websites

Options

  • My preferred theme settings can be found below

    theme_classic() +
    theme(text = element_text(family = "Times New Roman"),
          plot.title = element_text(hjust = 0.5, size = 10),
          axis.title = element_text(size = 7),
          legend.position = "bottom",
          legend.title = element_text(size = 10),
          legend.text = element_text(size = 8)) +
    labs(x = "", y = "", title = "")
  • The legend can be omitted by adding the following guides(fill = FALSE)

Other Info

  • The viridis package has a popular and neat color scheme. Colors can be added using the ggplot2 functions: scale_colour_viridis_d() and scale_fill_viridis_d(). _d is used for discrete, data _c is used for continuous data, and _b is used to give continuous data breaks. See more on the function’s help page. Options include limits and breaks as well as the option to change between four main color scales: viridis, magma, plasma, and inferno.

Viridis Color Scales

  • If you try to specify a specific font in a ggplot2 object, you may get the following error: Error: font family '<FONT>' not found. In order to get around this, the extrafont package can be utilized by running:

    extrafont::font_import()
  • The plotly package can be used to make interactive graphs. To convert ggplot2 graphs into interactive graphs, the function ggplotly() is used.

Back to the top

Misc

  • In order to ensure that documentation is being built with roxygen2, Go to Tools -> Project Options -> Build Tools and check the box that says “generate documentation with roxygen”

  • There are two progress bars that I have found to be useful and easy to work with: pbapply and progress.

    The following in some code that checks whether pbapply is installed. If it is, it will use a progress bar for a lapply, otherwise, it will use the standard lapply.

    # Function to determine if pbapply is installed. If it is installed, it will
    # display a progress bar
    list_apply <- function(x, fun, ...){
      if (requireNamespace("pbapply", quietly = TRUE)) {
        pbapply::pblapply(x, fun, ...)
        } else {
        lapply(x, fun, ...)
      }
    }

    Using progress is a little more complicated. It utilizes the functions pb$new and pb$tick. An example is below:

    pb <- progress::progress_bar$new(format = "working on it [:bar] :percent eta :eta",
                                     complete = "+", clear = F, 
                                     total = length(tt), width = 60)
    
    try <- lapply(tt, function(x) {
      pb$tick()
      tibble::as_tibble(x)
      })

Back to the top

About

GitHub repository to store various tricks and tips for R

Topics

Resources

Stars

Watchers

Forks