A couple of days ago, I passed by Sarah Drasner’s Array Explorer. It was through a retweet by Emily Robinson, who proposed the idea of a similar app for working with strings in R. I thought about giving it a try, and I deployed a preliminary Shiny App Stringr Explorer; which is still under development.
In the following sections, I will give a brief about the data extracted from the package documentation to use in the Stringr Explorer app, and I’ll be glad to get better suggestions and contributions.
🎁 A present for people learning JS! Finding the right array method can mean searching the docs one method at a time. I made this resource to help people quickly find what they need!— Sarah Drasner (@sarah_edo) December 27, 2017
codepen: https://t.co/b6G5uzJBl2 pic.twitter.com/yFiyrrDaCV
@robinson_es) December 28, 2017
First of all here’s the Shiny App
Creating a dataset with stringr functions info
I wanted to create a dataframe with stringr functions along with their usage and corresponding examples. I didn’t want to write everything from scratch, so I tried to extract whatever I can get from the documentation.
Package function names
To extract package functions I used:
str_fn_names <- ls("package:stringr")
Function help text
Then I used
utils:::.getHelpFile to get the help text associated with each function (This is what lives inside man/ directory)
str_fn_help <- map(str_fn_names, ~ utils:::.getHelpFile(help(.x, package = "stringr")))
From the help text, I could get the title that corresponds to each function, as follows:
str_fn_title <- map_chr(str_fn_help, ~ tools:::.Rd_get_metadata(.x, "title"))
so for instance, str_extract title will be Extract matching patterns from a string.
This might not be the best thing, but I wanted to start with the original titles, to save the time of writing everything. I said that I can modify them later, if they are not descriptive enough.
Function usage text
Then I got the usage text, removed any empty lines, and
glued the extracted lines.
str_fn_usage <- map(str_fn_help, ~ tools:::.Rd_get_metadata(.x, "usage")) %>% map(~.x[.x!=""]) %>% map(~paste(.x, collapse = "\n")) %>% map(glue)
so here, str_extract usage will be:
str_extract(string, pattern) str_extract_all(string, pattern, simplify = FALSE)
Using the extracted data, I created a dataframe to gather everything:
dat <- tibble(str_fn_names = str_fn_names, str_fn_help = str_fn_help, str_fn_title = str_fn_title, str_fn_usage = str_fn_usage)
But what about the functions examples?
I tried different ways to extract parts of the examples from the help text, but I wanted to have more control and get certain examples. So I had to write them in a dataframe and join with the created
dat. The examples are listed here, and still being updated.
I wasn’t sure if the data structure I used was the best for the problem, but I started with it till I move on and revisit it, or maybe get recommendation from others!
So here’s the preliminary version of the app, I am still working on it. Meanwhile if anyone wants to share suggestions or contribute, you could send me or contribute on Github repo