1

This is not necessarily regex per se but how can I extract a function from a package in the following format?

test<-"Aes"
test<-tolower(test)
ggplot2::test

This fails because "test" cannot be recognised as an exported function from ggplot2.

Error: 'test' is not an exported object from 'namespace:ggplot2'

What is a workaround to make this work?

Thanks!

NelsonGon
  • 13,015
  • 7
  • 27
  • 57

1 Answers1

2

You can parse it as text with eval and parse:

eval(parse(text = paste0("ggplot2::", test)))

#function (x, y, ...) 
#{
#    exprs <- rlang::enquos(x = x, y = y, ...)
#    is_missing <- vapply(exprs, rlang::quo_is_missing, logical(1))
#    aes <- new_aes(exprs[!is_missing], env = parent.frame())
#    rename_aes(aes)
#}
#<bytecode: 0x000001458db09718>
#<environment: namespace:ggplot2

You can use match.func if you've already load ggplot2.

match.fun(test)

#function (x, y, ...) 
#{
#    exprs <- rlang::enquos(x = x, y = y, ...)
#    is_missing <- vapply(exprs, rlang::quo_is_missing, logical(1))
#    aes <- new_aes(exprs[!is_missing], env = parent.frame())
#    rename_aes(aes)
#}
#<bytecode: 0x000001458db09718>
#<environment: namespace:ggplot2>
patL
  • 2,259
  • 1
  • 17
  • 38