See if there is a variable / function in the package?

I am trying to check if a specific variable or function exists in a package. For example, suppose I wanted to check if a function called plot existed in the "graph" of the package.

The following tests: is there a plot function, but not the package it comes from:

 exists('plot', mode='function') 

Or I can verify that there is something called plot in the graphics package, but that does not tell me if this is a function:

 'plot' %in% ls('package:graphics') 

Is there a good way to ask: "is there an object named X in package Y of mode Z"? (essentially, can I limit exists specific package?)

(Yes, I can combine the two lines above to first check that the plot is in graphics and then request the plot mode, but what if I had my own function plot masking graphics::plot ? Can I trust the conclusions exists('plot', mode='function') ?)

Background information: writing tests for my package and checking that various functions are exported. I use the testthat package, which runs tests in an environment where I can see all the internal functions of the package, and was stung by the fact that exists('myfunction', mode='function') returns true, but I actually forgot to export myfunction . I want to check if various functions are exported.

+6
source share
2 answers
  environment(plot) <environment: namespace:graphics> find('+') #[1] "package:base" find('plot') #[1] "package:graphics" find('plot', mode="function") #[1] "package:graphics" 
+5
source

Oh, I found it.

I noticed that ?ls says that the first argument ( 'package:graphics' for me) is also considered an environment. The exists ' where argument has the same documentation as the ls ' name argument, so I assumed that 'package:graphics' might work there too:

 exists('plot', where='package:graphics', mode='function') [1] TRUE # huzzah! 
+7
source

All Articles