Sys.frame in Rmarkdown


#1

In an R script:

temp = function(aParam = 1)  ls(envir=sys.frame(1))
temp()

The return value is:

[1] "aParam"

as expected.

In an Rmarkdown doc,

 ```{r}
 temp = function(aParam = 1)  ls(envir=sys.frame(1))
 temp()
 .```

the return value is a vector of 45 names starting with "base_pandoc_to".
Changing the frame number, I can't find the correct frame anywhere in the range -3 to 20.

Not a problem that can't be overcome, but I am curious to understand what's going on with sys.frame.
(Sorry for the stray dot, seems necessary to get this post to format roughly correctly.)


#2

This works in Rmarkdown:

 ```{r}
temp = function(aParam = 1) {
#  print(length(sys.frames()))
  theframes = sys.frames()
  numframes = length( theframes)
  fr = sys.frame(numframes)
  print (paste(" numframes ", numframes))
  print(ls(envir=fr))
}
temp()
 .```

for which the answer is:

## [1] " numframes  19"
## [1] "aParam"    "fr"        "numframes" "theframes"