ich ein Problem habe, wenn eine ggplot als Nebeneffekt einen Funktionsaufruf zu schaffen:generiert ggplot und Rück Ausgabe von Funktionsaufruf
MWE:
library(ggplot2)
dat <- data.frame(x = 1:10,
y = rnorm(10, 11:20),
id = sample(letters[1:3], 10, replace= T))
MegaFunction <- function(df) {
# do something to the data
df$y <- df$y - 10
# plot it
NicePlot(df)
# return output
return(df)
}
NicePlot <- function(x) {
ggplot(x, aes(x = x, y = y, col = id)) +
geom_point()
}
MegaFunction(dat) # returns values, but doesn't plot
out <- MegaFunction(dat) # returns values as out
NicePlot(out) # plots values successfully
So ist das Problem, dass ich kann die Diagramme nicht mit MegaFunction erstellen, aber ich kann es tun, wenn ich NicePlot auf der Ausgabe von MegaFunction (wie erwartet) aufrufen. Das hat wahrscheinlich etwas mit der Umgebung zu tun, aus der die Funktion aufgerufen wird, aber ich kann es nicht herausfinden. Irgendwelche Ideen? In Base R funktioniert das.
Von 'MegaFunction' zurück:' return (Liste (plotResult = NicePlot (df), dataResult = df)) ' – PoGibas
Verwenden' print (NicePlot (df)) '' innen MegaFunction' –
@Marco Dank! Das funktioniert und ist schön und einfach. Wenn Sie möchten, können Sie es als eine Antwort eingeben, die ich als "die Antwort" markieren werde. – Japhir