Hello, I have a file reading function that prompts the user with a file dialog if a filename is not provided in the argument list. It is desirable to return gracefully if the user selects "Cancel", but file.choose() throws an error instead of returning something like a character. > file.choose() [1] "/Users/ben/ben_idl.pref" > file.choose() Error in file.choose() : file choice cancelled I naively planned to use nchar() to test the length, assuming cancellation would return a zero-length character. That appears to be out of the question. Are there other options available in the base package? Thanks! Ben
Try this: tryCatch(file.choose(), error = function(e) "") On 6/6/07, Ben Tupper <btupper at bigelow.org> wrote:> Hello, > > > I have a file reading function that prompts the user with a file dialog > if a filename is not provided in the argument list. It is desirable to > return gracefully if the user selects "Cancel", but file.choose() throws > an error instead of returning something like a character. > > > file.choose() > [1] "/Users/ben/ben_idl.pref" > > > file.choose() > Error in file.choose() : file choice cancelled > > I naively planned to use nchar() to test the length, assuming > cancellation would return a zero-length character. That appears to be > out of the question. Are there other options available in the base package? > > Thanks! > Ben > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
See ?tryCatch. Example: Function returning NULL if cancelled: fileChoose <- function(...) { pathname <- NULL; tryCatch({ pathname <- file.choose(); }, error = function(ex) { }) pathname; } /Henrik On 6/6/07, Ben Tupper <btupper at bigelow.org> wrote:> Hello, > > > I have a file reading function that prompts the user with a file dialog > if a filename is not provided in the argument list. It is desirable to > return gracefully if the user selects "Cancel", but file.choose() throws > an error instead of returning something like a character. > > > file.choose() > [1] "/Users/ben/ben_idl.pref" > > > file.choose() > Error in file.choose() : file choice cancelled > > I naively planned to use nchar() to test the length, assuming > cancellation would return a zero-length character. That appears to be > out of the question. Are there other options available in the base package? > > Thanks! > Ben > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
On 6/6/07, Ben Tupper <btupper at bigelow.org> wrote:> Hello, > > > I have a file reading function that prompts the user with a file dialog > if a filename is not provided in the argument list. It is desirable to > return gracefully if the user selects "Cancel", but file.choose() throws > an error instead of returning something like a character. > > > file.choose() > [1] "/Users/ben/ben_idl.pref" > > > file.choose() > Error in file.choose() : file choice cancelled > > I naively planned to use nchar() to test the length, assuming > cancellation would return a zero-length character. That appears to be > out of the question. [...]Not really, errors can be caught and handled. This seems to give the behaviour you want: tryCatch(file.choose(), error = function(e) "") -Deepayan
Deepayan Sarkar wrote:> On 6/6/07, Ben Tupper <btupper at bigelow.org> wrote: >> Hello, >> >> >> I have a file reading function that prompts the user with a file dialog >> if a filename is not provided in the argument list. It is desirable to >> return gracefully if the user selects "Cancel", but file.choose() throws >> an error instead of returning something like a character. >> >> > file.choose() >> [1] "/Users/ben/ben_idl.pref" >> >> > file.choose() >> Error in file.choose() : file choice cancelled >> >> I naively planned to use nchar() to test the length, assuming >> cancellation would return a zero-length character. That appears to be >> out of the question. [...] > > Not really, errors can be caught and handled. This seems to give the > behaviour you want: > > tryCatch(file.choose(), error = function(e) "") > > -DeepayanThanks all, I am very pleased to know about conditions in the base package. Slick! Ben