Hi Rrof. Goulet, Thank you very much for sharing the function. It works.
Now I noticed that the return value of commandArgs() inside Rnw with ess includes a working Rnw file name. \begin{Schunk} \begin{Sinput} > commandArgs() \end{Sinput} \begin{Soutput} [1] "/usr/lib/R/bin/exec/R" [2] "--no-restore" [3] "--no-echo" [4] "--args" [5] "nextArghogehoge.RnwnextArg--options=output='hogehoge-woven.tex'" \end{Soutput} \end{Schunk} -- Susumu Tanimura On Wed, 8 May 2024 22:17:36 +0000 Vincent Goulet <vincent.gou...@act.ulaval.ca> wrote: > 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@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@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/ess-help > ______________________________________________ ESS-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/ess-help