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
그래서 문제는 I MegaFunction을 사용하여 플롯을 만들 수는 없지만 MegaFunction의 출력에서 NicePlot을 호출 할 때 (예상대로) 할 수 있습니다. 이것은 아마도 함수가 호출 된 환경과 관련이 있습니다. 그러나 그것을 이해할 수는 없습니다. 어떤 아이디어? 베이스 R에서는 이것이 가능합니다.
'반환 MegaFunction' : '리턴 (목록 (plotResult = NicePlot (DF)를 dataResult = DF))' – PoGibas
사용'인쇄 (NicePlot (DF))'안에'MegaFunction' –
@Marco 감사합니다! 그건 효과적이고 간단합니다. 원한다면 "대답"으로 표시 할 답변으로 입력 할 수 있습니다. – Japhir