[R] path + name of the running R file

Jim Lemon drjimlemon at gmail.com
Wed Mar 2 09:13:14 CET 2016


Hi Maurice,
If you have used "source" to run an R script, perhaps you could try
something like this:

get.running.file<-function(pattern="source",...) {
 file1 <- tempfile("Rrawhist")
 savehistory(file1)
 rawhist <- readLines(file1)
 unlink(file1)
 last.source<-grep(pattern,rev(rawhist),value=TRUE)[1]
 sourced.file<-unlist(strsplit(last.source,'"'))[2]
 return(sourced.file)
}

This is taken from the "history" function and tries to find the last
"source" command in the command history and extract the name of the
sourced file. It is very rough, but it does work as long as the first
argument to the "source" function is the quoted name of the sourced
file.

Jim


On Wed, Mar 2, 2016 at 6:01 PM, MAURICE Jean - externe
<jean-externe.maurice at edf.fr> wrote:
> Hi,
>
> Is it possible to get the path and name of the running R file ?
>
> Jean
>
>
>
>
> Ce message et toutes les pièces jointes (ci-après le 'Message') sont établis à l'intention exclusive des destinataires et les informations qui y figurent sont strictement confidentielles. Toute utilisation de ce Message non conforme à sa destination, toute diffusion ou toute publication totale ou partielle, est interdite sauf autorisation expresse.
>
> Si vous n'êtes pas le destinataire de ce Message, il vous est interdit de le copier, de le faire suivre, de le divulguer ou d'en utiliser tout ou partie. Si vous avez reçu ce Message par erreur, merci de le supprimer de votre système, ainsi que toutes ses copies, et de n'en garder aucune trace sur quelque support que ce soit. Nous vous remercions également d'en avertir immédiatement l'expéditeur par retour du message.
>
> Il est impossible de garantir que les communications par messagerie électronique arrivent en temps utile, sont sécurisées ou dénuées de toute erreur ou virus.
> ____________________________________________________
>
> This message and any attachments (the 'Message') are intended solely for the addressees. The information contained in this Message is confidential. Any use of information contained in this Message not in accord with its purpose, any dissemination or disclosure, either whole or partial, is prohibited except formal approval.
>
> If you are not the addressee, you may not copy, forward, disclose or use any part of it. If you have received this message in error, please delete it and all copies from your system and notify the sender immediately by return message.
>
> E-mail communication cannot be guaranteed to be timely secure, error or virus-free.
>
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list