I'm trying to generate latitude and longitude in R using the localgeo
package from function1
in my own package, however, I can't figure out how to do it without explicitly loading the package.
How can I enable a function within package1 easy access to package2's hidden environments and their objects?
### fails
data <- data.frame(City = c("New York", "Miami", "Los Angeles"),
State = c("NY", "FL", "CA")
data <- cbind(data, localgeo::geocode(data[["City"]], data[["State"]]))
Error in UseMethod("tbl_vars") :
no applicable method for 'tbl_vars' applied to an object of class "NULL"
### works
library(localgeo)
data <- data.frame(City = c("New York", "Miami", "Los Angeles"),
State = c("NY", "FL", "CA")
data <- cbind(data, geocode(data[["City"]], data[["State"]]))
I assume the problem is the function localgeo::geocode() looks like
function (city, state)
{
data.frame(city = as.character(city), state = as.character(state),
stringsAsFactors = FALSE) %>% left_join(.localgeo$geo_db,
by = c("city", "state")) %>% select(lon, lat)
}
<environment: namespace:localgeo>
and I don't know how to make .localgeo
available to my function.
Updates: It seems that using Depends: localgeo
in the DESCRIPTION
file of my package not only "loads" but "attaches" localgeo
and thus I can run the localgeo::geocode()
without a problem. Of course,
Unless there is a good reason otherwise, you should always list packages in Imports not Depends. That’s because a good package is self-contained, and minimises changes to the global environment (including the search path). The only exception is if your package is designed to be used in conjunction with another package. For example, the analogue package builds on top of vegan. It’s not useful without vegan, so it has vegan in Depends instead of Imports. Similarly, ggplot2 should really Depend on scales, rather than Importing it. Namespace
Is it possible to access hidden environments in package2
in my package1
without "attaching" package2
?
To those seeking an MWE, the challenge: solve this MWE