Three useful (to me) R notions

[This article was first published on Maëlle's R blog on Maëlle Salmon's personal website, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Following my recent post on three useful (to me) R patterns, I’ve written down three other things on a tiny sticky note. This post will allow me to throw away this beaten down sticky note, and maybe to show you one element you didn’t know?

nzchar(): “a fast way to find out if elements of a character vector are non-empty strings”

One of my favorite testing technique is the escape hatch strategy, about which I wrote a post on the R-hub blog: you make part of your code responsive to an environment variable, and you locally set that environment variable in your tests. In your code, to determine whether the environment variable has been set to something, anything, you can use the nzchar() function.

nzchar("Hello World")
#> [1] TRUE
nzchar("")
#> [1] FALSE
nzchar(NA)
#> [1] TRUE
nzchar(NA, keepNA = TRUE)
#> [1] NA

# Now with an environment variable
nzchar(Sys.getenv("blopblopblop"))
#> [1] FALSE
withr::with_envvar(
  new = c("blopblopblop" = "bla"),
  nzchar(Sys.getenv("blopblopblop"))
)
#> [1] TRUE

Allow me to mention the obvious: if you’re using nzchar() to examine an environment variable, do not forget Sys.getenv(). I spent too much time looking at the following piece of code last week: nzchar("BABELQUARTO_TESTS_URL") wondering why it didn’t do what I expected it to do… I had to replace it with nzchar(Sys.getenv("BABELQUARTO_TESTS_URL")).

The backports R package, and its README

R is quite stable to say the least, but useful new functions have been added to it over time. For instance, R 3.3.0 saw the birth of trimws(), that trims white space.

trimws(c("useless whitespace ", " all around  "))
#> [1] "useless whitespace" "all around"

To use such functions in your package, you might need to set a dependency on a recent version of R, making it harder for users with restrictive rights on their R install, to use your package. The backports R package can help you avoid to set that dependency: it provides backports for these useful new functions. The README explains how to set up its usage.

I like both the existence of backports and its README because it is a list of these newer functions! I can’t pretend I’m a hipsteR but there are gems I forget or never heard about. Reading the list helps me see what’s there! For similar reasons I find the reference index of the lintr package to be a treasure trove.

Using the first non-missing thing: coalescing

Sometimes you might have an ordered “wish list” in your code: use A, then if A is missing, use B, then if B is missing, use C, and so on and so forth until a default value or error. The pkgdown package for instance has a function called path_first_existing().

I only recently realized this idea is usually called coalescing. dplyr has a coalesce() function.

“Given a set of vectors, coalesce() finds the first non-missing value at each position. It’s inspired by the SQL COALESCE function which does the same thing for SQL NULLs.”

I know close to NULL about SQL, but I did notice the COALESCE function in another query language, SPARQL, for which Lise Vaudor and I are working on a DSL, meaning a helper package, called glitter.

It makes me very happy to learn such an useful new word, or at least it usage in the programming realm.

Conclusion

After writing a bit about nzchar(), the backports package and the idea of coalescence, I can now throw away my sticky note! Let’s see what makes its way onto a new sticky note…

To leave a comment for the author, please follow the link and comment on their blog: Maëlle's R blog on Maëlle Salmon's personal website.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)