Environment error when trying to use redefined ggp

2019-07-21 04:53发布

问题:

In order to avoid having to call print() to display graphics output, produced by ggplot2::qplot, I used @cbeleites' answer to this SO question: ggplot's qplot does not execute on sourcing. However, an attempt to run the script generates the following error:

Error in exists(name, envir = env, mode = mode) : argument "env" is missing, with no default

Here's the traceback output:

11 exists(name, envir = env, mode = mode)
10 find_global(scale_name, env, mode = "function")
 9 scales_add_missing(plot, c("x", "y"))
 8 ggplot_build(x)
 7 print.ggplot(p)
 6 print(p) at graphics.R#15
 5 qplot(data[["Project Age"]], data = data, geom = "histogram", binwidth = 1) at edaSourceForge.R#8
 4 eval(expr, envir, enclos)
 3 eval(ei, envir)
 2 withVisible(eval(ei, envir))
 1 source("~/diss-floss/analysis/edaSourceForge.R")

Finally, an additional note on how I run the script. I feel that it's important, as I believe the error is due to lack of visibility of a particular R environment. I run the script analysis/edaSourceForge.R in RStudio, with current working directory import:

source('~/diss-floss/analysis/edaSourceForge.R')

That doesn't seem to be a problem IMHO, as relative paths to the user-defined qplot() from the working directory and from the calling module match ("../utils/graphics.R"), but, of course, I might be wrong here.

My project's directory structure (~ is ruser's home directory):

+- ~/diss-floss (Project's root)
|- ...
|- import (current working directory)
|- analysis (edaSourceForge.R)
|- utils (graphics.R)
|- ...

The redefining function for ggplot::qplot is located in the graphics.R module:

qplot <- function (x, y = NULL, z = NULL, ...) {
  p <- ggplot2::qplot (x = x, y = y, z = z, ...)
  print (p)
}

Actual call to the user-defined qplot function is located in the edaSourceForge.R module

if (!suppressMessages(require(ggplot2))) install.packages('ggplot2')

source("../utils/graphics.R")

Since, as I said, I thought that the error is related to failure to find a particular R environment, I tried to use various environment-related R functions, but so far it wasn't helpful.

Your help and advice will be very much appreciated, as always!