0

My idea is to create a set of functions (ggplot2) that are some kind of hierarchically structured to create and edit a plot. So there will be a function that defines the grid first, another functions plots the data and several other functions add optional data/lines/polygons.

Here’s a small working example that should clarify my concern:

# a first function that creates the grid and background and
# manipulates some data

plot.func <- function() {

  a <<- 1 
  b <<- c(1,2,3,4)
  z <<- 0:15

  gg <<- ggplot(data=data, aes(x=height, y=weight)) +
          geom_point()
  return(gg)
}

# functions that add something to the plot working with
# objects defined in another functions
add.vline <- function(height) {
  d <<- a+b 
  gg <<- gg + geom_vline(xintercept=height)
  return(gg)
}

add.hline <- function(weight) {
  e <<- z-d
  gg <<- gg + geom_hline(yintercept=weight)
  return(gg)
}

plot.func()

add.vline(60)

add.hline(154)

As your see I (have to?) work with <<- a lot, because each functions only operates in his own (temporary?) environment. And to share the objects each functions has to have access to, I use <<- to make these objects available globally.

I'm quite sure, that there's a right (and nicer) way to do that. Maybe or even probably my approach is totally wrong but I did not find another solution.

UPDATE: Here's an example how I would like to use parameters like style to influence the following functions:

plot.func <- function(style) {

  style <<- style

  gg <- ggplot(data=data, aes(x=height, y=weight)) +
          geom_point()
  return(gg)
}

add.vline <- function(style, height) {
  if (style == 0) {
     gg <- gg + geom_vline(xintercept=height, col=red)
  } else {
     gg <- gg + geom_vline(xintercept=height, col=red)
  }
  return(gg)
}

gg <- plot.func(0)
add.vline(gg, 65)
  • This seems unnecessary because `ggplot` already kind of works like that. Can't you just do `+ geom_vline(xintercept=height)` instead of defining your own `add.vline(height)`? – Alex A. Apr 16 '15 at 16:25
  • 2
    If you have to do it using your own functions, pass the `ggplot` object as a parameter then modify the object outside of the function, like `gg <- add.hline(gg, weight)`, rather than doing a global assignment inside of the function. That's rarely (if ever) a good idea. – Alex A. Apr 16 '15 at 16:27
  • Guess I misread you: I removed the global assignments and put the object `gg` in the argument list - `Object gg not found` – Ander M. Morris Apr 16 '15 at 16:50
  • You'll have to define `gg` before you pass it to a function. – Alex A. Apr 16 '15 at 16:59
  • I see. I did `gg <- plot.func()` and the next functions can access the object `gg` and manupulate the plot. But the other inside-objects (a,b,z,d,e) do not work that way... – Ander M. Morris Apr 16 '15 at 17:13
  • What are you using those other objects for? They aren't referenced anywhere in the code you provided. – Alex A. Apr 16 '15 at 17:19
  • R is a functional language and the style of programming you are trying to do here is not functional. Ideally functions should not have side effects (they should not modify variables outside the scope of the function). – MrFlick Apr 16 '15 at 17:45
  • @AlexA. I added a more specific example. – Ander M. Morris Apr 16 '15 at 20:26
  • @MrFlick I understand that this is not the way R typically works, but is there way to achieve this without making a lot of global assignments? – Ander M. Morris Apr 16 '15 at 20:26
  • [Related](http://stackoverflow.com/q/8419877/3005513) – Alex A. Apr 16 '15 at 20:44

0 Answers0