Learn R Programming

geomultistar (version 1.0.0)

run_geoquery: Get a geographic vector from a query

Description

After defining a query and geographic dimensions, run the query and select the geographic data associated with it to get a geographic data layer as the result.

Usage

run_geoquery(
  dq,
  unify_by_grain = TRUE,
  fact = NULL,
  dimension = NULL,
  attribute = NULL
)

# S3 method for dimensional_query run_geoquery( dq, unify_by_grain = TRUE, fact = NULL, dimension = NULL, attribute = NULL )

Arguments

dq

A dimensional_query object.

unify_by_grain

A boolean, unify facts with the same grain.

fact

A string, name of the fact.

dimension

A string, name of the geographic dimension.

attribute

A string, name of the geographic attribute to consider.

Value

A sf object.

Details

In the case of having several fact tables, as an option, we can indicate if we do not want to unify the facts in the case of having the same grain.

If the result only has one fact table, it is not necessary to provide its name. Nor is it necessary to indicate the name of the geographic dimension if there is only one available.

If no attribute is specified, the geographic attribute of the result with finer granularity is selected.

See Also

Other geo functions: define_geoattribute(), geomultistar(), get_empty_geoinstances()

Examples

Run this code
# NOT RUN {
library(tidyr)
library(starschemar)
library(sf)

gms <- geomultistar(ms = ms_mrs, geodimension = "where") %>%
  define_geoattribute(
    attribute = "city",
    from_layer = usa_cities,
    by = c("city" = "city", "state" = "state")
  )  %>%
  define_geoattribute(
    attribute = "state",
    from_layer = usa_states,
    by = c("state" = "state")
  ) %>%
  define_geoattribute(attribute = "region",
                      from_attribute = "state") %>%
  define_geoattribute(attribute = "all_where",
                      from_layer = usa_nation)

gdq <- dimensional_query(gms) %>%
  select_dimension(name = "where",
                   attributes = c("state", "city")) %>%
  select_dimension(name = "when",
                   attributes = c("when_happened_year", "when_happened_week")) %>%
  select_fact(
    name = "mrs_age",
    measures = c("n_deaths")
  ) %>%
  select_fact(name = "mrs_cause",
              measures = c("pneumonia_and_influenza_deaths", "other_deaths")) %>%
  filter_dimension(name = "when", when_happened_week <= "03") %>%
  filter_dimension(name = "where", state == "MA")

sf <- gdq %>%
  run_geoquery()

# }

Run the code above in your browser using DataLab