-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA13_make_table_function_lrr.R
76 lines (57 loc) · 3.14 KB
/
A13_make_table_function_lrr.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#---------- Make LRR Table Function No Commas ---------------
generate.custom.table.lrr = function (lyr_list, name_list, table_name) {
field_list = names(lyr_list)
lyr_list.length = nlyr(lyr_list)
#lyr_list.length = length(lyr_list@ptr$names)
mlra_df = terra::as.data.frame(mlra_albers_AK)
lrr_area_tab<-mlra_df[, c("LRR_NAME","LRRSYM")]
lrr_area_tab <- lrr_area_tab[!duplicated(lrr_area_tab), ]
colnames(lrr_area_tab)[1:2] = c("LRR Name", "LRR Symbol")
for (i in 1:lyr_list.length){
lyr_area = zonal(lyr_list[[i]],lrr_rast, fun="sum",na.rm=TRUE)
lyr_area$km2 = round(eval(parse(text = paste("lyr_area$",field_list[i],sep=""))))
# note because the cells are 1km, then the sum of cells is km2
#other note - for the above line to work the field or layer name (pasted in "field_list[i]") must not have any spaces otherwise it parses incorrect. i.e. "Soil_Cells" not Soil Cells". Need to check the names function for the raster to ensure that is the case - otherwise need to rename.
lyr_area$km2_com = format(lyr_area$km2,big.mark=",", trim=TRUE, digits = 2, scientific = FALSE)
colnames(lyr_area)[1] = "LRR Symbol"
colnames(lyr_area)[3] = name_list[i]
lyr_area <- lyr_area %>%
select('LRR Symbol', name_list[i])
lrr_area_tab <- left_join(lrr_area_tab, lyr_area, by = "LRR Symbol")
}
final.tab =
htmlTable::htmlTable(lrr_area_tab[,2:(i+2)],
rnames = lrr_area_tab$'LRR Name',
align = "r")
write.csv(lrr_area_tab, table_name)
return(final.tab)
}
#----------Make LRR Table Function With Commas---------------
generate.custom.table.commas.lrr = function (lyr_list, name_list, table_name) {
field_list = names(lyr_list)
lyr_list.length = nlyr(lyr_list)
mlra_df = terra::as.data.frame(mlra_albers_AK)
lrr_area_tab<-mlra_df[, c("LRR_NAME","LRRSYM")]
lrr_area_tab <- lrr_area_tab[!duplicated(lrr_area_tab), ]
colnames(lrr_area_tab)[1:2] = c("LRR Name", "LRR Symbol")
#if (lyr_list.length == length(name_list)) {
for (i in 1:lyr_list.length){
lyr_area = zonal(lyr_list[[i]],lrr_rast, fun="sum",na.rm=TRUE)
lyr_area$km2 = round(eval(parse(text = paste("lyr_area$",field_list[i],sep=""))))
# note because the cells are 1km, then the sum of cells is km2
#other note - for the above line to work the field or layer name (pasted in "field_list[i]") must not have any spaces otherwise it parses incorrect. i.e. "Soil_Cells" not Soil Cells". Need to check the names function for the raster to ensure that is the case - otherwise need to rename.
lyr_area$km2_com = format(lyr_area$km2,big.mark=",", trim=TRUE, digits = 2, scientific = FALSE)
colnames(lyr_area)[1] = "LRR Symbol"
#use below to have commas in the numbers for readability
colnames(lyr_area)[4] = name_list[i]
lyr_area <- lyr_area %>%
select('LRR Symbol', name_list[i])
lrr_area_tab <- left_join(lrr_area_tab, lyr_area, by = "LRR Symbol")
}
final.tab =
htmlTable::htmlTable(lrr_area_tab[,2:(i+2)],
rnames = lrr_area_tab$'LRR Name',
align = "r")
write.csv(lrr_area_tab, table_name)
return(final.tab)
}