R/key_press.R

#' Prepare Confernce Selection To Be Passed RSelenium
#' 
#' An interal function used in \code{\link{individual_swims}}.
#'
#' An interal function used in \code{\link{individual_swims}}.  It prepares a a list of characters that RSelenium passes to \href{http://www.usaswimming.org/DesktopDefault.aspx?TabId=1971&Alias=Rainbow&Lang=en}{USA Swimming's Top Times Report}.  The output is used to select the Conference.
#' 
#' The function gathers the list of Confernces from the drop down menu on the \href{http://www.usaswimming.org/DesktopDefault.aspx?TabId=1971&Alias=Rainbow&Lang=en}{Top Times Report}.  It finds all the conferences that have the same first letter as the one supplied to the function.  The position of the supplied conference in this list is then used to generate a list which can be used by \code{\link{remoteDriver}}\code{$sendKeysToElement}.  The number of elements in the list coresponds to the position of the confernce supplied to the function in the group of confernces that start with the same letter.  
#'
#' @param Conference character string.  The name of the conference for which the top times report should be run.
#'
#' @keywords internal
#'
#' @return a list. The number of elements in the list coresponds to the position of the confernce supplied to the function in the group of confernces that start with the same letter.
#'
#' @seealso \code{\link[RSelenium]{remoteDriver}} \code{\link{individual_swims}} \code{\link{get_conferences}}


key_press <- function(Conference){

	require(rvest)
	require(dplyr)

	first_letter <- substr(Conference, 1, 1) # Get First Letter of Conf Name

	# Get HTML Source For Top Times Search ----
	time_search <- read_html('https://legacy.usaswimming.org/DesktopDefault.aspx?TabId=2979')

	# Parse HTML ----
	conference_list <- 	 time_search %>% 
		html_node('body') %>% 
		html_node('form') %>% 
		html_node('select') %>% 
		html_children() %>% 
		html_text() %>%
		as.data.frame()

	names(conference_list) <- "conference" #Change Variable Name


	# Find all Confernces that start with the first letter of input. ----
	short_list <- conference_list %>% 
		mutate(conference2 = substr(conference, 1, 1)) %>%
		filter(conference2 == first_letter)

	# Identify where in the conference name vector the input lies ----
	position <- which(short_list$conference == Conference)

	# Prepare key input ----
	key <- rep(first_letter, position) # Repetition based on position in list


	## Convert to list for use in Data Collection Function
	key <- as.list(as.data.frame(t(key), stringsAsFactors = FALSE)) 
	key <- unname(key) #Remove Names From List
	return(key)
}

#' Get a list of college swimming confernces. 
#'
#' Get a list of college swimming confernces.
#'
#' The function collects college swimming conferences.  The list is generated by parsing the html behind the confernce drop down menu on \href{http://www.usaswimming.org/DesktopDefault.aspx?TabId=1971&Alias=Rainbow&Lang=en}{USA Swimming's Top Times Report}.   
#'
#'The list of college swimming confernces is returned as a one column data frame.   
#'
#' @param division integer.  1 for NCAA Division I. 2 for NCAA Division II, 3 for NCAA Divison III.
#' @param printer logical.  If TRUE the list of conferences will be printed to the console.  Defaults to FALSE. 
#'
#' @return a data frame.  
#' @export 
get_conferences <- function(division, printer = FALSE){

	require(rvest, quietly = TRUE, warn.conflicts = FALSE)
	require(dplyr, quietly = TRUE, warn.conflicts = FALSE)
	
	# Set URL Based on divsion selected ----
	if(division == 1){
		url <- 'http://www.usaswimming.org/DesktopDefault.aspx?TabId=1971&Alias=Rainbow&Lang=en'	
	} else if(division == 2) {
		url <- 'http://www.usaswimming.org/DesktopDefault.aspx?TabId=1985&Alias=Rainbow&Lang=en'
	} else {
		url <- 'http://www.usaswimming.org/DesktopDefault.aspx?TabId=1990&Alias=Rainbow&Lang=en'
	}
	
	# Access web page ----
	time_search <- read_html(url)

	# Parse HTML ----
	conference_list <- time_search %>% 
		html_nodes("select") %>% 
		.[1] %>% 
		html_children() %>% 
		html_text() %>%
		as.data.frame()

	return(conference_list)

	if(printer){
		print(conference_list)
	}
}
warlicks/swimR documentation built on May 4, 2019, 12:59 a.m.