klmr / box

Write reusable, composable and modular R code
https://klmr.me/box/
MIT License
833 stars 47 forks source link

Trailing commas cause problems with functions accessed via `$` #266

Closed kamilzyla closed 2 years ago

kamilzyla commented 2 years ago

Error description

Assume we have the following module.R:

header <- function() {
  tags$span(
    "Hello!",
  )
}

Now box::use(./module) results in Error in box::use(./module) : argument "fun" is missing, with no default. The problem can be fixed by removing tags$ or the trailing comma:

# Works either way
header <- function() tags$span("Hello!")
header <- function() span("Hello!", )

The example was inspired by shiny::tags$span() which accepts trailing commas, but tags don't need to be defined to encounter the issue above.

The issue seemed related to #263 which I reported earlier, but it still occurs on the dev branch (tested after installing with remotes::install_github("klmr/box@9fb08c6")).

R version

platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          1.1                         
year           2021                        
month          08                          
day            10                          
svn rev        80725                       
language       R                           
version.string R version 4.1.1 (2021-08-10)
nickname       Kick Things

‘box’ version

1.1.9000

klmr commented 2 years ago

Thanks for the report. It’s actually unrelated to #263 even though it looks so similar. This error is due to the code that tries to find S3 generics. To do this it traverses the syntax tree of each function, and apparently it fails to account for missing arguments in the case where a function isn’t a simple name. The fix is straightforward, I just hope that I am not missing other cases with this logic.

Once the builds have run, you can install this bugfix build via

install.packages('box', repos = 'https://klmr.r-universe.dev')