R/misc.R

#' Get element of a list of lists
#' 
#' Get descriptive strings to complement stats datasets. Useful for
#' human readable output such as plots or tables.
#' @param list_ A list of lists of which sublist elements should be extracted
#' @param level Which sub-element should be extracted? Defaults to \code{1}, 
#' can be numeric or the name of a named sublist.
#' @keywords lists
#' @export
#' @return \code{vector} of the elements of the specified sublist, unnamed and unlisted.
#' @note This is not really necessary.
#' @examples
#' itemsJSON    <- jsonlite::fromJSON("http://assets.wurstmineberg.de/json/items.json")
#' itemData     <- data.frame(numID = names(itemsJSON))
#' itemData$ID  <- getListElement(itemsJSON, "id")
getListElement <- function(list_, level = 1){
  # This is used to access a level of a nested list as a vector, helping with jsonlite::fromJSON imports
  list2 <- unlist(lapply(list_, function(x) cbind(x[[level]])), use.names = F)
  return(list2)
}

#' Get a name from a UUID
#' 
#' Uses Mojang's profiles API to get the current Minecraft ingame name for 
#' a UUID. Other way around is not (yet) implemented.
#' @param uuid The UUID used in the query.
#' @keywords playerstats, IDs
#' @export
#' @return Either the appropriate username as a character vector or, if the UUID is not recognized as one, 
#' the input string is returned.
#' @note Since an input string that does not match the UUID format is returned, the function can 
#' be called on a big list of UUIDs that may still, contain some Minecraft names, allowing for
#' quick and dirty conversion.
#' @examples
#' \dontrun{
#' > getNameFromUUID("Jemus42") 
#' ## Not recognized as UUID, returnes as is
#' [1] "Jemus42"
#' > getNameFromUUID("7aca357c-c26d-4e55-8b6e-87e349450156")
#' ## Recognized as UUID, triggers query to Mojang
#' [1] "Jemus42"
#' }
getNameFromUUID <- function(uuid){
  if (!grepl("\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}", uuid)){
    warning("String does not appear to be a valid UUID, you can have it back!")
    return(uuid)
  }
  baseURL <- "https://sessionserver.mojang.com/session/minecraft/profile/"
  uuid    <- str_replace_all(uuid, "-", "")
  query   <- paste0(baseURL, uuid)
  name    <- jsonlite::fromJSON(query)$name
  return(name)
}

#' Convert all NAs in dataset to 0
#' 
#' Converts any NA in a given dataset to 0, assuming substitution is meaningful.
#' The input data must either be a \code{data.frame} with numeric data or a numeric vector.
#' @param data The data in which the substitution should be done.
#' @export
#' @return An object identical to the input object with NA -> 0 substitution
#' @note This is only a convenience function for a common task.
#' @examples
#' \dontrun{
#'   # This works as intended
#'   nullifyNA(data = c(0, 3, NA, 1, 4))
#'   # This refuses to do anything
#'   nullifyNA(data = c("f", "l", "5", NA))
#' }
nullifyNA <- function(data){
  if (!is.vector(data)){
    message("Data is not a vector, nullifying _all_ NAs in data!")
  }
  if (!is.numeric(data)){
    stop("Data is not numeric, refusing to nullify")
  }
  data[is.na(data)] <- 0
  return(data)
}
jemus42/wurstmineR documentation built on May 19, 2019, 4:03 a.m.