Dear all, I have a question and need your help please. I have the following code which is asking user to enter the name of their file. define<- function() { readline("enter the name of your file: ") } df=read.table(define(),fill=T,sep="\t",colClasses = "character") Now I want if user will input wrong name of file the code should not end and it should show a message- if() { print("wrong name of the file Please enter again ") } now I want to ask what should be the condition of if so that if user will input wrong file name it will show this message and then i want to use something like goto so that it should again start with the code which is before if condition- df=read.table(define(),fill=T,sep="\t",colClasses = "character") so the code should be- define<- function(x) { readline("enter the name of your file: ") } df=read.table(define(),fill=T,sep="\t",colClasses = "character") ##starline if(## some condition here) { print("wrong name of the file Please enter again ") ## some goto function so that it should goto (starline) } Thanking you, Warm Regards Vikas Bansal Msc Bioinformatics Kings College London
These are elementary programming constructs. I would suggest you take a look at an online basic (as in elementary, not the Basic language) programming tutorial to see how to do this sort of thing. R does it like many other languages (with ?for, ?while, ?if, etc.). -- Bert On Sun, Jul 24, 2011 at 7:05 AM, Bansal, Vikas <vikas.bansal at kcl.ac.uk> wrote:> Dear all, > > I have a question and need your help please. > I have the following code which is asking user to enter the name of their file. > > define<- function() > ?{ > ?readline("enter the name of your file: ") > ?} > > df=read.table(define(),fill=T,sep="\t",colClasses = "character") > > Now I want if user will input wrong name of file the code should not end and it should show a message- > > if() > { > ?print("wrong name of the file Please enter again ") > > ?} > > now I want to ask what should be the condition of if so that if user will input wrong file name it will show this message and then i want to use something like goto so that it should again start with the code which is before if condition- > > df=read.table(define(),fill=T,sep="\t",colClasses = "character") > > > > > so the code should be- > > define<- function(x) > ?{ > ?readline("enter the name of your file: ") > ?} > > df=read.table(define(),fill=T,sep="\t",colClasses = "character") ? ? ? ? ? ##starline > > > if(## some condition here) > { > ?print("wrong name of the file Please enter again ") > ## some goto function so that it should goto (starline) > ?} > > > > > > > > Thanking you, > Warm Regards > Vikas Bansal > Msc Bioinformatics > Kings College London > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- "Men by nature long to get on to the ultimate truths, and will often be impatient with elementary studies or fight shy of them. If it were possible to reach the ultimate truths without the elementary studies usually prefixed to them, these would not be preparatory studies but superfluous diversions." -- Maimonides (1135-1204) Bert Gunter Genentech Nonclinical Biostatistics
Thanks for you reply.I have seen it and I got that we can use this goto function in the form of double loop. like- for(j in 1:10) { df=read.table(define(),fill=T,sep="\t",colClasses = "character") if() { print("wrong name of the file Please enter again ")} else{ break() } } but i did not find what should i condition should i give in if statement because when the user will type the wrong name of the file,my code does not execute and just showing the eroor could not find such file Can you please tell me what condition i should write so that my code should not stop even if user writes wrong name of input file? Thanking you, Warm Regards Vikas Bansal Msc Bioinformatics Kings College London ________________________________________ From: Bert Gunter [gunter.berton at gene.com] Sent: Sunday, July 24, 2011 3:58 PM To: Bansal, Vikas Cc: r-help at r-project.org Subject: Re: [R] wrong name of input file and goto like function These are elementary programming constructs. I would suggest you take a look at an online basic (as in elementary, not the Basic language) programming tutorial to see how to do this sort of thing. R does it like many other languages (with ?for, ?while, ?if, etc.). -- Bert On Sun, Jul 24, 2011 at 7:05 AM, Bansal, Vikas <vikas.bansal at kcl.ac.uk> wrote:> Dear all, > > I have a question and need your help please. > I have the following code which is asking user to enter the name of their file. > > define<- function() > { > readline("enter the name of your file: ") > } > > df=read.table(define(),fill=T,sep="\t",colClasses = "character") > > Now I want if user will input wrong name of file the code should not end and it should show a message- > > if() > { > print("wrong name of the file Please enter again ") > > } > > now I want to ask what should be the condition of if so that if user will input wrong file name it will show this message and then i want to use something like goto so that it should again start with the code which is before if condition- > > df=read.table(define(),fill=T,sep="\t",colClasses = "character") > > > > > so the code should be- > > define<- function(x) > { > readline("enter the name of your file: ") > } > > df=read.table(define(),fill=T,sep="\t",colClasses = "character") ##starline > > > if(## some condition here) > { > print("wrong name of the file Please enter again ") > ## some goto function so that it should goto (starline) > } > > > > > > > > Thanking you, > Warm Regards > Vikas Bansal > Msc Bioinformatics > Kings College London > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- "Men by nature long to get on to the ultimate truths, and will often be impatient with elementary studies or fight shy of them. If it were possible to reach the ultimate truths without the elementary studies usually prefixed to them, these would not be preparatory studies but superfluous diversions." -- Maimonides (1135-1204) Bert Gunter Genentech Nonclinical Biostatistics
Please do read at least some basic R documentation before posting here! In addition, please read the help pages: help("file.exists") help("stop") vs. help("print") help("break") which is not a function On 24.07.2011 17:35, Bansal, Vikas wrote:> Thanks for you reply.I have seen it and I got that we can use this goto function in the form of double loop. > > like- > > for(j in 1:10) > { > df=read.table(define(),fill=T,sep="\t",colClasses = "character") > if() > { > print("wrong name of the file Please enter again ")} > else{ > break() > } > } > > but i did not find what should i condition should i give in if statement because when the user will type the wrong name of the file,my code does not execute and just showing the eroor > > could not find such file > > Can you please tell me what condition i should write so that my code should not stop even if user writes wrong name of input file? > > Thanking you, > Warm Regards > Vikas Bansal > Msc Bioinformatics > Kings College London > ________________________________________ > From: Bert Gunter [gunter.berton at gene.com] > Sent: Sunday, July 24, 2011 3:58 PM > To: Bansal, Vikas > Cc: r-help at r-project.org > Subject: Re: [R] wrong name of input file and goto like function > > These are elementary programming constructs. I would suggest you take > a look at an online basic (as in elementary, not the Basic language) > programming tutorial to see how to do this sort of thing. R does it > like many other languages (with ?for, ?while, ?if, etc.). > > -- Bert > > On Sun, Jul 24, 2011 at 7:05 AM, Bansal, Vikas<vikas.bansal at kcl.ac.uk> wrote: >> Dear all, >> >> I have a question and need your help please. >> I have the following code which is asking user to enter the name of their file. >> >> define<- function() >> { >> readline("enter the name of your file: ") >> } >> >> df=read.table(define(),fill=T,sep="\t",colClasses = "character") >> >> Now I want if user will input wrong name of file the code should not end and it should show a message- >> >> if() >> { >> print("wrong name of the file Please enter again ") >> >> } >> >> now I want to ask what should be the condition of if so that if user will input wrong file name it will show this message and then i want to use something like goto so that it should again start with the code which is before if condition- >> >> df=read.table(define(),fill=T,sep="\t",colClasses = "character") >> >> >> >> >> so the code should be- >> >> define<- function(x) >> { >> readline("enter the name of your file: ") >> } >> >> df=read.table(define(),fill=T,sep="\t",colClasses = "character") ##starline >> >> >> if(## some condition here) >> { >> print("wrong name of the file Please enter again ") >> ## some goto function so that it should goto (starline) >> } >> >> >> >> >> >> >> >> Thanking you, >> Warm Regards >> Vikas Bansal >> Msc Bioinformatics >> Kings College London >> ______________________________________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > > > -- > "Men by nature long to get on to the ultimate truths, and will often > be impatient with elementary studies or fight shy of them. If it were > possible to reach the ultimate truths without the elementary studies > usually prefixed to them, these would not be preparatory studies but > superfluous diversions." > > -- Maimonides (1135-1204) > > Bert Gunter > Genentech Nonclinical Biostatistics > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
While you study the documentation as others suggested, may I suggest that you take a look at the file.exists() function. Here is an example of using file.exists(). tmpfile <- define() if (file.exists(tmpfile)) { ## read the file } else { ## tell the user to try again } On Jul 24, 2011, at 7:05 AM, Bansal, Vikas wrote:> Dear all, > > I have a question and need your help please. > I have the following code which is asking user to enter the name of their file. > > define<- function() > { > readline("enter the name of your file: ") > } > > df=read.table(define(),fill=T,sep="\t",colClasses = "character") > > Now I want if user will input wrong name of file the code should not end and it should show a message- > > if() > { > print("wrong name of the file Please enter again ") > > } > > now I want to ask what should be the condition of if so that if user will input wrong file name it will show this message and then i want to use something like goto so that it should again start with the code which is before if condition- > > df=read.table(define(),fill=T,sep="\t",colClasses = "character") > > > > > so the code should be- > > define<- function(x) > { > readline("enter the name of your file: ") > } > > df=read.table(define(),fill=T,sep="\t",colClasses = "character") ##starline > > > if(## some condition here) > { > print("wrong name of the file Please enter again ") > ## some goto function so that it should goto (starline) > } > > > > > > > > Thanking you, > Warm Regards > Vikas Bansal > Msc Bioinformatics > Kings College London > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-------------------------------------------------------- Don MacQueen Lawrence Livermore National Laboratory (925) 423-1062 macqueen1 at llnl.gov