2017-10-11 94 views
1

我懷疑這是一個處理NSE的問題。但爲什麼這兩種方法不起作用,我怎麼才能讓他們工作。purrr和ggplot函數不顯示陰謀(NSE問題)

temp1 <- function(x){ 
    iris %>% 
    ggplot(aes(Sepal.Length, Sepal.Width)) + 
    geom_point() + 
    facet_wrap(as.formula(paste("~", x))) 
} 
walk('Species', temp1) 


temp2 <- function(x){ 
    x <- as.name(x) 
    iris %>% 
    ggplot(aes(Sepal.Length, Sepal.Width)) + 
    geom_point() + 
    facet_wrap(~ x) 
} 
walk('Species', temp2) 

回答

0

對我來說,它似乎不是一個NSE問題。如果你讀它?walk說(重點由我加的):

步行()調用.F其副作用和返回原始輸入

嘗試:

t <- walk('Species', temp1) 
t 
#[1] "Species" 

我想你可以得到你想要的,如果你添加一個明確的打印到你的ggplot。例如。將temp1更改爲:

temp1 <- function(x){ 
    print(iris %>% 
     ggplot(aes(Sepal.Length, Sepal.Width)) + 
     geom_point() + 
     facet_wrap(as.formula(paste("~", x)))) 
} 
+0

這很有道理。你可以評論爲什麼做(或者)通過管道不起作用。那是'...%>%print()'。 – student

+2

@student,因爲如果你使用簡單的管道,你會將'facet_wrap()'對象傳遞給'print'。如果你想要管道,你可以用'()'分組整個東西,然後傳給'print'。像'(虹膜%>%...)%>%print()' –