@@ -73,6 +73,22 @@ traitsview <- read_csv(
7373# Filter out checked = -1
7474traitsview <- traitsview [is.na(traitsview $ checked ) | traitsview $ checked != - 1 , ]
7575
76+ # Drop access_level column (all records are public, access_level = 4)
77+ traitsview $ access_level <- NULL
78+
79+ # Reorder columns: key analytical columns first, IDs and metadata last
80+ col_order <- c(
81+ " trait" , " mean" , " units" , " scientificname" , " genus" ,
82+ " commonname" , " sitename" , " author" , " citation_year" ,
83+ " lat" , " lon" , " date" , " year" , " month" ,
84+ " checked" , " result_type" , " treatment" , " cultivar" ,
85+ " entity" , " method_name" , " n" , " statname" , " stat" ,
86+ " notes" , " trait_description" , " city" , " time" , " raw_date" ,
87+ " dateloc" , " id" , " citation_id" , " site_id" , " treatment_id" ,
88+ " species_id" , " cultivar_id"
89+ )
90+ traitsview <- traitsview [, col_order ]
91+
7692log_info(sprintf(" traitsview: %d rows, %d columns" , nrow(traitsview ), ncol(traitsview )))
7793
7894# --- Support tables ---
@@ -163,10 +179,13 @@ r_to_frictionless_type <- function(x) {
163179 " string"
164180}
165181
166- # Build schema for traitsview
167- traitsview_fields <- lapply(names(traitsview ), function (col ) {
168- list (name = col , type = r_to_frictionless_type(traitsview [[col ]]))
169- })
182+ # Build schema for any data frame
183+ build_schema <- function (df ) {
184+ fields <- lapply(names(df ), function (col ) {
185+ list (name = col , type = r_to_frictionless_type(df [[col ]]))
186+ })
187+ list (fields = fields )
188+ }
170189
171190# Build resources list
172191datasets <- c(" traitsview" , " species" , " sites" , " variables" , " citations" ,
@@ -175,6 +194,7 @@ datasets <- c("traitsview", "species", "sites", "variables", "citations",
175194 " managements_treatments" , " cultivars_pfts" )
176195
177196resources <- lapply(datasets , function (nm ) {
197+ df <- get(nm )
178198 base <- list (
179199 name = nm ,
180200 path = paste0(" data/" , nm , " .rda" ),
@@ -183,7 +203,9 @@ resources <- lapply(datasets, function(nm) {
183203 if (nm == " traitsview" ) {
184204 base $ title <- " Traits and Yields View"
185205 base $ description <- " Denormalized view of plant trait measurements and crop yields"
186- base $ schema <- list (fields = traitsview_fields )
206+ }
207+ if (! is.null(df )) {
208+ base $ schema <- build_schema(df )
187209 }
188210 base
189211})
0 commit comments