Site icon R-bloggers

3 R functions that I enjoy

[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.

Straight from my sticky note, three functions that I like a lot, despite their not being new at all… But maybe new to some of you?

sprintf(), the dependency-free but less neat “glue”

Imagine I want to tell you who I am.

I could write

name <- whoami::fullname()
github_username <- whoami::gh_username()
glue::glue("My name is {name} and you'll find me on GitHub as {github_username}!")
#> My name is Maëlle Salmon and you'll find me on GitHub as maelle!

(Maybe the whoami package by Gábor Csárdi is a sub topic of this section?! So handy!)

The code above is very readable. The nice syntax with curly braces is something one finds again in the cli package.1

Now, there’s a dependency-free version of the glue code! Albeit a bit uglier 🤐

name <- whoami::fullname()
github_username <- whoami::gh_username()
sprintf(
  "My name is %s and you'll find me on GitHub as %s!",
  name,
  github_username
)
#> [1] "My name is Maëlle Salmon and you'll find me on GitHub as maelle!"

Sure it’s less readable, since the replacements are identified by their position, but I often find myself using it! I clearly remember seeing it in other people’s code and wondering what that was.

It’s a pattern one finds in other languages: the manual page for the function states “Use C-style String Formatting Commands”, and I know the Hugo equivalent.

append() and its after argument

To append values to a vector, I mostly use c(), but I recently discovered the base R function append() and its after argument that indicates where to include the new values! By default, the values are appended at the end of the vector.

I most recently used append() to create a test fixture.

x <- c("thing", "stuff", "element")
values <- c("bla", "blop")
append(x, values)
#> [1] "thing"   "stuff"   "element" "bla"     "blop"
append(x, values, after = 2)
#> [1] "thing"   "stuff"   "bla"     "blop"    "element"

It’s not a function I use every day, but it can come in handy depending on what you’re doing!

For my fellow XML fans out there, it reminds me of the .where argument of xml2::xml_add_sibling() and xml2::xml_add_child().

servr::httw() to serve a local directory as a website

Do you know about the servr package by Yihui Xie?

Its use case is having a local directory that is the source of a website, and wanting to preview it locally in your browser as if it were served by a real server.

I make use of it when working on the babelquarto package for instance, that builds multilingual Quarto books or websites. In the code of the multilingual books/websites, links to the other language versions are relative so they don’t work if I simply open HTML files in my browser. So, instead, I write servr::httw(<directory-with-the-website-source>).

You can also use servr if you want to preview locally your pkgdown website and be able to use the search function.

There’s servr::httw() that watches changes in the directory, and the more simple servr::httd().

Conclusion

Today I shared about sprintf() for glue-like functionality, append() and its after argument for appending values where you want in a vector, and servr::httw() for serving static files.

< section class="footnotes" role="doc-endnotes">
  1. Curious about cli? Come to this cool rOpenSci coworking session next week! ↩︎

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.
Exit mobile version