Skip to content

Commit

Permalink
fix more bugs!
Browse files Browse the repository at this point in the history
  • Loading branch information
hrodmn committed Feb 27, 2024
1 parent 77e4190 commit b36a579
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 33 deletions.
16 changes: 8 additions & 8 deletions R/query_FIADB.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ query_table <- function(table_name, plt_cns, con) {
dbplyr::in_schema(dbplyr::sql("public"), dbplyr::sql(table_name))
)

plt_cn_field <- dplyr::case_when(
"plt_cn" %in% colnames(tab) ~ "plt_cn",
!"plt_cn" %in% colnames(tab) ~ "cn"
)

tab |>
dplyr::filter(.data[[plt_cn_field]] %in% !!plt_cns) |>
dplyr::collect()
if ("plt_cn" %in% colnames(tab)) {
tab <- dplyr::filter(
tab,
.data$plt_cn %in% !!plt_cns
)
}

dplyr::collect(tab)
}
16 changes: 0 additions & 16 deletions tests/testthat/test-tidy_FIA.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ test_that("tidy_fia produces expected output when postgis = TRUE", {
expect_true(
all(fia_data[["cond"]][["plt_cn"]] %in% fia_data[["plot"]][["cn"]])
)

expect_true(
all(fia_data[["survey"]][["cn"]] %in% fia_data[["plot"]][["cn"]])
)
})

test_that("tidy_fia produces expected output when postgis = FALSE", {
Expand Down Expand Up @@ -111,10 +107,6 @@ test_that("tidy_fia produces expected output when postgis = FALSE", {
expect_true(
all(fia_data[["cond"]][["plt_cn"]] %in% fia_data[["plot"]][["cn"]])
)

expect_true(
all(fia_data[["survey"]][["cn"]] %in% fia_data[["plot"]][["cn"]])
)
})

test_that("tidy_fia produces expected output when aoi is NULL and postgis = TRUE", {
Expand Down Expand Up @@ -170,10 +162,6 @@ test_that("tidy_fia produces expected output when aoi is NULL and postgis = TRUE
expect_true(
all(fia_data[["cond"]][["plt_cn"]] %in% fia_data[["plot"]][["cn"]])
)

expect_true(
all(fia_data[["survey"]][["cn"]] %in% fia_data[["plot"]][["cn"]])
)
})

test_that("tidy_fia produces expected output when aoi is NULL and postgis = FALSE", {
Expand Down Expand Up @@ -230,8 +218,4 @@ test_that("tidy_fia produces expected output when aoi is NULL and postgis = FALS
expect_true(
all(fia_data[["cond"]][["plt_cn"]] %in% fia_data[["plot"]][["cn"]])
)

expect_true(
all(fia_data[["survey"]][["cn"]] %in% fia_data[["plot"]][["cn"]])
)
})
13 changes: 4 additions & 9 deletions vignettes/white_spruce.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ aoi <- northern_counties |>
fia_data <- tidy_fia(
aoi = aoi,
table_names = c("plot", "tree"),
table_names = c("plot", "tree", "county"),
postgis = TRUE
)
```
Expand All @@ -120,11 +120,6 @@ ref_species <- read_ref_table("REF_SPECIES") |>
select(spcd, common_name, sftwd_hrdwd)
```

The tree table has codes for state and county but the readable names are stored in a separate table ("COUNTY") which is stored at the state-level but is also available for the entire US.
```{r}
# ref_county <- read_ref_table("COUNTY") |>
# select(statecd, countycd, county = countynm)
```

## Analysis

Expand Down Expand Up @@ -164,8 +159,8 @@ white_spruce <- fia_data[["tree"]] |>
common_name == "white spruce",
statuscd == 1, !is.na(dia), !is.na(ht)
) |>
left_join(plot_stats, by = "plt_cn") #|>
# left_join(ref_county, by = c("statecd", "countycd"))
left_join(plot_stats, by = "plt_cn") |>
left_join(fia_data[["county"]], by = c("statecd", "countycd"))
```

To get a sense for height/diameter relationships in white spruce I will make a graph that shows the trend by forest type and county.
Expand All @@ -185,7 +180,7 @@ white_spruce |>
That graph is somewhat hard to read and does not show any obvious differences in height:diameter relationships so I will try one graph with a separate line for each county.
```{r county_plot}
white_spruce |>
ggplot(aes(x = dia, y = ht, group = countycd, color = factor(countycd))) +
ggplot(aes(x = dia, y = ht, group = countynm, color = countynm)) +
geom_smooth() +
labs(
x = "DBH (in)",
Expand Down

0 comments on commit b36a579

Please sign in to comment.