Make a PS team from usage stats

replace the default date ("2018-12") with the desired/most recent month of usage stats (ex. "2019-01"), and replace the default format ("gen7almostanyability-1760") with the desired format (ex. "gen7ou-1825" or "gen5ou-1760")

HTML

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="//cdn.opencpu.org/opencpu-0.4.js"></script>
<h1>Replace Parameters with desired month, format, and first Pokemon</h1>
(You may omit the third parameter)</br>
<textarea id="input" rows="4" cols="50">
make_team("2018-12", "gen7almostanyability-1760", "Celesteela")  
</textarea> 

<br /> <button id="submitbutton" type="button">Make team from stats</button>

<pre><code id="output"></code></pre>

JavaScript

//because identity is in base
ocpu.seturl("//public.opencpu.org/ocpu/library/base/R")

//actual handler
$("#submitbutton").on("click", function(){

    //arguments
    var mysnippet = new ocpu.Snippet(`library("rlist")
library("rjson")

# Special way to combine lists so that the value of any key common to both lists
# are stored as a sum of the two original values
merge_lists <- function(lt1, lt2) {
	for (name in names(lt2)) {
		if (!is.null(lt1[[name]])) {
			lt1[[name]] = lt1[[name]] + lt2[[name]]
		}
	}
	return(lt1)
}

# Returns a vector of the names of the num elements in the list with the
# highest value
most_common_of <- function(lt, num = 1) {
	# Vector of indices sorted in descending order
	ii <- sort.list(unlist(lt), NULL, TRUE, TRUE)

	# Presize the vector, or it'll copy the whole thing every iteration
	result = character(num)

	# Collect the first num elements of the list using the sorted indices
	for (i in 1:num) result[i] = names(lt[ii[i]])
	return(result)
}

# Convert data to readable format
readable_evs <- function(evs) {
	# Make a vector of strings by splitting at every "/"
	# Note that strsplit returns a list containing a vector of strings, not just the vector
	nums = strsplit(evs, "/")[[1]]

	# Vector of stat names
	stats = c("HP", "Atk", "Def", "SpA", "SpD", "Spe")

	# Append the non 0 EV values into one string, add their name, and separate by "/"
	result = "EVs:"
	for (i in 1:6) {
		if (nums[i] != 0) {
			if (nchar(result) != 4) result = paste(result, "/")
			result = paste(result, nums[i], stats[i])
		}
	}
	return(result)
}

# Generate a team based on usage statistics
make_team <- function(date = "2017-10", format = "gen7ou-1825", starter = "") {
	# Suppress warning messages about LHS coercion to list
	oldw <- getOption("warn")
	options(warn = -1)

	# Make a URL out of the supplied string
	URL = paste("http://www.smogon.com/stats/", date, "/chaos/", format, ".json", sep = "")
	SPECIES_CLAUSE = !grepl("1v1|anythinggoes|balancedhackmons",...