0
からの出力を返す:ggplotを生成し、関数呼び出しの副作用としてggplotを作成するときに、私は問題を持っている関数呼び出し
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
だから、問題は、私ということですMegaFunctionを使用してプロットを作成することはできませんが、期待通りにMegaFunctionの出力でNicePlotを呼び出すとできます。これはおそらく、関数が呼び出される環境と関係がありますが、わかりません。何か案は?基底Rではこれが機能します。
'返しMegaFunction':'リターン(リスト(plotResult = NicePlot(DF)を、dataResult = DF)) ' – PoGibas
使用'プリント(NicePlot(DF)) ''内部MegaFunction' –
@Marcoのおかげ!それはうまく簡単です。もしあなたが望むなら、それを答えとして入力して、私は "答え"としてマークします。 – Japhir