2

This problem is similar to a previous Stack question, except that the function calls the plyr/dplyr package functions:

Using the example for illustration:

library(dplyr)
df <- data.frame(A=1:10, B=2:11, C=3:12) 
func <- function(name,dat=df){
        output <- dat %>%
                  select(A,name) %>%
                  arrange(desc(name))
}

The following call naturally produces an error:

result <- func(B)

While passing the arg. as character:

result <- func("B")

gives:

Error: All select() inputs must resolve to integer column positions.

Update: Note that func also calls the arrange() function from dplyr. For now, select_ solved 1/2 of the problem as suggested in the comment.

Using arrange_(desc(name)) throws this error: Error in eval(expr, envir, enclos) : incorrect size (1), expecting : 10

Update/Edit: As suggested in the comments, a resolution could be:

func <- function(name,dat=df){
        output <- dat %>%
                  select_(~A,name) %>%
                  arrange_(interp(~desc(var), var=as.name(name)))
}

result <- func(B)

Is it possible to "patch" the following call to ggplot() to func? This is highly-desirable to automate the process:

 plot <-  ggplot(result, aes(x=A, y=B))+
          geom_bar(stat='identity)
Community
  • 1
  • 1
remi
  • 781
  • 2
  • 13
  • 22
  • 4
    Use `select_("A",name)` instead. Also, you don't need that whole piping and `output <-` part just do `func <- function(name, dat = df) select_(dat, "A", name)` – David Arenburg Sep 19 '15 at 19:33
  • 1
    Thanks David, `select_(~A,name) or select_("A",name)` worked. Could you post the answer? – remi Sep 19 '15 at 20:18
  • Ok, added. Though there is probably some place for improvement. – David Arenburg Sep 19 '15 at 20:53
  • Have you seen the update regarding the call to `arrange()`? – remi Sep 19 '15 at 21:08
  • No, just saw now. Btw, why are you using `library(plyr)`? Also, the issue isn't with `arrange_` rather with `desc` – David Arenburg Sep 19 '15 at 21:17
  • 1
    Actually `dplyr`: I load both. Yep, with `desc`. The issue with `arrange_` can be fixed: `arrange_(as.name(name))` using package `lazyeval`. See this [question](http://stackoverflow.com/questions/26724124/standard-evaluation-in-dplyr-summarise-on-variable-given-as-a-character-string). – remi Sep 19 '15 at 21:28
  • 1
    Try `dat %>% select_("A", name) %>% arrange_(interp(~ desc(var), var = as.name(name)))` – talat Sep 19 '15 at 21:31
  • Btw, you could solve this very easily without all this `_` and `interp` mess with just base R using `func <- function(name, dat = df) dat[order(-dat[, name]), c("A", name)]`. – David Arenburg Sep 19 '15 at 21:35
  • Thanks docendo, works perfect. Can you please post the answer. Thanks David, I agree there are different ways. `dplyr` is compact and handy. I'm glad I learned something new through solving this. – remi Sep 19 '15 at 21:45
  • @remi, I closed it as a duplicate of the question you linked to in the comments. – talat Sep 19 '15 at 21:58

0 Answers0