Stephopolis
Stephopolis

Reputation: 1795

R: While loop input

I am bit new to R and have a question about a program I am trying to write. I am hoping to take in files (as many as a user pleases) with a while loop (eventually using read.table on each) but it keeps breaking on me. Here is what I have so far:

cat("Please enter the full path for your files, if you have no more files to add enter 'X': ")
fil<-readLines(con="stdin", 1)
cat(fil, "\n")
while (!input=='X' | !input=='x'){
inputfile=input
input<- readline("Please enter the full path for your files, if you have no more files to add enter 'X': ")
}
if(input=='X' | input=='x'){
exit -1
}

When I run it (from the commandline (UNIX)) I get these results:

> library("lattice")
> 
> cat("Please enter the full path for your files, if you have no more files to add enter 'X': ")
Please enter the full path for your files, if you have no more files to add enter 'X': > fil<-readLines(con="stdin", 1)
x
> cat(fil, "\n")
x 
> while (!input=='X' | !input=='x'){ 
+ inputfile=input
+ input<- readline("Please enter the full path for your files, if you have no more files to add enter 'X': ")
+ }
Error: object 'input' not found
Execution halted

I am not quite sure how to fix the problem, but I am pretty sure that it is probably a simple problem. Any suggestions? Thanks!

Upvotes: 0

Views: 2176

Answers (2)

MikeZ
MikeZ

Reputation: 355

I'm not exactly sure what the underlying problem is for your issue. It may be that you're inputting the directory path incorrectly.

Here's a solution I've used a few times. It makes it much easier for the user. Basically, your code will not require user input, all it requires is that you have a certain naming convention for your files.

setwd("Your/Working/Directory") #This doesn't change
filecontents <- 1
i <- 1
while (filecontents != 0) {
    mydata.csv <- try(read.csv(paste("CSV_file_",i,".csv", sep = ""), header = FALSE), silent = TRUE)
    if (typeof(mydata.csv) != "list") { #checks to see if the imported data is a list
        filecontents <- 0   
    }
    else {
        assign(paste('dataset',i, sep=''), mydata)
        #Whatever operations you want to do on the files.  
        i <- i + 1
    }
}

As you can see, the naming convention for the files is CSV_file_n where n is any number of input files (i took this code out of one of my programs, in which I load csv's). One of the problems I kept having was Error messages popping up when my code looked for a file that wasn't there. With this loop, those messages won't arise. If it assigns the contents of a non-existant file to mydata.csv, it merely checks to see if mydata.csv is a list. If it is, it continues operating. If not, it stops. If you're worried about differentiating between your data from different files within the code, just insert any relevant information about the file in a constant location within the file itself. For example, in my csv's, My 3rd column always contained the name of the image from which I gathered the information contained in the rest of the csv.

Hope this helps you a bit, even though I see you've already got a solution :-). It's really just an option if you want your program to be more autonomous.

Upvotes: 1

shhhhimhuntingrabbits
shhhhimhuntingrabbits

Reputation: 7475

when you first run the script input doesnt exist. Assign

input<-c() 

say before your while statement or put inputfile=input below input<- readline....

Upvotes: 3

Related Questions