modelscript

2020-06-08

{modelscript} is a little RStudio add-in I wrote to help me with modelling. I've been testing out the tidymodels framework for R (fantastic, btw) and I thought it would be handy to be able to create a new .R file with all the steps you'd need for most modelling tasks. You can install it with `remotes::install_github("RobertMyles/modelscript"). A new RStudio add-in will appear in the Addins menu that will create your R modelling script:

For anybody who'd like to make their own RStudio add-in, it was easy to do. In RStudio, you can create a new section in an .R file with control shift R, so I use this basic idea to create a template for all (or most, depending on the application) of the steps you'd typically use in any modelling task. Since we can interact with RStudio itself through rstudioapi, it was quite straightforward to set it up as an RStudio add-in.

Basically, you create a new R package, create an inst/rstudio/addins.dcf file in the package, and fill in the following four entries:

Name:
Description:
Binding:
Interactive:

Mine is:

Name: modelscript
Description: Creates a modelling script template in a new .R file.
Binding: modelscript
Interactive: false

The Binding is the (R) function it calls, in this case a function called modelscript(). This function is quite straightforward, it uses rstudioapi::documentNew() to create a new .R file with the text I wanted used. Once you install modelscript, refresh your Rsession and a new entry on the RStudio Addins menu will appear, which you can click to create the .R file as in the GIF above.

The template itlself contains a prep and a model building section. 'prep' contains subsections for:

  • libraries
  • data
  • EDA
  • tidy
  • visualize

'Build Model' contains subsections for:

  • 1: Initial Split
  • 2: Preprocessing
  • 3: Model Specification
  • 4: Hyperparameter Tuning Specification
  • 5: Bundle into Workflow
  • 6: Cross Validation
  • 7: Tune
  • 8: Explore Tuning Results
  • 9: Finalize Workflow
  • 10: Final Fit
  • 11: Evaluate

Obviously these might not all be used, or other parts might be needed, or for example, the EDA - tidy - visualize part might be repeated, but it's a nice helper for getting up and running. And you could of course modify it for your own use and then make your own package and add-in. 🤓