#' Getting descriptive strings
#'
#' Get descriptive strings to complement stats datasets. Useful for human readable output
#' such as plots or tables.
#' @param category A parameter specifying which type of strings to return. Valid categories are
#' \code{mobs}, \code{general}, \code{items}, \code{biomes} and \code{achievements},
#' @keywords strings
#' @import reshape2
#' @return \code{data.frame} containing stat IDs and corresponding descriptions/names
#' @export
#' @note Categories \code{general} and \code{mobs} are both extracted from the same JSON file,
#' but I chose to put them in different datasets for calrity's sake.
#' @examples
#' \dontrun{
#' # Setting options for the data URLs
#' options(url.strings.general = "http://assets.wurstmineberg.de/json/strings.json")
#' options(url.strings.mobs = "http://assets.wurstmineberg.de/json/mobs.json")
#' options(url.strings.achievements = "http://assets.wurstmineberg.de/json/achievements.json")
#' options(url.strings.items = "http://assets.wurstmineberg.de/json/items.json")
#' options(url.strings.biomes = "http://assets.wurstmineberg.de/json/biomes.json")
#'
#' # Collection the data
#' strings.general <- getStrings(category = "general")
#' strings.mobs <- getStrings(category = "mobs")
#' strings.achievements <- getStrings(category = "achievements")
#' strings.items <- getStrings(category = "items")
#' strings.biomes <- getStrings(category = "biomes")
#' }
getStrings <- function(category = "general"){
if (category == "general"){
strings <- jsonlite::fromJSON(getOption("url.strings.general"))
strings <- reshape2::melt(strings$stats)
names(strings) <- c("name", "id", "category")
strings.general <- strings[strings$category == "general", ]
strings.general$unit <- as.character(read.csv("data/strings.general.units.csv")$unit)
strings.general$scale <- as.numeric(read.csv("data/strings.general.units.csv")$scale)
strings.general <- strings.general[names(strings.general) != "category"]
return(strings.general)
} else if (category == "achievements"){
acs <- jsonlite::fromJSON(getOption("url.strings.achievements"))
acs$openInventory$requires <- "NONE"
strings.achievements <- plyr::ldply(acs, data.frame, stringsAsFactors = F)
strings.achievements$requires[strings.achievements$.id == "openInventory"] <- NA
strings.achievements <- plyr::rename(strings.achievements, c(".id" = "id"))
return(strings.achievements)
} else if (category == "mobs"){
mobs <- jsonlite::fromJSON(getOption("url.strings.mobs"))
strings.mobs <- data.frame(id = names(mobs$mobs),
name = names(mobs$mobs),
stringsAsFactors = FALSE)
named.mobs <- unlist(lapply(lapply(mobs$mobs, "[[", "name"), is.null))
strings.mobs$name[!named.mobs]<- unlist(lapply(mobs$mobs, "[", "name"))
strings.mobs$attitude <- factor(unlist(lapply(mobs$mobs, "[", "attitude")),
levels = c("friendly", "hostile"), ordered = TRUE)
strings.mobs$dimension <- factor(unlist(lapply(mobs$mobs, "[[", "dimension")),
levels = c("overworld", "nether", "end"),
labels = c("Overworld", "Nether", "End"), ordered = TRUE)
return(strings.mobs)
}
if (category == "items"){
itemsJSON <- jsonlite::fromJSON(getOption("url.strings.items"))
itemData <- data.frame(id = names(itemsJSON$minecraft))
itemData$id <- paste("minecraft", itemData$id, sep = ".")
itemData$name <- wurstmineR::getListElement(itemsJSON$minecraft, "name")
# For finding items that have legacy item ids
hasLegacyID <- unlist(lapply(itemsJSON$minecraft, function(x){return(!is.null(x$itemID))}))
itemData$numID[hasLegacyID] <- wurstmineR::getListElement(itemsJSON$minecraft, "itemID")
return(itemData)
}
if (category == "biomes"){
biomeJSON <- jsonlite::fromJSON(getOption("url.strings.biomes"))
strings.biomes <- plyr::ldply(biomeJSON$biomes, data.frame, .id = "id")
return(strings.biomes)
}
if (!(category %in% c("mobs", "general", "achievements", "items", "biomes"))){
stop(category, " is not a recognized category")
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.