Hi, this may be slightly off-topic, but as you are the experts: we have written a small vignette, in which we want to refer to .R, .Rd source files by means of relative paths. More specifically, we want to use TeX package "listings" to include source code, [which btw in the mean time works pretty well together with "fancyvrb" so can be used with Sweave without problems...] To avoid redundancy we would like to use \lstinputlisting[firstline=<n1>,lastline=<n2>]{<r-source-file.R>} and to do so we somehow need path information. This is /not/ a "listings" issue, as we might also have used \input{<r-source-file.R>}, only is \lstinputlisting a bit more flexible... Using relative paths, i.e. as the vignette resides in subfolder "inst/doc" something like ../../../<mypkg>/R/<myR.R> or ../../../<mypkg>/man/<myRd.Rd> does the job for both R CMD build and R CMD check , --- in standard configurations. However, as you may change the location of the check folder with the -o option of R CMD check, (and possibly other things, we have not yet thought of ...), our solution is not quite satisfactory, so we have been wondering whether there is a (platform-independent) way to access the package source folder (under check) from within TeX. --- or if you prefer to solve it from R-side: We would appreciate an Sweave-chunk to do the following: +have three arguments firstline, lastline, filename [where filename is relative to the package source folder] +read out the information about the path to the package source folder [from the env-variable?] +with this information read in the part of the source file between <firstline> and <lastline> and places this content ---without wrapping it to \begin{Schunk} ... \end{Schunk}--- into the .tex file ---preferrably already into a \begin{listing}\end{listing} environment... Any suggestions how to resolve this? Thank you already, Peter
See ?system.file with the package= argument. On Fri, Oct 10, 2008 at 2:19 PM, Peter Ruckdeschel <peter.ruckdeschel at web.de> wrote:> Hi, > > this may be slightly off-topic, but as you are the experts: > > we have written a small vignette, in which we want to refer to > .R, .Rd source files by means of relative paths. > > More specifically, we want to use TeX package "listings" to include > source code, > > [which btw in the mean time works pretty well together with "fancyvrb" > so can be used > with Sweave without problems...] > > To avoid redundancy we would like to use > > \lstinputlisting[firstline=<n1>,lastline=<n2>]{<r-source-file.R>} > > and to do so we somehow need path information. > > This is /not/ a "listings" issue, as we might also have used > \input{<r-source-file.R>}, only is \lstinputlisting a bit more > flexible... > > Using relative paths, i.e. as the vignette resides in subfolder "inst/doc" > something like > > ../../../<mypkg>/R/<myR.R> or ../../../<mypkg>/man/<myRd.Rd> > > does the job for both R CMD build and R CMD check , > --- in standard configurations. > > However, as you may change the location of the check folder with > the -o option of R CMD check, (and possibly other things, we have > not yet thought of ...), our solution is not quite satisfactory, > so we have been wondering whether there is a (platform-independent) > way to access the package source folder (under check) from within > TeX. > > --- or if you prefer to solve it from R-side: > > We would appreciate an Sweave-chunk to do the following: > +have three arguments firstline, lastline, filename > [where filename is relative to the package source folder] > +read out the information about the path to the package > source folder [from the env-variable?] > +with this information read in the part of the source file > between <firstline> and <lastline> > and places this content ---without wrapping it to > \begin{Schunk} ... \end{Schunk}--- into the .tex file > ---preferrably already into a \begin{listing}\end{listing} > environment... > > Any suggestions how to resolve this? > > Thank you already, > Peter > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Gabor Grothendieck schrieb:> See ?system.file with the package= argument. >Thank you Gabor, but this refers to the /installed/ package, while we are needing path information about the not-yet-built source code of the package during R CMD check / build. Peter
Try placing a copy of the files in the inst directory and then accessing them via system.files("myfile.R", package = "mypackage") or place them in the same directory as the Sweave file and then access them without a directory path at all: readLines("myfile.R") On Fri, Oct 10, 2008 at 2:48 PM, Peter Ruckdeschel <peter.ruckdeschel at web.de> wrote:> Gabor Grothendieck schrieb: >> See ?system.file with the package= argument. >> > > Thank you Gabor, > > but this refers to the /installed/ package, while we are needing > path information about the not-yet-built source code of the > package during R CMD check / build. > > Peter >
Thanks again Gabor, for your quick reply,> Try placing a copy of the files in the inst directory and then > accessing them via system.files("myfile.R", package = "mypackage") >you mean I should do this in an S-chunk in the .Rnw file? I.e., running Sweave on it would then produce the copy into my Sweave file I need? This could indeed be an opition, but as noted in my reply to Robert, my guess is that in the library where you install your packages to and which is found with system.file(), you will no longer find the R source files but rather some .rdb or .rdx file. At least this is what I found --- or is there some magic trick to get back the source files from this?> or place them in the same directory as the Sweave file > and then access them without a directory path at all: > readLines("myfile.R") >If I put the sources statically into the same directory as the Sweave file (i.e. in [...]/myRsources/myPkg/inst/doc), of course everything works fine; The issue is that I want to "dynamically" include parts of the source R code itself (without unnecessary static copying the source R code due to consistency traps, when changing the R code but not the copy) from the R directory of the package into the Sweave file by something like \input{[Path]/myRfile.R} so that once I change the R code, I do not have to change the .Rnw file. [I do not include the whole .R file but only certain lines of it, of course] As already said, in a standard configuration something like \input{../../../myPkg/R/myRfile.R} % goes from [...]/myRSources/myPkg/inst/src to [...]/myRSources/myPkg/R % in R CMD build % resp. from [...]/myRSources/myPkg.Rcheck/inst/src to [...]/myRSources/myPkg/R % in R CMD check does the job. Now if I am in [...]/myRSources and say something like R CMD build myPkg ### still works R CMD check myPkg -o /yetAnotherPath/myCheck ## does not work as /yetAnotherPath/myPkg/R need not be [...]/myPkg/R So what I was looking for was something like defining an environment variable $myRSources (in a platform-independent way) which may be accessed (in a platform-independent way) from the TeX command \input{...} --- perhaps with something like \input{$myRSources/myPkg/R/myRfile.R} but I cannot figure out how to do this... Thank you once again Peter