Re: [Rd] [R] Open a file which name contains a tilde
Hello, R 3.6.0 on Ubuntu 19.04. Since no one mentioned it, notice that the tilde in the middle of a string needs to be surrounded by spaces to be expanded. The first code line works as expected, only the second is wrong (buggy). path.expand('a~b') #[1] "a~b" path.expand('a ~ b') #[1] "a /home/rui b" Rui Barradas Às 04:27 de 08/06/19, Richard O'Keefe escreveu: ?path.expand Expand a path name, for example by replacing a leading tilde by the user's home directory (if defined on that platform). *A* path name. The argument is a character vector. If multiple path names are passed, they are passed On most builds of R *A LEADING* "~user" will be replaced... Nothing is said in the R documentation about *multiple* or *non-leading* tildes being replaced. The actual behaviour is inconsistent with the documentation. SOMETHING is a bug. It's not clear to me why this is in any way dependent on readline. I've implemented tilde expansion several times and always without readline. It sounds as though R might be calling tilde_expand() when it *should*, to be consistent with the documentation, be calling tilde_expand_word(). as separate elements of the character vector. On Sat, 8 Jun 2019 at 04:10, Berry, Charles wrote: On Jun 6, 2019, at 2:04 PM, Richard O'Keefe wrote: How can expanding tildes anywhere but the beginning of a file name NOT be considered a bug? I think that that IS what libreadline is doing if one allows a whitespace separated list of file names. As reported in R-help, https://www.mail-archive.com/r-help@r-project.org/msg254116.html path.expand seems to expand tildes beginning whitespace separated strings that could be filenames, but not other tildes. Thus, path.expand("~/.newsrc ~/.R/*") # expands both tildes [1] "/Users/cberry/.newsrc /Users/cberry/.R/*" path.expand("~/.newsrc~/.R/*") # expands only the first [1] "/Users/cberry/.newsrc~/.R/*" This could be a feature if what one wanted was to pass the result to a system command that will process multiple file arguments, e.g. system(paste( "wc", path.expand("~/.newsrc ~/.R/*"))) # run wc on some files I doubt that this was intended by R-core, but perhaps the readline devs had this in mind. Chuck On Thu, 6 Jun 2019 at 23:04, Ivan Krylov wrote: On Wed, 5 Jun 2019 18:07:15 +0200 Frank Schwidom wrote: +> path.expand("a ~ b") [1] "a /home/user b" How can I switch off any file crippling activity? It doesn't seem to be possible if readline is enabled and works correctly. Calls to path.expand [1] end up [2] in R_ExpandFileName [3], which calls R_ExpandFileName_readline [4], which uses libreadline function tilde_expand [5]. tilde_expand seems to be designed to expand '~' anywhere in the string it is handed, i.e. operate on whole command lines, not file paths. I am taking the liberty of Cc-ing R-devel in case this can be considered a bug. -- Best regards, Ivan [1] https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/main/names.c#L807 [2] https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/main/platform.c#L1915 [3] https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/unix/sys-unix.c#L147 [4] https://github.com/wch/r-source/blob/12d1d2d232d84aa355e48b81180a0e2c6f2f/src/unix/sys-std.c#L494 [5] https://git.savannah.gnu.org/cgit/readline.git/tree/tilde.c?h=devel#n187 __ r-h...@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. [[alternative HTML version deleted]] [[alternative HTML version deleted]] __ r-h...@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Determining the exit code of an "almost finished" R script
Dear All, I'm using "reg.finalizer" in a function that is to be called in R scripts to do some cleanup on success. I have not found a way to run the function only if the script run without errors, so when the exit code is expected to be 0. What I've tried is checking "geterrmessage()", but unfortunately it's not perfect: if an error was handled with eg "tryCatch" previously, that message still shows up there. Is there any better way of figuring out in "reg.finalizer" or ".Last" if the session run OK or exiting with !0 exit code? Thanks, Gergely [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Determining the exit code of an "almost finished" R script
On 08/06/2019 7:42 a.m., Gergely Daróczi wrote: Dear All, I'm using "reg.finalizer" in a function that is to be called in R scripts to do some cleanup on success. I have not found a way to run the function only if the script run without errors, so when the exit code is expected to be 0. What I've tried is checking "geterrmessage()", but unfortunately it's not perfect: if an error was handled with eg "tryCatch" previously, that message still shows up there. Is there any better way of figuring out in "reg.finalizer" or ".Last" if the session run OK or exiting with !0 exit code? Can't you just put the line to run it as the last line of your script? You won't get there if there was an error. The point of reg.finalizer is to run code even if there was an error; your situation is much simpler. Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Determining the exit code of an "almost finished" R script
On Sat, Jun 8, 2019 at 2:13 PM Duncan Murdoch wrote: > > On 08/06/2019 7:42 a.m., Gergely Daróczi wrote: > > Dear All, > > > > I'm using "reg.finalizer" in a function that is to be called in R scripts > > to do some cleanup on success. I have not found a way to run the function > > only if the script run without errors, so when the exit code is expected to > > be 0. > > > > What I've tried is checking "geterrmessage()", but unfortunately it's not > > perfect: if an error was handled with eg "tryCatch" previously, that > > message still shows up there. > > > > Is there any better way of figuring out in "reg.finalizer" or ".Last" if > > the session run OK or exiting with !0 exit code? > > Can't you just put the line to run it as the last line of your script? > You won't get there if there was an error. Thank you very much, Duncan, but unfortunately, that's not feasible. "reg.finalizer" is called from a function early in the script, which function first checks when the same script last run and optionally exists early. If that optional early exit (within the helper function) doesn't happen, then the "reg.finalizer" call sets a status update when the R script has finished, but that should only happen if the script run without any problems. Of course, this helper could be split into two -- (1) function call at the beginning of the script and (2) another at the end, but it would be much less elegant and error-prone. I know I'm trying to hack-in some features in my R scripts that should be better handled by an external job scheduler, but I hope this is doable. > > > The point of reg.finalizer is to run code even if there was an error; > your situation is much simpler. > > Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Determining the exit code of an "almost finished" R script
On 08/06/2019 9:55 a.m., Gergely Daróczi wrote: On Sat, Jun 8, 2019 at 2:13 PM Duncan Murdoch wrote: On 08/06/2019 7:42 a.m., Gergely Daróczi wrote: Dear All, I'm using "reg.finalizer" in a function that is to be called in R scripts to do some cleanup on success. I have not found a way to run the function only if the script run without errors, so when the exit code is expected to be 0. What I've tried is checking "geterrmessage()", but unfortunately it's not perfect: if an error was handled with eg "tryCatch" previously, that message still shows up there. Is there any better way of figuring out in "reg.finalizer" or ".Last" if the session run OK or exiting with !0 exit code? Can't you just put the line to run it as the last line of your script? You won't get there if there was an error. Thank you very much, Duncan, but unfortunately, that's not feasible. "reg.finalizer" is called from a function early in the script, which function first checks when the same script last run and optionally exists early. If that optional early exit (within the helper function) doesn't happen, then the "reg.finalizer" call sets a status update when the R script has finished, but that should only happen if the script run without any problems. Of course, this helper could be split into two -- (1) function call at the beginning of the script and (2) another at the end, but it would be much less elegant and error-prone. I know I'm trying to hack-in some features in my R scripts that should be better handled by an external job scheduler, but I hope this is doable. I still think you're using reg.finalizer() in a way it's not designed to work, and this makes it more complicated than necessary. The strategy of splitting into two seems safer to me: you never know when a finalizer will be called, because it is triggered by garbage collections, and those can happen asynchronously, not under your control. It is nice to have all code for some purpose in one place, so if you really want that, you could put together your own explicitly called finalizer, something like this: finalizers <- list() addFinalizer <- function(fn) { finalizers <<- c(finalizers, list(fn)) } runFinalizers <- function() { for (i in rev(seq_along(finalizers))) { # Run in reverse order finalizers[[i]]() # Call the finalizer finalizers[[i]] <- NULL # Allow related objects to be released } } In the place you now call reg.finalizer(), you call addFinalizer() instead; so all code specific to that task remains local. At the end of your script if things have been successful, you call runFinalizers(). Duncan Murdoch The point of reg.finalizer is to run code even if there was an error; your situation is much simpler. Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel