Command Line Carnac

While being able to use {ggplot2} anywhere is most certainly a compelling use case for making WebR CLI apps, an even more compelling one is access to R’s rich modeling and statistical ecosystem. We’ll go through a toy example of using idioms from the {tidymodels}-verse help us guess resting metabolic rates based on body weight.

The Weight Of It All

We’re using data from the {ISwR} package to create a very basic model (since the point of this chapter is showing you have access to the rich stats tooling in R and not to teach modeling). This can be done in a local R installation or in WebR:

library(ISwR)
library(bundle)
library(tidymodels)

linear_reg() |> 
  set_engine("lm") |> 
  fit(
    metabolic.rate ~ body.weight, 
    data = rmr
  ) |> 
  bundle() |> 
  saveRDS(F
    "./support/ch-09/webr-predict/data/rmr-model.rds"
  )

Even though real-world examples will be more complex, you will need to do a similar serialization dance to save your model so it can be used in WebR.

We need to install the necessary packages in WebR, which we can do via pkgtrap that was introduced in the previous chapter.

$ pkgtrap tidymodels bundle

That ends up installing ~90+ packages. 😲

Now, we could jave just used plain ol’ built-in lm, fit, and predict to avoid dependency Hades, but modern R modeling workflows should really be done in the {tidymodels} ecosystem, and I wanted to make it perfectly clear that we have access to a large percentage of the members of that ecosystem from within WebR.

The accompanying index.mjs in the ch-09 support directory has some extra R code in it to let the caller specify the weight units in the single parameter the CLI takes, and also validate the parameter. We won’t go over that here, but we will take a look at retrieving and using the serialized model:

readRDS(
  "/data/rmr-model.rds"
) |> 
  unbundle() |> 
  predict(
    new_data = data.frame(body.weight = input_weight)
  ) |> 
  getElement(".pred") |> 
  as.integer()

When we run the index.mjs (either directly or via global CLI installation), here’s what wil be output:

$ node index.mjs 175lb
Estimated resting metabolic rate: 1371 kcal/day

If the model ever changes, just replace the rmr-model.rds file, bump up the version number of the CLI Node package, release a new version, and have folks update their installation.

Things To Try

One obvious thing to try is using a different model. Perhaps one that is more representative of a real-world use case.

You will also want to poke around a bit to see which components of the {tidymodels} ecosystem aren’t supported yet.

The kcal/day output is not using commas. Use R or JavaScript formatters to fix that.

More Information

Check out tidymodels if you haven’t taken a dip in that ocean, yet.

Next Up

In the next chapter, we’ll show how to tap into R’s ecosystem of exploratory data analysis tools to start creating CLI utilities others can use without knowing R.