0

Since R 3.6, I got an error when calling S3 functions from an attached environment (see below). Am I missing something? Thanks in advance for any tip.

Best, G

env <- namespace::makeNamespace('testspace')
de <- 'doit <- function (x, ...) UseMethod("doit",x)
       doit.default <- function(x,...) print(x)
       doit.character <- function(x,...) cat(x,"\n")'

eval(parse(text=de),envir=env)
base::namespaceExport(env, ls(env))
attachNamespace('testspace')

methods("doit")
# [1] doit.character doit.default
doit("lala")
# Error in UseMethod("doit", x) :
#  no applicable method for 'doit' applied to an object of class "character"
8QB
  • 1

1 Answers1

0

After a lot of head scratching (getting bold) a solution:

nn <- 'testspace'
ns <- namespace::makeNamespace(nn)
setNamespaceInfo(ns,"S3methods",matrix(NA_character_,0L,4L)) # see comment below.

de <- 'doit <- function (x, ...) UseMethod("doit",x)
       doit.default <- function(x,...) print(x)
       doit.character <- function(x,...) cat(x,"\n")
       .S3method("doit","character",doit.character)
       .S3method("doit","default"  ,doit.default)'
eval(parse(text=de),envir=ns)

base::namespaceExport(ns, ls(ns))
attachNamespace(nn)

methods("doit")
# [1] doit.character doit.default
doit("lala")
# lala
doit(pi)
# [1] 3.141593

Note that namespace::makeNamespace appears to make a matrix with only 3 columns while four are needed.

8QB
  • 1