Could you help me understand how Quasiquotation works? I'm using map and count function but it doesn't seem to work properly.
First try:
map(names(starwars),~starwars %>% count(.x))
Error: Column `.x` is unknown
Second try:
map(names(starwars),~starwars %>% count(!!.x))
#not useful [[1]]
# A tibble: 1 x 2
`"name"` n
<chr> <int>
1 name 87
[[2]]
# A tibble: 1 x 2
`"height"` n
<chr> <int>
1 height 87
Third try:
map(names(starwars),~starwars %>% count(!!!.x))
# the same
Another example (working on functions):
If I want to make a function that takes a list and the changes each element of the list with regard to the previous element on that list:
my_list <- list("a" =1 , "b" = 2, "c" = 3)
# this obviously is not working (list + number)
> my_list+1
Error in my_list + 1 : non-numeric argument to binary operator
# this is a bit strange
my_list %>% map(~+1)
#this works fine
my_list %>% map(+1)
# as this
my_list %>% map(~.x+1)
# moving on to add the previous element to the next element
imap(my_list, my_list[[.y +1]] := .x %>% +1)
Error in `:=`(my_list[[.y + 1]], .x %>% +1) : could not find function ":="
# wrong eval 1?
imap(my_list, my_list[[.y +1]] <- .x %>% +1)
Error in eval(lhs, parent, parent) : object '.x' not found
# wrong eval 2?
imap(my_list, my_list[[.y +1]] <- !!.x %>% +1)
Error in eval(lhs, parent, parent) : object '.x' not found
# wrong symbol 1?
imap(my_list, my_list[[.y +1]] = .x %>% +1)
Error: unexpected '=' in "imap(my_list, my_list[[.y +1]] ="