Overview of secret and spelling packages

If you didn’t notice this blog is mostly about me talking to future myself with some pieces of advice that I find useful at the moment. It is most obvious with Docker post, but pretty much every post is kinda like that. Another major theme of the blog so far is me looking at cool packages that I think are useful. I’m especially interested in packages that solve a certain problem that is usually not so flashy, but solving those problems make it easy for many people to do what they want to do better and faster.

This brings me to the topic of todays post. More accurately, there are two topics: secret package from Gabor Csardi and spelling package from OpenSci. I’ve talked a bit about secret package in UseR!2017 overview.

secret package

The secret package is designed to create vaults where you can keep your secrets. I imagine you’ve seen many times a post about, for example, scraping tweets from twitter using rTweet package. It’s almost a custom for these posts to say something like this in the beginning:

TWITTER_SECRET <- "YOU_NEED_TO_PUT_YOUR_API_KEY_HERE"

or something similar. This is one way to do it, but secret allows you to do the same thing but more secure. Of course, you still need to save the credentials somewhere, but the advantage is that your code will still run AND you can check-in your work to Git/GitHub/GitLab.

The Readme file explains step-by-step way to set up the vault. I’ll just show how I’ve done it in relation to the next post I want to do that will be about Hacker News. Google provided a public dataset with data about Hacker News posts and comments in BigQuery. You can connect to it using bigrquery package. As an example of how to use it I’ll save a name of the project in the vault. I know that it is not necessary since actual authentication is happening in the browser, so even if you know the name of the project it won’t really get you anywhere since you also need to have access to my Google account to do anything malicious. But it is a good example to show how secret works.

library(secret)
try(local_key(), silent = TRUE)
## Please enter private key passphrase:
## Please enter private key passphrase:

Setting up key itself can be done using ssh utility in Linux. As you can see, I already have one, so we can continue with creating a vault:

vault <- file.path(tempdir(), ".vault")
dir.create(vault)
create_vault(vault)
dir(vault)
## [1] "README"  "secrets" "users"

Now I can add myself to the user of this vault:

key_dir <- file.path("~/.ssh") # this folder is created by default when you create keys
misha_public_key <- file.path(key_dir, "secret_example.pub")
misha_private_key <- file.path(key_dir, "secret_example")
add_user("misha_secret", misha_public_key, vault = vault)

Now I’m adding my “secret” name of the project into the vault:

secret_to_keep <- c(project <- "hacker-news-post")
add_secret("secret_two", secret_to_keep, users = "misha_secret", vault = vault)

At this point I can read it from the vault in any project without showing it to anyone:

get_secret("secret_two", key = misha_private_key, vault = vault)
## [1] "hacker-news-post"

Pretty cool.

spelling package

Just secret package by itself is interesting, but I felt like it is not enough for such a well-established blog such as mine. Moreover, spelling package by itself is not really huge, so I’ve decided to put them together.

As you can probably guess, spelling package checks spelling in packages and posts to make your package as error free as possible (at least grammatically). Since RStudio doesn’t have spell-check by default, package like that makes it easy to still keep track of what you are writing.

So, let’s see how well I’ve done with this post:

spelling::spell_check_files("./2017-11-05-overview-of-secret-and-spelling-packages.Rmd")
##   WORD       FOUND IN
## Balyasin   2017-11-05-overview-of-secret-and-spelling-packages.Rmd:2
## BigQuery   2017-11-05-overview-of-secret-and-spelling-packages.Rmd:28
## Csardi     2017-11-05-overview-of-secret-and-spelling-packages.Rmd:16
## didn       2017-11-05-overview-of-secret-and-spelling-packages.Rmd:14
## doesn      2017-11-05-overview-of-secret-and-spelling-packages.Rmd:72,80
## GitLab     2017-11-05-overview-of-secret-and-spelling-packages.Rmd:26
## Hm         2017-11-05-overview-of-secret-and-spelling-packages.Rmd:80
## Misha      2017-11-05-overview-of-secret-and-spelling-packages.Rmd:2
## OpenSci    2017-11-05-overview-of-secret-and-spelling-packages.Rmd:16
## RStudio    2017-11-05-overview-of-secret-and-spelling-packages.Rmd:72
## todays     2017-11-05-overview-of-secret-and-spelling-packages.Rmd:16,80
## UseR       2017-11-05-overview-of-secret-and-spelling-packages.Rmd:16
## ve         2017-11-05-overview-of-secret-and-spelling-packages.Rmd:16,20,28,70,74

Hm, looks like it doesn’t like contractions… Well, the only error seems to be “todays”. Is it an error? You live and learn :). It is possible to add all the words you think are correct (such as my name, for example). The following command would do it:

spelling::update_wordlist("./2017-11-05-overview-of-secret-and-spelling-packages.Rmd", confirm = TRUE)

I won’t run it since when I render this post it’ll not show the “errors” above. UPDATE: Looks like I was wrong about the command above. It’ll only work for packages, not for individual files. I’m not sure how to add new words from the file. It’s not a big deal, I guess, but rather annoying.