Introducing the snakecase package

Malte Grosser

2019-05-25

There are many style guides out there which recommend specific naming conventions for programming languages. At 2017’s useR conference Rasmus Bååth showed quite impressively the variety of cases which even exist within base R in his talk “The current state of naming conventions in R”.

However, consistent style is not only about naming new objects.

Import

When you do a data analysis, most of the data already exists and you import it from disk, an API or a database. Here is the first moment in your data analysis when you have to decide if you want to rename your data or leave it as it is.

Let’s say you have some data named in any of the following conventions

string <- c("lowerCamelCase", "ALL_CAPS", "IDontKNOWWhat_thisCASE_is")

You can now easily convert this string for example to snake case via

library(snakecase)
to_snake_case(string)
#> [1] "lower_camel_case"              "all_caps"                     
#> [3] "i_dont_know_what_this_case_is"

Graphics

Whenever you want to construct a graphic and you don’t like your conventions to come up in it, you can easily convert strings to a more humanly readable output like

to_mixed_case(string, sep_out = " ")
#> [1] "lower Camel Case"              "All Caps"                     
#> [3] "I Dont Know What this Case is"

You might have noticed the sep_out argument. This allows you to combine any case with any output separator to create other well known cases like

to_snake_case(string, sep_out = ".")
#> [1] "lower.camel.case"              "all.caps"                     
#> [3] "i.dont.know.what.this.case.is"
to_snake_case(string, sep_out = "-")
#> [1] "lower-camel-case"              "all-caps"                     
#> [3] "i-dont-know-what-this-case-is"

or completely new ones like

to_screaming_snake_case(string, sep_out = "=")
#> [1] "LOWER=CAMEL=CASE"              "ALL=CAPS"                     
#> [3] "I=DONT=KNOW=WHAT=THIS=CASE=IS"

Export

Finally, when you are done with your analysis and want to write data back into a .CSV file or your customers database, which has a camel case convention, you can just use

to_upper_camel_case(string)
#> [1] "LowerCamelCase"          "AllCaps"                
#> [3] "IDontKnowWhatThisCaseIs"

Further information

The snakecase package goes quite deep into the little quirks which arise in automatic case conversion. However, it is well tweaked, to handle almost every edge case in an intuitive and elegant manner.

To get a complete overview of its functionality like other cases, handling of abbreviations, special input characters, different parsing options, transliterations and more, I recommend you to have a look into the quite extensive readme on its github repository.

As the package is relatively small and basically consists of its workhorse function to_any_case(), I can also react quite fast on new issues.

And of course I tweet occasionally about new functionality.

To round this up let me give you one advice about best practices: be aware that automatic case conversion depends on the input string and it is recommended to verify the results. Hence you might want to pipe them into dput() and hard-code name changes instead of blindly trusting the output

library(magrittr)
to_any_case(c("SomeBAdInput", "someGoodInput")) %>% dput()
#> c("some_b_ad_input", "some_good_input")

Happy snakecasing everyone ;)