Skip to content

drake and gt: gt modifies global environment #297

Description

@gadenbuie

Briefly: Using gt within an Rmd built by drake with rmarkdown::render() results in the error

target report
fail report
Error: Target `report` failed. Call `diagnose(report)` for details. Error message:
  cannot remove bindings from a locked environment. 
Please read the "Self-invalidation" section of the make() help file.

I tracked the error to gt registering the S3 methods for "knitr", "knit_print", "gt_tbl" on package load in zzz.R.

I'm not sure whether this should be addressed in gt or drake (cc @wlandau), but I opened here in case there's an easy fix in gt.

Reprex

Here's a minimal drake plan that would be saved in drake.R:

library(drake)
library(gt)

plan <- drake_plan(
  data = mtcars,
  report = rmarkdown::render(
    knitr_in("report.Rmd"),
    output_file = file_out("report.html")
  )
)

make(plan)

And in report.Rmd:

---
title: "Example GT Report"
output: html_document
---

```{r}
library(drake)
library(gt)
loadd(data)
gt(data)
```

In a fresh session, run

source("make.R")
## target report
## ...
## fail report
## Error: Target `report` failed. Call `diagnose(report)` for details. Error message:
##   cannot remove bindings from a locked environment. 
## Please read the "Self-invalidation" section of the make() help file.

User-level Fixes

A user can add lock_envir = FALSE to drake::make(), but this is not recommended.

A user can also use a custom environment for the drake build, for example by modifying the make() step of drake.R

drake_env <- new.env()
make(plan, envir = drake_env)

but for larger drake projects this is less desirable because the user need to source all scripts tracked by drake into drake_env.

I also noticed that the problem does not occur when using knitr::knit(), so there may be an interaction here with rmarkdown::render(). The downside of this approach is that rmarkdown::render() is required for Rmd v2 documents.

Finally, I confirmed that commenting out the register_s3_method() line does also "resolve" the problem, but I'm not sure what downstream consequences that might cause.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions