In an R package build, build data from script in data-raw

In a package I am making, I am following advice here and elsewhere and using usethis::use_data_raw() to create a script to make a data frame and then runs usethis::use_data() to put this data frame in the package's data/ directory. See here for an example of script like this.

I can run this script manually of course, and it places the data correctly in data/, but I think it would be better if the package's build script could run this for me every time the package is built. Making a little Makefile to run this code is easy, but where does that Make go and how does it, or how could it, get used by the build process?

Sorry if this question is naive. I'm new to R package development and don't know what kind of build tools package builds use (e.g. if I go to the RStudio menu Build/Build Source Package, I don't know what build scripts are being run). Does the R package build process use Make? If so, where's its Makefile, and how can I customize it?

1 Like

You could run your script from a helper or setup file in your tests directory. See doc here.

Though maybe not a best-practice as you'd have to navigate from the test environment back to your real package environment. This would be crossing the line of keeping your tests isolated.

Each test is run in a clean environment to keep tests as isolated as possible.

@mjandrews I think that is the general pattern. Looking through the nycflights repository as an example, there is no extra build script and the Travis CI file specifies a standard package build process (i.e. the data files aren't always rebuilt).

But of course I don't want to discourage you from improving your build process! If you already know how to write a Makefile, the RStudio configuration part is relatively easy in comparison. First save the Makefile in the root of the package and add it to .Rbuildignore. Then in RStudio go to Build -> Configure Build Tools... and choose Makefile from the dropdown menu. Then when you type Ctrl/Cmd+Shift+B, it will execute your Makefile.

See this previous discussion for other ideas.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.