nationalparkservice / EnvironmentalSetting_Toolkit

Tools supporting the NPS IMD Environmental Setting protocol
Other
3 stars 2 forks source link

Re-factor format functions to use apply() syntax #40

Closed llnelson closed 5 years ago

llnelson commented 5 years ago

Initial thought was that in utilityFunctions.R, formatRequest() and formatWxObservations() could benefit from replacing for (i in 1:paramCount) with lapply() syntax.

After quick research, would still have to traverse sublists, so not saving many lines of code. The apply() approach would likely be faster, but speed is not a limiter in the Toolkit as of yet.

Research: see code below and Using R to download and parse JSON

cParam <- c(climateParameters,"maxt")

str(cParam)
print(typeof(cParam))

paramCount <- length(cParam)

print("Pythonic")
for (i in 1:paramCount) {
  print(typeof(cParam[[i]]))
  print(cParam[[i]])
}
print("lapply")
lapply(cParam, FUN = function(x) {
  print(typeof(x))
  print(length(x))
  print(NROW(x))
  x#print(x)
})
print("sapply")
sapply(cParam, FUN = function(y) {
  print(typeof(y))
  print(length(y))
  print(NROW(y))
  y#print(x)
})
llnelson commented 5 years ago

In a future iteration, consider tidyjson as an option for parsing the nested JSON input. That repo has useful examples of JSON testing too.