Skip to content

Commit

Permalink
knfi 1.0.1 code change
Browse files Browse the repository at this point in the history
  • Loading branch information
SYOUNG9836 committed Nov 19, 2024
1 parent 11355bc commit 7522e40
Show file tree
Hide file tree
Showing 29 changed files with 854 additions and 509 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
^Meta$
^TinyTeX\.zip$
^CRAN-SUBMISSION$
^\.RDataTmp$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.Rhistory
.RData
.Ruserdata
.RDataTmp
*.zip
inst/doc
docs
Expand Down
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: knfi
Title: Analysis of Korean National Forest Inventory Database
Version: 1.0.0
Version: 1.0.1
Maintainer: Sinyoung Park <[email protected]>
Authors@R:
c(person(given = "Sinyoung", family = "Park", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-3658-0935")),
Expand Down Expand Up @@ -59,7 +59,9 @@ Imports:
sp,
cowplot,
ggpubr,
drat
drat,
purrr,
scales
Depends:
R (>= 3.6)
Suggests:
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ import(vegan)
importFrom(cowplot,plot_grid)
importFrom(data.table,rbindlist)
importFrom(magrittr,"%>%")
importFrom(rlang,caller_env)
importFrom(rlang,env)
importFrom(rlang,env_parent)
importFrom(rlang,eval_tidy)
importFrom(rlang,parse_exprs)
importFrom(rlang,sym)
importFrom(rlang,syms)
importFrom(scales,hue_pal)
importFrom(stats,sd)
importFrom(stats,setNames)
importFrom(stats,var)
importFrom(tidyr,spread)
importFrom(utils,install.packages)
Expand Down
15 changes: 13 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# knfi 1.0.0
# knfi 1.0.1
## Bug fixes
* Fixed a bug in `summary_nfi` and `biomass_nfi` where functions were not working when `largetree_area = FALSE`.
* Fixed a filtering bug in `filter_nfi` where plots with the same subplot name in different cycles were not properly filtered at sublayers - resolved by implementing simultaneous filtering using both subplot and cycle.

## Improvements
* `read_nfi`: Added support for additional `list.files()` arguments (e.g., `pattern`, `recursive`) to provide more flexibility in file selection.
* `summary_nfi`, `iv_nfi`, `diversity_nfi`, `biomass_nfi`, `cwd_biomass_nfi`: Added `continuousplot` parameter to filter for plots with measurements across all NFI cycles.
* `tsvis_nfi`: Added layout control parameters (`ncol`, `nrow`) for customizing plot visualization and `top` parameter for selecting number of top species to display.
*`nfi_col`: Renamed from `col_name` to provide a more descriptive name

* Initial CRAN submission.

# knfi 1.0.0
* Initial CRAN submission(2024-10-10).
92 changes: 59 additions & 33 deletions R/biomass_nfi.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ bm_df <- function(data){
(CONDEC_CLASS_CD ==0) ~ "OTHER_CON", # Other conifer species
TRUE ~ as.character(NA)

# Bambusoideae (대나무) ??

))


output <- left_join(output, bio_coeff, by= c("species_bm" ="SPCD") )
output <- left_join(output, bio_coeff[,-1], by= c("species_bm" ="SPCD") )

## Calculating aboveground biomass--------------------------------------------------------------
## species ~ Volume*(Wood density)*(Biomass expansion factor)-------------------------------
Expand Down Expand Up @@ -98,6 +96,7 @@ bm_df <- function(data){
#' @param byplot : A logical flag (default FALSE); if TRUE, calculates statistics for each plot separately. If FALSE, calculates for the entire dataset or groups specified by `plotgrp` and `treegrp`.
#' @param plotgrp : A character vector; variables from 'plot' tables for grouping. Use \code{c()} to combine multiple variables.
#' @param treegrp : A character vector; variables from 'tree' tables for grouping. Use \code{c()} to combine multiple variables.
#' @param continuousplot : A logical flag (default TRUE); if TRUE, includes only plots that have been continuously measured in all NFI cycles (5th, 6th, etc.). If FALSE, includes plots regardless of missing cycle measurements.
#' @param strat : A character vector; the variable used for post-stratification. In the National Forest Inventory of Korea, it is typically used by forest type.
#' @param clusterplot : A logical flag (default FALSE); if TRUE, treats each cluster plot as a single unit. If FALSE, calculates for each subplot separately.
#' @param largetreearea : A logical flag (default TRUE); if TRUE, includes large tree survey plots in the analysis. If FALSE, only uses standard tree plots.
Expand All @@ -119,10 +118,10 @@ bm_df <- function(data){
#' data("nfi_donghae")
#'
#' # Basic usage
#' biomass <- biomass_nfi(nfi_donghae)
#' biomass <- biomass_nfi(nfi_donghae, continuousplot = TRUE)
#'
#' # Calculate biomass by administrative district
#' district_biomass <- biomass_nfi(nfi_donghae, plotgrp = "SGG")
#' district_biomass <- biomass_nfi(nfi_donghae, plotgrp = "SGG", continuousplot=TRUE)
#'
#' # Calculate biomass for each plot
#' plot_biomass <- biomass_nfi(nfi_donghae, byplot = TRUE)
Expand All @@ -134,7 +133,7 @@ bm_df <- function(data){
#' @export


biomass_nfi <- function(data, byplot= FALSE, plotgrp=NULL, treegrp= NULL, strat="FORTYP_SUB", clusterplot=FALSE, largetreearea=TRUE, stockedland=TRUE, talltree=TRUE){
biomass_nfi <- function(data, byplot= FALSE, plotgrp=NULL, treegrp= NULL, continuousplot=FALSE, strat="FORTYP_SUB", clusterplot=FALSE, largetreearea=TRUE, stockedland=TRUE, talltree=TRUE){


## error message--------------------------------------------------------------
Expand All @@ -147,10 +146,12 @@ biomass_nfi <- function(data, byplot= FALSE, plotgrp=NULL, treegrp= NULL, strat=


if (clusterplot){
if(strat=="FORTYP_SUB"){
warning("When the param 'clusterplot' is set to TRUE, param 'strat' uses FORTYP_CLST (the forest type for the cluster plot) instead of FORTYP_SUB (the forest type for each subplot).")
if(!is.null(strat) && strat=="FORTYP_SUB"){
stop("When the param 'clusterplot' is set to TRUE, param 'strat' uses FORTYP_CLST (the forest type for the cluster plot) instead of FORTYP_SUB (the forest type for each subplot).")
}

strat <- c("FORTYP_CLST")
if(!is.null(plotgrp) && plotgrp=="FORTYP_SUB"){
stop("When the param 'clusterplot' is set to TRUE, param 'plotgrp' uses FORTYP_CLST (the forest type for the cluster plot) instead of FORTYP_SUB (the forest type for each subplot).")
}
}

Expand Down Expand Up @@ -202,6 +203,24 @@ biomass_nfi <- function(data, byplot= FALSE, plotgrp=NULL, treegrp= NULL, strat=
if(talltree){
data$tree <- data$tree %>% filter(WDY_PLNTS_TYP_CD == "1")
}

if ("FORTYP_SUB" %in% names(data$plot)) {
data$plot <- data$plot %>% filter(!is.na(FORTYP_SUB))
}

if(continuousplot){

all_cycle <- unique(data$plot$CYCLE)
samples_with_all_cycle <- data$plot %>%
filter(!is.na(FORTYP_SUB)) %>%
group_by(SUB_PLOT) %>%
filter(all(all_cycle %in% CYCLE)) %>%
distinct(SUB_PLOT) %>%
pull(SUB_PLOT)

data <- filter_nfi(data, c("plot$SUB_PLOT %in% samples_with_all_cycle"))

}

df <- left_join(data$tree[,c('CLST_PLOT', 'SUB_PLOT',"CYCLE", 'WDY_PLNTS_TYP_CD','SP', 'SPCD',
'CONDEC_CLASS_CD', 'DECEVER_CD', 'DBH', 'VOL_EST', 'LARGEP_TREE', treegrp)],
Expand All @@ -217,11 +236,12 @@ biomass_nfi <- function(data, byplot= FALSE, plotgrp=NULL, treegrp= NULL, strat=

if(!largetreearea){
df <- df %>% filter(df$LARGEP_TREE == "0")
df$largetree <- 0
}else{
df$largetree <- ifelse(df$DBH>=30, 1, 0)
df$largetree_area <- 0.08 - ((df$NONFR_INCL_AREA_LARGEP*10)/10000) # unit m2/10
}

df$largetree_area <- 0.08 - ((df$NONFR_INCL_AREA_LARGEP*10)/10000) # unit m2/10
df$tree_area <- 0.04 - ((df$NONFR_INCL_AREA_SUBP*10)/10000)

df <- bm_df(df)
Expand Down Expand Up @@ -254,26 +274,25 @@ biomass_nfi <- function(data, byplot= FALSE, plotgrp=NULL, treegrp= NULL, strat=
group_by(CYCLE, !!plot_id, INVYR) %>%
summarise(largetree_area = sum(largetree_area, na.rm=TRUE),
tree_area= sum(tree_area, na.rm=TRUE),.groups = 'drop')

bm_temp <- df %>%
group_by(CYCLE, !!plot_id, INVYR, largetree, !!!plotgrp, !!!treegrp, !!strat) %>%
group_by(CYCLE, !!plot_id, INVYR, largetree, !!!plotgrp, SP, !!strat) %>%
summarise(volume_m3 = sum(VOL_EST, na.rm=TRUE),
biomass_ton = sum(T_biomass, na.rm=TRUE),
AG_biomass_ton = sum(AG_biomass, na.rm=TRUE),
carbon_stock_tC = sum(carbon_stock, na.rm=TRUE),
co2_stock_tCO2 = sum(co2_stock, na.rm=TRUE),.groups = 'drop')

bm_temp <- full_join(bm_temp, plot_area, by=c('CYCLE', 'INVYR', quo_name(plot_id)))

condition <- (names(bm_temp) %in% c("volume_m3","biomass_ton","AG_biomass_ton","carbon_stock_tC","co2_stock_tCO2"))


if(!largetreearea){ # 1.1.1 Biomass calculation by cluster plots excluding large tree survey plots

condition_ha <- c("volume_m3_ha","biomass_ton_ha","AG_biomass_ton_ha","carbon_stock_tC_ha","co2_stock_tCO2_ha")
bm_temp[condition_ha] <- NA
bm_temp <- as.data.frame(bm_temp)

condition <- (names(bm_temp) %in% c("volume_m3","biomass_ton","AG_biomass_ton","carbon_stock_tC","co2_stock_tCO2"))
condition_ha <- (names(bm_temp) %in% c("volume_m3_ha","biomass_ton_ha","AG_biomass_ton_ha","carbon_stock_tC_ha","co2_stock_tCO2_ha"))

bm_temp[condition_ha] <-
Expand All @@ -284,11 +303,13 @@ biomass_nfi <- function(data, byplot= FALSE, plotgrp=NULL, treegrp= NULL, strat=

bm_temp[condition] <- NULL
bm_temp$tree_area <- NULL
bm_temp$largetreearea <- NULL
bm_temp$largetree_area <- NULL


}else{ # 1.1.2 Biomass calculation by cluster plots including large tree survey plots

condition <- (names(bm_temp) %in% c("volume_m3","biomass_ton","AG_biomass_ton","carbon_stock_tC","co2_stock_tCO2"))

bm_temp[condition] <-
lapply(bm_temp[condition], function(x) ifelse(bm_temp$largetree == 1,
x/(bm_temp$largetree_area),
Expand Down Expand Up @@ -319,15 +340,13 @@ biomass_nfi <- function(data, byplot= FALSE, plotgrp=NULL, treegrp= NULL, strat=
carbon_stock_tC = sum(carbon_stock, na.rm=TRUE),
co2_stock_tCO2 = sum(co2_stock, na.rm=TRUE),.groups = 'drop')


condition <- (names(bm_temp) %in% c("volume_m3","biomass_ton","AG_biomass_ton","carbon_stock_tC","co2_stock_tCO2"))

if(!largetreearea){ # 1.2.1 Biomass calculation by subplots excluding large tree survey plots

condition_ha <- c("volume_m3_ha","biomass_ton_ha","AG_biomass_ton_ha","carbon_stock_tC_ha","co2_stock_tCO2_ha")
bm_temp[condition_ha] <- NA
bm_temp <- as.data.frame(bm_temp)

condition <- (names(bm_temp) %in% c("volume_m3","biomass_ton","AG_biomass_ton","carbon_stock_tC","co2_stock_tCO2"))
condition_ha <- (names(bm_temp) %in% c("volume_m3_ha","biomass_ton_ha","AG_biomass_ton_ha","carbon_stock_tC_ha","co2_stock_tCO2_ha"))

bm_temp[condition_ha] <-
Expand All @@ -338,12 +357,14 @@ biomass_nfi <- function(data, byplot= FALSE, plotgrp=NULL, treegrp= NULL, strat=

bm_temp[condition] <- NULL
bm_temp$tree_area <- NULL
bm_temp$largetreearea <- NULL
bm_temp$largetree_area <- NULL



}else{ # 1.2.2 Biomass calculation by subplots including large tree survey plots

condition <- (names(bm_temp) %in% c("volume_m3","biomass_ton","AG_biomass_ton","carbon_stock_tC","co2_stock_tCO2"))

bm_temp[condition] <-
lapply(bm_temp[condition], function(x) ifelse(bm_temp$largetree == 1,
x/(bm_temp$largetree_area),
Expand Down Expand Up @@ -559,10 +580,12 @@ biomass_tsvis <- function(data, plotgrp=NULL, treegrp=NULL, strat="FORTYP_SUB",
}

if (clusterplot){
if(strat=="FORTYP_SUB"){
warning("When the param 'clusterplot' is set to TRUE, param 'strat' uses FORTYP_CLST (the forest type for the cluster plot) instead of FORTYP_SUB (the forest type for each subplot).")

strat <- c("FORTYP_CLST")
if(!is.null(strat) && strat=="FORTYP_SUB"){
stop("When the param 'clusterplot' is set to TRUE, param 'strat' uses FORTYP_CLST (the forest type for the cluster plot) instead of FORTYP_SUB (the forest type for each subplot).")
}

if(!is.null(plotgrp) && plotgrp=="FORTYP_SUB"){
stop("When the param 'clusterplot' is set to TRUE, param 'plotgrp' uses FORTYP_CLST (the forest type for the cluster plot) instead of FORTYP_SUB (the forest type for each subplot).")
}
}

Expand Down Expand Up @@ -598,6 +621,10 @@ biomass_tsvis <- function(data, plotgrp=NULL, treegrp=NULL, strat="FORTYP_SUB",
data$tree <- data$tree %>% filter(WDY_PLNTS_TYP_CD == "1")
}

if ("FORTYP_SUB" %in% names(data$plot)) {
data$plot <- data$plot %>% filter(!is.na(FORTYP_SUB))
}

df <- left_join(data$tree[,c('CLST_PLOT', 'SUB_PLOT',"CYCLE", 'WDY_PLNTS_TYP_CD','SP', 'SPCD',
'CONDEC_CLASS_CD', 'DECEVER_CD', 'DBH', 'VOL_EST', 'LARGEP_TREE', treegrp )],
data$plot[,c('CLST_PLOT', 'SUB_PLOT', "CYCLE", 'INVYR', "LAND_USE", "LAND_USECD",
Expand All @@ -611,12 +638,12 @@ biomass_tsvis <- function(data, plotgrp=NULL, treegrp=NULL, strat="FORTYP_SUB",

if(!largetreearea){
df <- df %>% filter(df$LARGEP_TREE == "0")
df$largetree <- 0
}else{
df$largetree <- ifelse(df$DBH>=30, 1, 0)
df$largetree_area <- 0.08 - ((df$NONFR_INCL_AREA_LARGEP*10)/10000) # unit m2/10
}


df$largetree_area <- 0.08 - ((df$NONFR_INCL_AREA_LARGEP*10)/10000) # unit m2/10
df$tree_area <- 0.04 - ((df$NONFR_INCL_AREA_SUBP*10)/10000)

df <- bm_df(df)
Expand Down Expand Up @@ -660,15 +687,14 @@ biomass_tsvis <- function(data, plotgrp=NULL, treegrp=NULL, strat="FORTYP_SUB",

bm_temp <- full_join(bm_temp, plot_area, by=c('CYCLE', 'INVYR', quo_name(plot_id)))

condition <- (names(bm_temp) %in% c("volume_m3","biomass_ton","AG_biomass_ton","carbon_stock_tC","co2_stock_tCO2"))


if(!largetreearea){ # 1.1.1 Biomass calculation by cluster plots excluding large tree survey plots

condition_ha <- c("volume_m3_ha","biomass_ton_ha","AG_biomass_ton_ha","carbon_stock_tC_ha","co2_stock_tCO2_ha")
bm_temp[condition_ha] <- NA
bm_temp <- as.data.frame(bm_temp)

condition <- (names(bm_temp) %in% c("volume_m3","biomass_ton","AG_biomass_ton","carbon_stock_tC","co2_stock_tCO2"))
condition_ha <- (names(bm_temp) %in% c("volume_m3_ha","biomass_ton_ha","AG_biomass_ton_ha","carbon_stock_tC_ha","co2_stock_tCO2_ha"))

bm_temp[condition_ha] <-
Expand All @@ -679,11 +705,12 @@ biomass_tsvis <- function(data, plotgrp=NULL, treegrp=NULL, strat="FORTYP_SUB",

bm_temp[condition] <- NULL
bm_temp$tree_area <- NULL
bm_temp$largetreearea <- NULL
bm_temp$largetree_area <- NULL


}else{ # 1.1.2 Biomass calculation by cluster plots including large tree survey plots

condition <- (names(bm_temp) %in% c("volume_m3","biomass_ton","AG_biomass_ton","carbon_stock_tC","co2_stock_tCO2"))
bm_temp[condition] <-
lapply(bm_temp[condition], function(x) ifelse(bm_temp$largetree == 1,
x/(bm_temp$largetree_area),
Expand Down Expand Up @@ -713,15 +740,13 @@ biomass_tsvis <- function(data, plotgrp=NULL, treegrp=NULL, strat="FORTYP_SUB",
carbon_stock_tC = sum(carbon_stock, na.rm=TRUE),
co2_stock_tCO2 = sum(co2_stock, na.rm=TRUE),.groups = 'drop')


condition <- (names(bm_temp) %in% c("volume_m3","biomass_ton","AG_biomass_ton","carbon_stock_tC","co2_stock_tCO2"))

if(!largetreearea){ # 1.2.1 Biomass calculation by subplots excluding large tree survey plots

condition_ha <- c("volume_m3_ha","biomass_ton_ha","AG_biomass_ton_ha","carbon_stock_tC_ha","co2_stock_tCO2_ha")
bm_temp[condition_ha] <- NA
bm_temp <- as.data.frame(bm_temp)

condition <- (names(bm_temp) %in% c("volume_m3","biomass_ton","AG_biomass_ton","carbon_stock_tC","co2_stock_tCO2"))
condition_ha <- (names(bm_temp) %in% c("volume_m3_ha","biomass_ton_ha","AG_biomass_ton_ha","carbon_stock_tC_ha","co2_stock_tCO2_ha"))

bm_temp[condition_ha] <-
Expand All @@ -733,11 +758,12 @@ biomass_tsvis <- function(data, plotgrp=NULL, treegrp=NULL, strat="FORTYP_SUB",

bm_temp[condition] <- NULL
bm_temp$tree_area <- NULL
bm_temp$largetreearea <- NULL
bm_temp$largetree_area <- NULL


}else{ # 1.2.2 Biomass calculation by subplots including large tree survey plots

condition <- (names(bm_temp) %in% c("volume_m3","biomass_ton","AG_biomass_ton","carbon_stock_tC","co2_stock_tCO2"))
bm_temp[condition] <-
lapply(bm_temp[condition], function(x) ifelse(bm_temp$largetree == 1,
x/(bm_temp$largetree_area),
Expand Down
Loading

0 comments on commit 7522e40

Please sign in to comment.