0

I would like to have the option of specifying very precisely (exactly, if possible) the behavior of functions. For example,

foo <- function() {}
Env <- new.env(parent=emptyenv())
environment(foo) <- Env
foo()
# Error in { : could not find function "{"
Env$`{` <- base::`{`
environment(foo) <- Env
foo()
# NULL

I know I need to set the environment of foo, and that whatever foo depends upon is defined in that environment (or that environment's parent, or the parent's parent, etc.). But

  • what all must be specified, and
  • what can't be specified (i.e., in what cases am I stuck with default behavior)?

For example, in the following, why is there no error when the call to bar is attempted, when I leave " undefined (what kind of a thing is ", anyway? Or is it ""?).

bar <- function() {""}
Env <- new.env(parent=emptyenv())
Env$`{` <- base::`{`
environment(bar) <- Env
bar()
# [1] ""

My larger exercise is exploring ways to build very large systems with lots of dependencies, but no circular dependencies. I think I will need precise control over function environments in order to guarantee that there are no cycles. I am aiming for a ball of mud of sorts, where I can, given certain discipline, put all the R code I ever write into a single structure (which might not all be in memory, or even on local storage)β€”and, whenever I write new code, have the option of depending on some code I wrote in the past, provided that I have access to that ealier code.


Related posts include: [distinct], [specify], and [environments-enclosures-frames].

jay.sf
  • 60,139
  • 8
  • 53
  • 110
Ana Nimbus
  • 635
  • 3
  • 16
  • 1
    The `"` (quote) symbol in R isn't an operator or function. It's dealt with at the parser level and turned into a character literal value. So there's no behavior to define. You basically will need to define all operators and functions/variables your function will need. It's difficult to even being to image all possible cases for R now and in the future. This isn't how it's intended to be used so it's not really a case that's documented well. It would be better to stick to more specific questions about behavior that can be definitely answered. – MrFlick May 04 '22 at 01:49
  • @MrFlick hmm. Sounds like I need to learn more about how R goes from reading in text to creating the corresponding objects, etc. C (and C++, I think), have well-documented _phases_ that describe this process. Does R have some similar documentation? – Ana Nimbus May 04 '22 at 02:01
  • 1
    There's a whole section on parsing in the R Language Definition: https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Parser – MrFlick May 04 '22 at 02:08

0 Answers0