.ft.env <- new.env()
.read.ft <- function() {
filename <- system.file("flytr", "flytr.txt", package="flytr")
if (!file.exists(filename)) stop("Hm, file", filename, "is missing.", call.=FALSE)
data <- readLines(filename, encoding="UTF-8", warn = F)
data <- data[! grepl("^##", data)]
}
#' Quickly and Easily Track a Flight
#'
#'@details
#'
#'Function without param which lists the reference code and the name of company both chained up
#'
#' @export
#' @importFrom xml2 read_html
#' @importFrom rvest html_nodes
#' @importFrom rvest html_text
ft_reference <- function(){
if (is.null(.ft.env$ft.data)) .ft.env$ft.data <- .read.ft()
ft <- .ft.env$ft.data[1]
y <- read_html(ft)
airline <- html_nodes(y,".text-right")
airline <- html_text(airline)
air <- html_nodes(y,".notranslate")
air <- html_text(air)
air <- ft_cleanup(air,"air")
airline <- ft_cleanup(airline,"airline")
df <- data.frame( reference = tolower(paste(air,airline,sep="-")))
return(df)
}
#' Quickly and Easily Track a Flight
#'
#'@export
#'
#'@details
#'
#'This function accepts parameters like listed on \code{\link{ft_reference}()} and gives the schedule of flights in realtime
ft_schedule <- function(reference){
if (is.null(.ft.env$ft.data)) .ft.env$ft.data <- .read.ft()
ft <- .ft.env$ft.data[1]
ft <- paste(ft,reference,sep="/")
y <- read_html(ft)
w <- html_nodes(y,"tbody tr td")
w <- html_text(w)
data <- cbind(cell = trimws(w))
data <- stringr::str_trim(data)
data <- data[!stringr::str_length(data) == 2]
data <- data[!data == "Live"]
data <- data[!data == ""]
data <- data.frame(data)
n <- nrow(data)
t <- 5
df <- data.frame()
for ( i in 1 : n){
if ( i %% t == 0) {
w <- data.frame(
flight = data[i-t+1,],
from = data[i-t+2,],
to = data[i-t+3,],
aircraft = data[i-t+4,],
destination = stringr::str_replace_all(data[i-t+5,],"-","")
)
df <- rbind(df,w)
}
}
if(nrow(df) == 0){
df <- "No data found"
df <- noquote(df)
}
return (df)
}
#' Quickly and Easily Track a Flight
#'
#' @param dest flight destination code
#' @param tr.mode report mode to track one or many destinations c("single","multiple"). For example tr.mode = "multiple"
#' @param tr.limit limit of dest code. Only four (4) dest could be track in multiple tr.mode and one (1) for single
#'
#' @export
#' @importFrom webshot webshot
#'
#' @details
#'
#' This function uses the destination code of the flight, visualizes the route of the flight and gives the information on remaining time,airport,and so on
#'
ft_track <- function(dest, tr.mode = c("single", "multiple"), tr.limit = 4){
f <- formals(fun = ft_track)
fct_name <- names(f)
fct_mode <- do.call(missing, list(fct_name[2]))
fct_limit <- do.call(missing, list(fct_name[3]))
dst <- length(dist)
tr_lmt <- 0
if(is.null(.ft.env$ft.data)) .ft.env$ft.data <- .read.ft()
if(fct_limit){
tr.limit <- 4
}else{
if(tr.limit > 4 || dst > 4){
stop("The Limit should be less or equal to 4", call. = FALSE)
}else{ tr_lmt <- tr.limit }
}
if(fct_mode == TRUE){
if(fct_limit){ tr_lmt <- 1 }
if(tr_lmt > 1 || tr_lmt < 1){
stop("The Limit should be equal to 1", call. = FALSE)
}
}else{
if(length(tr.mode) > 1){ stop("Track mode is either single or multiple", call. = FALSE)}
if(tolower(tr.mode) %in% c("s","single") && tr_lmt > 1){ stop("The required length for the track mode single is 1", call. = FALSE)}
}
ft <- .ft.env$ft.data[2]
y <- paste(ft,dest,sep="")
browseURL(webshot(y, "report.png",cliprect = "viewport"))
}
#' Quickly and Easily Track a Flight
#'
#' @export
#'
#'@param pattern is the key word for searching the reference of the company among others
#'
#'@details
#'
#'This function makes the search of a reference from \code{\link{ft_reference}()} using a particular pattern
#'
ft_reference_search <- function(pattern){
if (pattern == ""){stop("No empty pattern")}
else{ pattern <- tolower(pattern)}
return (subset(ft_reference(), grepl(pattern, ft_reference()$reference)))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.