boomer icon indicating copy to clipboard operation
boomer copied to clipboard

pryr message when displaying an arg value for the first time

Open moodymudskipper opened this issue 3 years ago • 2 comments

from : https://github.com/moodymudskipper/boomer/issues/40

c <- function(x) {
  if (x > 0) wat
}
b <- function(x) c(x)
b <- boomer::rig(b, print_args = TRUE)
a <- function(x) b(3)

a(5)
#> 👇 b
#> 💣 c
#> Registered S3 method overwritten by 'pryr':
#>   method      from
#>   print.bytes Rcpp
#> x :
#> [1] 3
#> 💥 c(x)
#> Error: simpleError/error/condition
#> Error in (function (x) : object 'wat' not found
#> 👆 b

We might find a way to trigger this at a more appropriate time.

On the other hand, we use only function promise_evaled, which has nothing to do with print.bytes(), is unexported, and has really simple code, so it might be cleaner just to have it in the package.

Its cpp code is :

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]`
bool promise_evaled(Symbol name, Environment env) {  
  SEXP object = Rf_findVar(name, env);  return PRVALUE(object) != R_UnboundValue;
}

The R code is :

promise_evaled <- function(name, env) {
  .Call('pryr_promise_evaled', PACKAGE = 'pryr', name, env)
}

I find in the repo the old C code, so we can avoid a Rcpp dependency :

SEXP promise_evaled(SEXP name, SEXP env) {  
  SEXP object = findVar(name, env);  
  return(ScalarLogical(PRVALUE(object) != R_UnboundValue));
}

and the R code would be

.Call("promise_evaled", name, env)

moodymudskipper avatar Jun 21 '21 08:06 moodymudskipper

Seems that it should be simple but I can't get it to work.

  • ran usethis::use_c()
  • created C and R functions
  • ran pkgbuild::compile_dll(force = TRUE, register_routines = TRUE)
  • re-document, re-build

the package builds but promise_evaled() crashes the session.

Keeping in separate branch so we can easily revisit later : https://github.com/moodymudskipper/boomer/tree/promise_evaled

moodymudskipper avatar Jun 21 '21 13:06 moodymudskipper

meanwhile b2539c0bfba2567d15d12f716b802961c0e4847b prints the message early so it doesn't clutter the output :

image

changing milestone since we can live with that

moodymudskipper avatar Jun 21 '21 14:06 moodymudskipper