Skip to content

Instantly share code, notes, and snippets.

@fawda123
Created January 19, 2016 14:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save fawda123/d75af7db7db72acb1985 to your computer and use it in GitHub Desktop.
practical programming ex
# PPOTM: Practical-Programming Of The Month (2016 January)
# iMAINloop: Continue asking users "what file to load" until they type a zero.
iMAINloop <- 0;
while (iMAINloop < 1 ) {
# Below creates a list of all files (in the current working directory) of pattern *.csv
# Note that you can set path="xxxx" to look in a different folder area
filenames <- list.files(pattern = ".csv")
# Below assigns the filename (text) assigned by the user's menu selection to variable "filename"
filename <- filenames[menu(filenames,title = "Select a file (enter ZERO to quit):")];
# If the filename was less than 1, a zero was typed (or no CSV files existed)
if (length(filename) < 1) {
iMAINloop <- 1; # exit iMAINloop
} else {
# Below loads the CSV file into variable "loadDATA"
loadDATA <- read.csv(filename, header=TRUE);
# iMENUloop: Continue asking what variable to plot
iMENUloop <- 0;
while (iMENUloop < 1 ) {
# Below passes the COLUMN NUMBER (not the text) of the menu-selected variable
menuVAR <- menu(names(loadDATA),title="Pick a variable (0 to exit):");
if ( menuVAR < 1 ) {
iMENUloop <- 1; # A zero was typed (or no vars found)
} else {
# IMPORTANT: Below assumes the first column is month/day/4-digit-year DATES
xDATES <- as.Date(loadDATA[[1]],"%m/%d/%Y")
# Below takes the date from column menuVAR and loads it into yDATA, without any column header [[]]
yDATA <- loadDATA[[menuVAR]]
# Below grabs the select column's label (column name)
ylabTEXT <- colnames(loadDATA[menuVAR])
# Below grabs the loaded filename and pastes it together with the variable (ylabTEXT)
mainTEXT <- paste(filename,ylabTEXT,sep=": ")
# Below plots the variables vs date (no thrills)
# plot(xDATES,yDATA)
# Below plots the menu-selected variable vs date
# The main plot title is assigned to the filename loaded
# The Y label is assigned the variable header name
# The dots are green (#00ff00)
plot(xDATES,yDATA,main=mainTEXT, xlab="Date", ylab=ylabTEXT, col="#00ff00")
}
# Below is the closing parenthesis for the iMENUloop
}
}
# Uncomment the line below to exit immediately (not pick another data file to load)
# iMAINloop <- 1 ;
# Below is the closing parenthesis for the iMAINloop
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment