The following list of sites are particularly handy in package development:
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")
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()
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)
# Setting up github
usethis::use_github()
usethis::use_github_labels()
usethis::use_tidy_issue_template()
usethis::use_github_links()
# Create a PR
usethis::pr_init()
# Push a PR
usethis::pr_push()
# Finish a PR
usethis::pr_finish()
Badges are written in R Markdown with the following structure:
[![Badge Name]](url to badge image)(url when click on badge)
Some examples:
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()
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")
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_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 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 optionseval = include_figures
and in the beginning of your markdown file defineinclude_figures
to beTRUE
orFALSE
.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
andfig.height
. For more details, see this useful guide.
- ggplot2 Website
- Graph Gallery is a beautiful website with tons of awesome graphs and demos
- ggpubr is a must have for publishing images
- R Color Palettes
- ggsci has several very nice looking themes
- In depth ggplot2 tutorial
-
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)
- The
viridis
package has a popular and neat color scheme. Colors can be added using theggplot2
functions:scale_colour_viridis_d()
andscale_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
, andinferno
.
-
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, theextrafont
package can be utilized by running:extrafont::font_import()
-
The
plotly
package can be used to make interactive graphs. To convertggplot2
graphs into interactive graphs, the functionggplotly()
is used.
-
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
andprogress
.The following in some code that checks whether
pbapply
is installed. If it is, it will use a progress bar for alapply
, otherwise, it will use the standardlapply
.# 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 functionspb$new
andpb$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) })