Skip to contents

Try to figure out if user provided latitude / longitude as vectors, data.frame, file, or interactively pick file.

Usage

latlon_from_anything(
  anything,
  lon_if_used,
  interactiveprompt = TRUE,
  invalid_msg_table = FALSE,
  set_invalid_to_na = TRUE
)

Arguments

anything

If missing and interactive mode in RStudio, prompts user for file. Otherwise, this can be a filename (csv or xlsx, with path), or data.frame/ data.table/ matrix, or vector of longitudes (in which case y must be the latitudes). File or data.frame/data.table/matrix must have columns called lat and lon, or names that can be inferred to be that by latlon_infer()

lon_if_used

If anything parameter is a vector of longitudes, lon_if_used must be the latitudes. Ignored otherwise.

interactiveprompt

If TRUE (default) and in interactive mode not running shiny, will prompt user for file if "anything" is missing.

invalid_msg_table

Set to TRUE to add columns "valid" and "invalid_msg" to output

set_invalid_to_na

used by latlon_df_clean()

Value

A data.frame that has at least columns lon and lat (and others if they were in anything), and a logical column called "valid"

Details

Also see closely related function sitepoints_from_any().

This function relies on

read_csv_or_xl() and

latlon_df_clean() which in turn uses latlon_infer() latlon_as.numeric() latlon_is.valid()

A draft function read_and_clean_points() would a more general way to get points,

but is still work in progress... it is similar to latlon_from_anything()

except it also uses these functions:

latlon_from_regid(), latlon_from_programid()

and could eventually use _from_naics() etc.

Even more generally, FIPS and shapefile inputs could be read through a single wrapper function at some point.

See also

sitepoints_from_any() which is like this but also adds ejam_uniq_id column, and see read_csv_or_xl() and latlon_df_clean()

Examples

 latlon_from_anything(testpoints_10)
 latlon_from_anything(testpoints_10$lat, testpoints_10$lon)
 pts = c("33,-100", "32,-101")
 latlon_from_anything(pts)
 pts = data.frame(Longitude = testpoints_10$lon, Latitude = testpoints_10$lat)
 latlon_from_anything(pts)
 pts = data.table(Lat = testpoints_10$lat, Long = testpoints_10$lon)
 latlon_from_anything(pts)
 if (FALSE) { # \dontrun{
 if (interactive()) {
   pts <- latlon_from_anything()
 }} # }
 if (FALSE) { # \dontrun{
 pts = system.file("testdata/latlon/testpoints_10.xlsx", package = "EJAM")
 latlon_from_anything(pts)
 } # }