Sweave support cleanup (patch from yihui, see #7555)

http://www.lyx.org/trac/ticket/7555

This part is a simple cleanup:

 * rename variables to start with a dot
 * reset encoding after running sweave
 * improve on-screen display of code chunks



git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@39506 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Jean-Marc Lasgouttes 2011-08-23 09:52:21 +00:00
parent 2ba3007e4b
commit c9d9bde0b2
2 changed files with 21 additions and 18 deletions

View File

@ -1,6 +1,6 @@
#\DeclareLyXModule[sweave->latex,fancyvrb.sty]{Sweave} #\DeclareLyXModule[sweave->latex,fancyvrb.sty]{Sweave}
#DescriptionBegin #DescriptionBegin
#Allows to use the statistical language S/R as a literate programming tool via Sweave package. #Allows to use the statistical language S/R as a literate programming tool via the Sweave() function.
#See sweave.lyx in examples. #See sweave.lyx in examples.
#DescriptionEnd #DescriptionEnd
#Category: literate #Category: literate
@ -16,7 +16,7 @@ End
AddToPreamble AddToPreamble
<<echo=F>>= <<echo=F>>=
if(exists("ls.enc")) options(encoding=ls.enc) if(exists(".orig.enc")) options(encoding = .orig.enc)
@ @
EndPreamble EndPreamble
@ -27,6 +27,8 @@ Style Chunk
Margin static Margin static
Align Left Align Left
AlignPossible Block, Left, Right, Center AlignPossible Block, Left, Right, Center
TopSep 0.7
BottomSep 0.7
NewLine 0 NewLine 0
FreeSpacing 1 FreeSpacing 1
PassThru 1 PassThru 1

View File

@ -13,7 +13,7 @@
# argument 3 is the iconv name for the encoding of the file # argument 3 is the iconv name for the encoding of the file
# argument 4 is the original document directory # argument 4 is the original document directory
ls.args <- commandArgs(trailingOnly=TRUE) .cmdargs <- commandArgs(trailingOnly=TRUE)
# check whether Sweave.sty is seen by LaTeX. if it is not, we will # check whether Sweave.sty is seen by LaTeX. if it is not, we will
# copy it alongside the .tex file (in general in the temporary # copy it alongside the .tex file (in general in the temporary
@ -21,35 +21,36 @@ ls.args <- commandArgs(trailingOnly=TRUE)
# but this is a problem of installation of R on the user's machine. # but this is a problem of installation of R on the user's machine.
# The advantage compared to the use of stylepath, is that the exported # The advantage compared to the use of stylepath, is that the exported
# .tex file will be portable to another machine. (JMarc) # .tex file will be portable to another machine. (JMarc)
ls.sweavesty <- system("kpsewhich Sweave.sty", intern=TRUE, ignore.stderr=TRUE) if (!length(system("kpsewhich Sweave.sty", intern=TRUE, ignore.stderr=TRUE))) {
if (!length(ls.sweavesty)) { file.copy(file.path(R.home("share"), "texmf", "tex", "latex", "Sweave.sty"),
stypath <- file.path(R.home("share"), "texmf", "tex", "latex", "Sweave.sty") dirname(.cmdargs[2]), overwrite=TRUE)
file.copy(stypath, dirname(ls.args[2]), overwrite=TRUE)
} }
# set default encoding for input and output files; ls.enc is used in # set default encoding for input and output files; .orig.enc is used in
# the sweave module preamble to reset the encoding to what it was. # the sweave module preamble to reset the encoding to what it was.
ls.enc <- getOption("encoding") .orig.enc <- getOption("encoding")
options(encoding=ls.args[3]) options(encoding=.cmdargs[3])
# Change current directory to the document directory, so that R can find # Change current directory to the document directory, so that R can find
# data files. # data files.
setwd(ls.args[4]) setwd(.cmdargs[4])
# this is passed as a prefix.string to tell where temporary files should go # this is passed as a prefix.string to tell where temporary files should go
# the output file without extension and without '.' # the output file without extension and without '.'
tmpout <- gsub(".", "-", sub("\\.tex$", "", basename(ls.args[2])), fixed = TRUE) tmpout <- gsub(".", "-", sub("\\.tex$", "", basename(.cmdargs[2])), fixed = TRUE)
# replace # replace
ls.pr <- paste(dirname(ls.args[2]), tmpout, sep="/") .prefix.str <- paste(dirname(.cmdargs[2]), tmpout, sep="/")
rm(tmpout)
# finally run sweave # finally run sweave
Sweave(file=ls.args[1], output=ls.args[2], syntax="SweaveSyntaxNoweb", prefix.string=ls.pr) Sweave(file=.cmdargs[1], output=.cmdargs[2], syntax="SweaveSyntaxNoweb", prefix.string=.prefix.str)
# remove absolute path from \includegraphics # remove absolute path from \includegraphics
ls.doc = readLines(ls.args[2]) options(encoding=.cmdargs[3]) # encoding has been changed in the preamble chunk
ls.cmd = paste('\\includegraphics{', dirname(ls.args[2]), "/", sep = "") ls.doc = readLines(.cmdargs[2])
ls.cmd = paste('\\includegraphics{', dirname(.cmdargs[2]), "/", sep = "")
ls.idx = grep(ls.cmd, ls.doc, fixed = TRUE) ls.idx = grep(ls.cmd, ls.doc, fixed = TRUE)
if (length(ls.idx)) { if (length(ls.idx)) {
ls.doc[ls.idx] = sub(ls.cmd, "\\includegraphics{", ls.doc[ls.idx], fixed = TRUE) ls.doc[ls.idx] = sub(ls.cmd, "\\includegraphics{", ls.doc[ls.idx], fixed = TRUE)
writeLines(ls.doc, ls.args[2]) writeLines(ls.doc, .cmdargs[2])
} }