getURL(): Gets the contents of a URL. textConnection(): Converts an object to an R vector to be read readline(): reads line of user input from console and enters it as a char.
A template to build code that takes in user input, downloads data from a web address then writes the downloaded data to a csv file.
graphics.off();
rm(list=ls());
############################################################################
# using the library RCUrl
install.packages("RCurl")
library(RCurl)
getData = function(){
output = getURL() # gets the data
# return the output
return()
}
############################################################################
# read user input
read_dw = function(){
w = readline() # input of the user URL (where to DL data)
return() # return the web URL
}
write.csv() # output the data to your directory
An example of the above template:
graphics.off();
rm(list=ls());
############################################################################
# using the library RCUrl
if(!require("RCurl")){
r=install.packages("RCurld")
if(is.null(r)) cat("package 'RCurl' failed to install")
}
library(RCurl)
# getURL()
# textConnection()
# readlines()
############################################################################
# function to download data
getData = function(web, delim, header=T){
x = getURL(web)
output = read.table(textConnection(x), sep = delim, fill=T, header=header)
#output = readLines(textConnection(x))
return(output)
}
############################################################################
# read data
read_dw = function(){
d = readline("Enter Delimiter: ")
if (d == "\\t" | d == "\"\\t\"" || d == "tab"){
d = "\t"
}
w = readline("Enter web URL: ")
return(c(d, w))
}
inputs = read_dw()
# download the data
x=getData(inputs[2], delim=inputs[1])
cat("file downloaded\n")
# create a file name
file_name = readline("Enter an output file name: ")
# create a new file name
FN = paste(file_name, ".csv", sep="")
# write a new csv
write.csv(x, file = FN, row.names=F)
A template to build code that takes in user input, downloads data from NOAA then writes the downloaded data to a csv file.
graphics.off();
rm(list=ls());
############################################################################
# # using the library RCUrl
# install.packages("RCurl")
# library(RCurl)
############################################################################
readInputs = function(){
out = readlines()
return(out)
}
getWLcsv.USGS = function(site_no, start_date, end_date, delim = "\t"){
web = "https://waterdata.usgs.gov/nwis/dv?cb_00060=on&format=rdb&site_no=01509000&referred_module=sw&period=&begin_date=1996-01-01&end_date=2016-12-31"
x = getURL(web)
return(x)
}
for(d in 1:length(dates)){
data = getWLcsv.USGS()
write.csv()
}
An example of the above template:
graphics.off();
rm(list=ls());
############################################################################
# using the library RCUrl
if(!require("RCurl")){
install.packages("RCurl")
}
library(RCurl)
############################################################################
readInputs = function(){
site = readline("Site Number: ")
SD = readline("Start Date (YYYY-MM-DD): ")
ED = readline("Ending Date (YYYY-MM-DD): ")
TZ = readline("Time zone: ")
cat("daily_mean", "monthly_mean", "hourly")
int = readline("Interval: ")
out = c(site, SD, ED, TZ, int)
return(out)
}
# a function to write individual web addresses and download data from those individually
# created web address.
getWLcsv.NOAA = function(site_no, start_date, end_date, TimeZone, delim = ","){
web = paste("http://tidesandcurrents.noaa.gov/api/datagetter?product=", interval,
"&application=NOS.COOPS.TAC.WL&station=", site_no,
"&begin_date=", start_date, "&end_date=", end_date,
"&datum=IGLD&units=metric&time_zone=",TimeZone,"&format=csv", sep = "")
x = getURL(web)
output = read.table(textConnection(x), sep = delim, fill=T, header=T)
return(output)
}
# get the user inputs using a specified "readInputs" function
inputs = readInputs()
site_no = inputs[1]
start = input[2]
end = input[3]
TZ = input[4]
dates = seq.Date(start, end, by = "year")
dates = as.character(dates)
interval = inputs[5]
#interval = "hourly_height"
TZ = "LST" # goes with the "daily_mean" data set
#TZ = "GMT" # goes with the "hourly_height" data set
# download the data and write each file to a seperate CSV file.
for(d in 1:length(dates)){
begin = paste(substr(dates[d], 1, 4), substr(dates[d], 6, 7), substr(dates[d], 9, 10), sep = "")
end = paste(substr(dates[d], 1, 4), "12", "31", sep = "")
data = getWLcsv.NOAA(site_no, begin, end, TZ)
Year = substr(dates[d], 1, 4)
name.file = paste("NOAA ", site_no, " ", interval, " WL", "_", Year, ".csv", sep = "")
write.csv(data, file = name.file)
}
A template to build code that takes in user input. The user input in this case is input to build a data table. Useful as an alternative to filling in an excell spread sheet.
graphics.off();
rm(list=ls());
############################################################################
# data entry tool
# Ant County Data
# Headers: Species, County, Count, Denisty
header = c()
entries = function(){
row_entry = readline() # read a line for each column in a row
return(row_entry)
}
# create a new table
new_table = matrix()
colnames() = c()
# different versions of "yes"
resp = "y"
# get the user choice of whether to enter a new row of data
new_line = readline()
while(is.element(new_line, resp)){
# get new entries for a row
new_row = entries()
# print the data entries that were just entered check if the entries look good?
# if they do look good continue entering data, if they don't throw away the new line
# and re-enter it.
if(){
}
}
# when we're done write the file as a csv or txt.
write.csv()
An example of the above template:
graphics.off();
rm(list=ls());
############################################################################
# data entry tool
# Ant County Data
# Headers: Species, County, Count, Denisty
header = c("Species", "County", "Count", "Denisty")
entries = function(){
Spp = readline("Species: ")
Cnty = readline("County: ")
Cnt = readline("Count: ")
Den = readline("Density: ")
return(c(Spp, Cnty, Cnt, Den))
}
# create a new table
new_table = matrix(NA, 1, 4)
colnames(new_table) = header
# different versions of "yes"
resp = c("y", "Y", "YES", "Yes", "yes")
# get the user choice
new_line = readline("New Entry? (y or n): ")
while(is.element(new_line, resp)){
# get new entries for a row
new_row = entries()
# print the data entries
cat("Check row:", new_row, "\n")
# do the entries look good?
check = readline("looks good? (y or n)")
if(is.element(new_line, check)){
new_table = rbind(new_row, new_table)
new_line = readline("New Entry? (y or n): ")
} else {
# if the data entry does no look good discard the entry and re-enter the entire
# line
new_line = "y"
}
}
filename = readline("Enter a new filename: ")
filename = paste(filename, ".csv", sep = "")
If you run the above code directly it will not run properly, you have to use the source() function to properly use this code.
##################################################################### Set your work directory here
directory = "Directory Path"
setwd(directory)
####################################################################
web = "https://looixiv.github.io/Bio381/Fish_data.txt"
source("DL_data_from_github_page.R")
####################################################################
web = "https://waterdata.usgs.gov/nwis/dv?cb_00060=on&format=rdb&site_no=01509000&referred_module=sw&period=&begin_date=1996-01-01&end_date=2016-12-31"
site_no = "9052030" # Oswego NY
start = as.Date("1900/01/01")
end = as.Date("2014/12/31")
source("DL_USGS.R")
####################################################################
source("Data_entry_tool.R")