When creating a custom constraint, row-indexing by the redist_map object throws an error.
For example, this custom constraint indexing an individual column by current district works fine:
library(redist)
iowa_map <- redist_map(iowa, existing_plan = cd_2010, pop_tol = 0.05)
dist_constr <- redist_constr(iowa_map) %>%
# Example: custom constraint for even population in districts
add_constr_custom(strength = 5000, function(plan, distr){
pop <- iowa_map$pop[plan==distr]
if (sum(pop) %% 2 == 0) {
1
} else {
0
}
})
But if you try to change pop to row index the entire redist_map directly, you get an error:
# Example: custom constraint to ensure even population in districts
dist_constr <- redist_constr(iowa_map) %>%
# Example: custom constraint to ensure even population in districts
add_constr_custom(strength = 5000, function(plan, distr){
iowa_sub <- iowa_map[plan==distr,]
if (sum(iowa_sub$pop) %% 2 == 0) {
1
} else {
0
}
})
#> [1] "iowa_map"
#> Error in rlang::env_has(env, name): attempt to use zero-length variable name
When creating a custom constraint, row-indexing by the
redist_map
object throws an error.For example, this custom constraint indexing an individual column by current district works fine:
But if you try to change
pop
to row index the entireredist_map
directly, you get an error: