0

In order to aid myself with displaying debugging information, I decided to create the following tiny function that would dynamically switch between displaying data in RStudio's internal data browser and simple character-based output, depending on capabilities of the platform, where my modules are being sourced at:

View <- function (...) {
  if (.Platform$GUI == "RStudio")
    View(...)
  else
    print(...)
}

This function is located, along with other utility functions, in the module <PROJ_HOME>/utils/debug.R. All modules that need these functions, include it via source("../utils/debug.R").

Running my project's code on my Amazon EC2 instance's Linux console is fine. However, running it on the same virtual machine via RStudio Server results in the following error message:

Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?

It seems to me that R gets confused as to which View() function needs to be called. Initially, I assumed that RStudio overloads the utils::View() and I tried to call it explicitly, but it failed. Then I thought that RStudio somehow defines its View() implementation in global environment, so that it just needs to be called View() without package/library reference. However, as I you see, it doesn't work either. Another potential reason of the error might be that I overestimated the "smartness" of R in terms of my use of ... arguments.

So, what is wrong and how to fix it?

Aleksandr Blekh
  • 2,462
  • 4
  • 32
  • 64
  • 1
    How about an explicit utils::View(...) call in your function ? – Karl Forner Mar 06 '14 at 16:08
  • @KarlForner: Just discovered your comment. I've tried `utils::View(...)` call, but it results in the following error and warning: `"Error in .External2(C_dataviewer, x, title) : invalid device In addition: Warning message: In utils::View(...) : unable to open display"`. That's running my code in RStudio Server. – Aleksandr Blekh Mar 11 '14 at 03:33

1 Answers1

1

RStudio hooks the View function. One approach might be to see if anyone has overridden the View function from utils, and to call the override if it exists. What about this?

View <- if (identical(utils::View, View)) print else View
Jonathan
  • 8,497
  • 41
  • 35
  • Thank you for suggestion, Jonathan! However, your approach results in the same error and warning as mentioned in my comment to Karl above. – Aleksandr Blekh Mar 11 '14 at 06:04
  • Jonathan, please disregard my previous comment - I realized that I was testing your changes without clearing R environment (workspace). When I tested your code after properly clearing the workspace, it works well on both platforms exactly as I planned. Great job! Thank you again! Answer accepted! So, from your answer I conclude that R allows overriding functions with omitting (...) arguments syntax. Correct? I would appreciate if you'd take a look at my recent R questions at: http://stackoverflow.com/users/2872891/aleksandr-blekh?tab=questions&sort=newest. – Aleksandr Blekh Mar 11 '14 at 06:22
  • I'm still curious about why my original approach produces a recursion error, while your (essentially very similar) code doesn't... – Aleksandr Blekh Mar 11 '14 at 06:47
  • 1
    Your original approach creates a function that calls `View`. R resolves function names at the moment the function runs. – Jonathan Mar 11 '14 at 16:52
  • 1
    The modified approach works because it copies functions rather than wrapping them. The copy happens before the assignment of `View`, so inside the assignment `View` refers to its earlier definition. – Jonathan Mar 11 '14 at 16:56
  • 1
    One of R's more confusing features is the ability to have several different variables with the same name all present in different environments--to understand what function/variable is being referred to at runtime, it's useful to look at environment bindings if you haven't already: http://adv-r.had.co.nz/Environments.html – Jonathan Mar 11 '14 at 16:59
  • Appreciate your clear explanation! I knew a bit about R environments, but not in the context of functions. Learning something new about R every day... – Aleksandr Blekh Mar 11 '14 at 20:04