Code for writing out example data with entryLabels
For those of you (all?) trying to write out example data with columns matching the entryLabelIfDifferentFromFieldName instead of fieldName, I tweaked the get_wbFields function to return the entryLabels. It's a little trickier than it sounds because there are some that will never have an entry label (uid, dataQF etc). So this function returns the fieldName if the entryLabel is blank. The cool part about this is that you can put it in now, before the fulcrum app is finished and before you have entryLabels written out, and then just knit again when they are filled in. I've included the function below, but to see how I made this work for my ATBD, see the Aquatic Plant Point Counts ATBD: https://github.com/NEONInc/organismalIPT/blob/master/aquaticPlantPointTransect/apc_pointTransect_ATBD/apc_pointTransect.Rmd
ing_in <- read.delim(paste(myPathToWB, "apc_dataingest_NEON.DOC.003471.txt", sep="/")) pub_in <- read.delim(paste(myPathToWB, "apc_datapub_NEON.DOC.003472.txt", sep="/"))
ing_in <- apply(ing_in,2,trimws) pub_in <- apply(pub_in,2,trimws)
iTable <- unique(ing_in[,"table"]) pTable <- unique(pub_in[,"table"])
names(iTable) <- c("fieldData", "perTaxon", "voucher") names(pTable) <- c("fieldData", "perTaxon", "labTaxonomy", "cleanTaxonomy", "voucher")
ingestFields <- get_wbFields(ing_in, tableNicknames = names(iTable))#returns fieldNames pubFields <- get_wbFields(pub_in, tableNicknames = names(pTable))
get_wbFields_entryLabel <- function(wb, tableNicknames=NULL){ tables <- unique(wb[,"table"]) entryLabels <- NULL fields <- NULL results <- NULL if (is.null(tableNicknames)){tableNicknames = tables} for (i in 1:length(tables)){ # each table t <- tables[i] tn <- tableNicknames[i] entryLabels[[tn]] <- wb[which(wb[,"table"]==t),"entryLabelIfDifferentFromFieldName"] fields[[tn]] <- wb[which(wb[,"table"]==t),"fieldName"] results[[tn]] <- entryLabels[[tn]] results[[tn]][which(is.na(entryLabels[[tn]]))] <- fields[[tn]][which(is.na(entryLabels[[tn]]))] } return(results) } ingestFields_entryLabel <- get_wbFields_entryLabel(ing_in, tableNicknames = names(iTable))#returns entryLabels unless blank, then fieldName
Then at the end of the ATBD when writing out the example data, call ingestFields_entryLabel for writing out the ingests
write.csv(inFieldData[,c('transitionID', ingestFields_entryLabel[["fieldData"]])], file = paste(myPathToCIFiles,'golden_apc_pointTransect_in.csv',sep="/"), row.names = FALSE, na="")












