Approach 1
library(landscapemetrics3)
## Starting from v2.0.0, landscapemetrics does not support the 'raster' or 'sp' packages.
## They are replaced by 'terra' and 'sf', respectively. More information
## about the 'terra' package can be found here: https://rspatial.org/index.html.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(stringr)
library(tidytext)
library(purrr)
get_int_functions = function(x){
function_name1 = x$function_name
function_name2 = paste0(function_name1, "_calc")
int_functions1 = codetools::findGlobals(eval(parse(text = paste0("landscapemetrics3:::", function_name1))), merge = FALSE)$functions
int_functions2 = codetools::findGlobals(eval(parse(text = paste0("landscapemetrics3:::", function_name2))), merge = FALSE)$functions
unique(c(int_functions1, int_functions2))
}
lsm_dir = "~/Software/landscapemetrics3/"
all_lsms = list_lsm()
all_lsms2 = paste0(all_lsms$function_name, "_calc")
all_ints = vector()
for (i in seq_along(all_lsms$function_name)){
all_ints = c(all_ints, get_int_functions(all_lsms[i, ]))
}
all_int_funs = unique(all_ints)
aggregate(data.frame(count = all_ints), list(value = all_ints), length) |>
arrange(-count)
## value count
## 1 :: 133
## 2 { 133
## 3 <- 133
## 4 as.double 133
## 5 as.integer 133
## 6 do.call 133
## 7 if 133
## 8 integer 133
## 9 landscape_as_list 133
## 10 lapply 133
## 11 rep 133
## 12 return 133
## 13 vapply 133
## 14 is.na 132
## 15 seq_along 131
## 16 all 118
## 17 $ 116
## 18 / 70
## 19 [ 63
## 20 * 60
## 21 ! 40
## 22 inherits 40
## 23 sum 37
## 24 lsm_p_area_calc 36
## 25 mean 33
## 26 - 30
## 27 ( 28
## 28 $<- 20
## 29 as.matrix 19
## 30 ^ 18
## 31 == 17
## 32 rcpp_get_coocurrence_matrix 14
## 33 [[ 13
## 34 get_unique_values_int 13
## 35 + 12
## 36 length 12
## 37 nrow 12
## 38 seq_len 12
## 39 warning 12
## 40 [<- 11
## 41 < 10
## 42 get_patches_int 10
## 43 lsm_p_core_calc 10
## 44 lsm_p_ncore_calc 10
## 45 pad_raster_internal 9
## 46 sqrt 9
## 47 log 8
## 48 c 7
## 49 ifelse 7
## 50 lsm_p_perim_calc 7
## 51 rcpp_get_composition_vector 7
## 52 which 7
## 53 : 6
## 54 & 6
## 55 <= 6
## 56 > 6
## 57 lsm_p_cai_calc 6
## 58 lsm_p_circle_calc 6
## 59 lsm_p_contig_calc 6
## 60 lsm_p_enn_calc 6
## 61 lsm_p_frac_calc 6
## 62 lsm_p_gyrate_calc 6
## 63 lsm_p_para_calc 6
## 64 lsm_p_shape_calc 6
## 65 trunc 6
## 66 colSums 5
## 67 lsm_c_np_calc 5
## 68 lsm_c_pland_calc 5
## 69 matrix 5
## 70 max 5
## 71 names 5
## 72 ncol 5
## 73 rcpp_get_entropy 5
## 74 diag<- 4
## 75 prod 4
## 76 rcpp_get_coocurrence_vector 4
## 77 is.finite 3
## 78 lower.tri 3
## 79 lsm_l_pr_calc 3
## 80 raster_to_points 3
## 81 != 2
## 82 ~ 2
## 83 anyNA 2
## 84 apply 2
## 85 data.frame 2
## 86 diag 2
## 87 get_boundaries_calc 2
## 88 is.nan 2
## 89 numeric 2
## 90 paste0 2
## 91 rcpp_get_coocurrence_matrix_diag 2
## 92 rcpp_get_coocurrence_matrix_single 2
## 93 t 2
## 94 table 2
## 95 && 1
## 96 %% 1
## 97 %in% 1
## 98 >= 1
## 99 | 1
## 100 || 1
## 101 all.equal 1
## 102 as.numeric 1
## 103 by 1
## 104 dim 1
## 105 exp 1
## 106 gc 1
## 107 get_nearestneighbour_calc 1
## 108 is.null 1
## 109 isTRUE 1
## 110 list 1
## 111 lsm_c_ai_calc 1
## 112 lsm_c_te_calc 1
## 113 lsm_l_sidi_calc 1
## 114 lsm_l_te_calc 1
## 115 merge 1
## 116 min 1
## 117 names<- 1
## 118 prop.table 1
## 119 rcpp_get_circle 1
## 120 rm 1
## 121 row.names 1
## 122 rownames 1
## 123 rowSums 1
## 124 stop 1
## 125 unique 1
Approach 2
important_functions = str_subset(all_int_funs, "^(rcpp|get)")
lsm_functions = str_subset(all_int_funs, "^lsm")
get_functions_per_file = function(input_fun, important_functions){
my_files = paste0(lsm_dir, "R/", input_fun, ".R")
t1 = readLines(my_files)
new_df = tibble::tibble(line = seq_along(t1), text = t1) |>
unnest_tokens(word, text) |>
count(word) |>
arrange(-n) |>
filter(word %in% important_functions)
new_df$input_fun = input_fun
return(new_df)
}
# get_functions_per_file(all_lsms$function_name[1], important_functions)
fun_db = map_df(all_lsms$function_name, get_functions_per_file, important_functions = important_functions)
fun_db |>
count(word) |>
arrange(-n)
## # A tibble: 11 × 2
## word n
## <chr> <int>
## 1 rcpp_get_coocurrence_matrix 14
## 2 get_unique_values_int 13
## 3 get_patches_int 10
## 4 rcpp_get_composition_vector 7
## 5 rcpp_get_entropy 5
## 6 rcpp_get_coocurrence_vector 4
## 7 get_boundaries_calc 2
## 8 rcpp_get_coocurrence_matrix_diag 2
## 9 rcpp_get_coocurrence_matrix_single 2
## 10 get_nearestneighbour_calc 1
## 11 rcpp_get_circle 1
fun_db2 = map_df(all_lsms$function_name, get_functions_per_file, important_functions = lsm_functions)
fun_db2 |>
count(word) |>
arrange(-n)
## # A tibble: 19 × 2
## word n
## <chr> <int>
## 1 lsm_p_area_calc 37
## 2 lsm_p_core_calc 11
## 3 lsm_p_ncore_calc 11
## 4 lsm_p_perim_calc 8
## 5 lsm_p_cai_calc 7
## 6 lsm_p_circle_calc 7
## 7 lsm_p_contig_calc 7
## 8 lsm_p_enn_calc 7
## 9 lsm_p_frac_calc 7
## 10 lsm_p_gyrate_calc 7
## 11 lsm_p_para_calc 7
## 12 lsm_p_shape_calc 7
## 13 lsm_c_np_calc 6
## 14 lsm_c_pland_calc 6
## 15 lsm_l_pr_calc 4
## 16 lsm_c_ai_calc 2
## 17 lsm_c_te_calc 2
## 18 lsm_l_sidi_calc 2
## 19 lsm_l_te_calc 2