0

I'm working on a script that finds text in large PDFs, and I have the bare bones script written out. I'm trying to refactor my code to encapsulate the main while loop in a function, so I can run sapply() on it with a list of the PDFs. Some of the functions that I call within the main loop require values from that main loop: here's a stripped down, pseudo-version of my code:

pdfParse <- function() {
  N <- sample(1:50, 1)*2
  n = N/2; i = 0   

  while (i <= N) {
    what <- whatP(n)
    i = i + length(what)
    if !length(what) {break}
    else {n <- N/2 - i}
  }
  n
}

res <- sample(0:1, N)
r = 1
whatP <- function(t) {
  r = r*2
  if (t%%3) {
    if (t%%5) {
      return(res[(n/r):n])
    } else {
      whatP((rev(t)[1]):(rev(t)[1] + r))
  } else {return(rep(NaN, 2))}
}

So my question is, how do I access the variable n that I've defined in the pdfParse function within the function it calls? Even if it's possible, I'd like to avoid assigning it as a global variable. I've read a bit into closures, but I'm not sure if that's an applicable solution here.

Edit: For clarification, whatP(n) starts out with n as its initial argument, but it's recursive, so depending on whether certain conditions are fulfilled, it may end up operating on a vector that doesn't even include n. but I still want to return the something that depends on the original n I defined in pdfParse

jovianlynxdroid
  • 335
  • 3
  • 5
  • 14
  • How is it accessed within functions that are called FROM WITHIN the function? Isn't it just accessed because your function call is `whatP(n)`? – iod Mar 23 '18 at 19:11
  • From what I see here, you call `whatP(n)` in `pdfParse`. Then in your `whatP` function, change `n` to `t`. That should fix your issue. – smanski Mar 23 '18 at 19:11
  • hmm, maybe this wasn't the best condensation of my code. `whatP(n)` starts out with `n` as its initial argument, but it's recursive, so depending on whether certain conditions are fulfilled, it may end up operating on a vector that doesn't even include `n`. but I still want to return the something that depends on the original `n` I defined in `pdfParse` – jovianlynxdroid Mar 23 '18 at 19:23
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Your `pdfParse ` function has a syntax error in the `if` statement and `N` is not defined. – MrFlick Mar 23 '18 at 19:51
  • Why does this question have the [s4] tag? It doesn't appear to have anything to do with S4. – JDL Mar 27 '18 at 08:38

1 Answers1

1

The simplest (and probably safest, given that your res function is recursive) is to make n an argument of whatP.

whatP <- function(t,n) {
...
}

and then call it from pdfParse with two arguments instead of one.

If for some reason you don't want to do this, then you have two options

(a) you can actually just use n as though it were in scope. R's rules for where it looks for a variable are very different from, say, C(++). In order, R searches in

  • the environment of the current function
  • the environment of its parent (the function that called it)
  • the environment of its parent's parent and so on
  • the global environment
  • the environments of loaded packages, in the same order they appear in search().

Since your function is being called from within the function that defined n, it will find the appropriate value under the second (or third, given it's recursive) bullet.

(b) you can use get with a suitable (negative) value of pos, corresponding to the parent. (Alternatively, use sys.frame). Not recommended here as it's tricky to get right with recursive functions, but can be useful in other situations (and it will bypass any n you might have redefined in the meantime in another, closer, scope).

JDL
  • 1,496
  • 10
  • 18