[ESS] How to get working Rnw file name inside script

Vincent Goulet V|ncent@Gou|et @end|ng |rom @ct@u|@v@|@c@
Thu May 9 00:17:36 CEST 2024


I'm not sure this is what you're looking for, but here goes. I have been using the following function for a number of years to extract the name of the file from which Sweave is called.

###
### getSourceName()
###
##  Extract the name of the file used in the command 'Sweave' calling
##  this function.
##
##  Value
##
##  Filename as a character string
##
getSourceName <- function()
{
   args <- commandArgs(FALSE)

   ## If Sweave is launched with
   ##
   ##   R -e "Sweave('foo.Rnw', ...)"
   ##
   ## the expression is one of the arguments and we can extract the
   ## filename.
   if (length(cmd <- grep("Sweave\\(", args, value = TRUE)))
       return(regmatches(cmd,
                         regexpr("(?<=Sweave\\(['\"]).*(?=['\"])",
                                 cmd, perl = TRUE)))

   ## If Sweave is launched with
   ##
   ##   R CMD Sweave --encoding="utf-8" foo.Rnw
   ##
   ## the argument containing the relevant information is a character
   ## string of the form
   ##
   ##   nextArg--encoding=utf-8nextArgfoo.Rnw
   ##
   ## The arguments are in the order they were entered at the command line.
   if (length(cmd <- grep("^nextArg", args, value = TRUE)))
       return(grep("^--|^$", strsplit(cmd, 'nextArg', fixed = TRUE)[[1L]],
                   invert = TRUE, value = TRUE))
}

Hope this helps!

v.

Vincent Goulet
Professeur titulaire
École d'actuariat, Université Laval

> Le 28 avr. 2024 à 01:57, Susumu Tanimura via ESS-help <ess-help using r-project.org> a écrit :
> 
> Dear all,
> 
> When using the Stangle() function within an Rnw file to insert an
> entire R code into an appendix of a document, it is necessary to
> provide the working Rnw file name. While manually entering the name is
> a viable option, it would be beneficial for me to implement it within a
> template for Rnw files. 
> 
> My question is how to get working Rnw file name inside script.
> 
> This issue is not directly related to ESS, but the solution must be
> compatible with ESS and Emacs.
> 
> Thank you
> 
> -- 
> Susumu Tanimura
> 
> ______________________________________________
> ESS-help using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/ess-help



More information about the ESS-help mailing list