[Sciviews-commits] r518 - in pkg/svSweave: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jan 27 21:44:40 CET 2013


Author: phgrosjean
Date: 2013-01-27 21:44:40 +0100 (Sun, 27 Jan 2013)
New Revision: 518

Modified:
   pkg/svSweave/DESCRIPTION
   pkg/svSweave/NEWS
   pkg/svSweave/R/cleanLyxRnw.R
   pkg/svSweave/R/tangleLyxRnw.R
   pkg/svSweave/R/weaveLyxRnw.R
   pkg/svSweave/man/cleanLyxRnw.Rd
   pkg/svSweave/man/svSweave-package.Rd
Log:
weaveLyxRnw() and tangleLyxRnw() now detect when  the LyX doxument uses the SciViews Knitr module and they automatically switch to knitr to process it.
There is now also an additional argument: logFile, which indicate what log file to use.

Modified: pkg/svSweave/DESCRIPTION
===================================================================
--- pkg/svSweave/DESCRIPTION	2013-01-27 17:57:21 UTC (rev 517)
+++ pkg/svSweave/DESCRIPTION	2013-01-27 20:44:40 UTC (rev 518)
@@ -1,7 +1,7 @@
 Package: svSweave
 Type: Package
-Version: 0.9-7
-Date: 2013-01-11
+Version: 0.9-8
+Date: 2013-01-27
 Title: SciViews GUI API - Sweave functions
 Authors at R: c(person("Philippe", "Grosjean", role = c("aut", "cre"),
   email = "phgrosjean at sciviews.org"))

Modified: pkg/svSweave/NEWS
===================================================================
--- pkg/svSweave/NEWS	2013-01-27 17:57:21 UTC (rev 517)
+++ pkg/svSweave/NEWS	2013-01-27 20:44:40 UTC (rev 518)
@@ -1,5 +1,15 @@
 = svSweave News
 
+== Changes in svSweave 0.9-8
+
+* weaveLyxRnw(), knitLyxRnw(), tangleLyxRnw() and purlLyxRnw() now have a
+  new argument logFile, indicating which log file to use.
+  
+* cleanLyxRnw() now detects if the SciViews Knitr LyX module is used, and it
+  automatically switches to knitr to knit or purl the document in weaveLyxRnw()
+  and tangleLyxRnw().
+
+
 == Changes in svSweave 0.9-7
 
 * All Asciidoc-related items are now moved to svDoc package.

Modified: pkg/svSweave/R/cleanLyxRnw.R
===================================================================
--- pkg/svSweave/R/cleanLyxRnw.R	2013-01-27 17:57:21 UTC (rev 517)
+++ pkg/svSweave/R/cleanLyxRnw.R	2013-01-27 20:44:40 UTC (rev 518)
@@ -1,5 +1,8 @@
 cleanLyxRnw <- function (RnwCon, RnwCon2 = RnwCon, encoding = "UTF-8")
 {
+	## By default, it is supposed to be a Sweave document
+	opts <- list(kind = "Sweave")
+	
 	## Run in LyX as the Sweave copier using something like:
 	##> R -e svSweave::cleanLyxRnw(\"$$i\",\"$$o\") -q --vanilla --slave
 	
@@ -10,6 +13,9 @@
 	## Read the data in the Rnw file
 	Rnw <- readLines(RnwCon, encoding = encoding)
 
+	## Is it a knitr document?
+	if (any(grepl("\\%Sweave-kind=knitr", Rnw))) opts$kind <- "Knitr"
+	
 	## If the Rnw file is produced with LyX and SciViews Sweave module, chunks are
 	## separated by \rchunk{<<[pars]>>= ... @}
 
@@ -27,8 +33,10 @@
 	chunk <- parts %% 2 > 0 	# R chunks are odd parts
 
 	## Do we need to change something?
-	if (!any(chunk))
-		return(writeLines(Rnw, RnwCon2))
+	if (!any(chunk)) {
+		writeLines(Rnw, RnwCon2)
+		return(invisible(opts))
+	}
 
 	## Eliminate empty strings not followed by empty strings inside chunks
 	Rnw[chunk & Rnw == "" & Rnw != c(Rnw[-1], " ")] <- NA
@@ -45,5 +53,8 @@
 
 	## Write the result to the new .Rnw file
 	## TODO: test useBytes = TRUE!
-	return(writeLines(Rnw, RnwCon2, useBytes = TRUE))
+	writeLines(Rnw, RnwCon2, useBytes = TRUE)
+	
+	## Invisibly return opts
+	invisible(opts)
 }

Modified: pkg/svSweave/R/tangleLyxRnw.R
===================================================================
--- pkg/svSweave/R/tangleLyxRnw.R	2013-01-27 17:57:21 UTC (rev 517)
+++ pkg/svSweave/R/tangleLyxRnw.R	2013-01-27 20:44:40 UTC (rev 518)
@@ -1,12 +1,10 @@
-## TODO: merge both functions and use a tag in the document to decide
-##       if using sweave or knitr
-
 tangleLyxRnw <- function (file, driver = Rtangle(),
 syntax = getOption("SweaveSyntax"), encoding = "UTF-8", width = 80,
-useFancyQuotes = TRUE, annotate = TRUE, ...)
+useFancyQuotes = TRUE, annotate = TRUE, logFile = file.path(tempdir(),
+".lyxSweave.log"), ...)
 {
 	## Run in LyX as the Sweave -> R converter:
-	##> R -e svSweave::tangleLyxRnw(\"$$i\"[,annotate=FALSE]) -q --vanilla
+	##> R -e svSweave::tangleLyxRnw(\"$$i\",log=\"/tmp/.lyxSweave.log\"[,annotate=FALSE]) -q --vanilla
 	
 	## Switch encoding (we do work with UTF-8 by default)
 	oenc <- options(encoding = encoding)
@@ -29,32 +27,40 @@
 	if (!file.exists(file)) {
 		stop("You must provide the name of an existing .Rnw file to process!")
 	} else {
-				## Redirect output
-		unlink("/tmp/.lyxSweave.log")
+		## Redirect output
+		unlink(logFile)
 		on.exit({
-			## Echo results
 			sink(type = "message")
 			sink()
-			try(cat(readLines("/tmp/.lyxSweave.log"), sep = "\n"), silent = TRUE)
+			## Echo results
+			try(cat(readLines(logFile), sep = "\n"), silent = TRUE)
 		}, add = TRUE)
-		con <- file("/tmp/.lyxSweave.log", open = "wt")
+		con <- file(logFile, open = "wt")
 		sink(con)
 		sink(con, type = "message")
 		cat("Tangling ", basename(file), " ...\n", sep = "")
 		
 		## Clean the R noweb file
-		cleanLyxRnw(file, encoding = encoding)
+		opts <- cleanLyxRnw(file, encoding = encoding)
 		
-		## Weave the file
-		Stangle(file, driver = driver, syntax = syntax, annotate = annotate, ...)	
+		## Tangle or Purl the file
+		if (!is.list(opts) || !length(opts$kind))
+			stop("no Sweave kind defined (must be Sweave or Knitr)")
+		cat("Processing the document using ", opts$kind, "\n", sep = "")
+		switch(opts$kind,
+			Sweave = Stangle(file, driver = driver, syntax = syntax,
+				annotate = annotate, ...),
+			Knitr = purl(file, ...),
+			stop("Wrong kind of Sweave document: '", opts$kind, "'")
+		)	
 	}
 }
 
 purlLyxRnw <- function (file, encoding = "UTF-8", width = 80,
-useFancyQuotes = TRUE, ...)
+useFancyQuotes = TRUE, logFile = file.path(tempdir(), ".lyxSweave.log"), ...)
 {
 	## Run in LyX as the Sweave -> R converter:
-	##> R -e svSweave::tangleLyxRnw(\"$$i\"[,annotate=FALSE]) -q --vanilla
+	##> R -e svSweave::tangleLyxRnw(\"$$i\",log=\"/tmp/.lyxSweave.log\"[,annotate=FALSE]) -q --vanilla
 
 	## Switch encoding (we do work with UTF-8 by default)
 	oenc <- options(encoding = encoding)
@@ -77,15 +83,15 @@
 	if (!file.exists(file)) {
 		stop("You must provide the name of an existing .Rnw file to process!")
 	} else {
-				## Redirect output
-		unlink("/tmp/.lyxSweave.log")
+		## Redirect output
+		unlink(logFile)
 		on.exit({
-			## Echo results
 			sink(type = "message")
 			sink()
-			try(cat(readLines("/tmp/.lyxSweave.log"), sep = "\n"), silent = TRUE)
+			## Echo results
+			try(cat(readLines(logFile), sep = "\n"), silent = TRUE)
 		}, add = TRUE)
-		con <- file("/tmp/.lyxSweave.log", open = "wt")
+		con <- file(logFile, open = "wt")
 		sink(con)
 		sink(con, type = "message")
 		cat("Tangling ", basename(file), " using knitr...\n", sep = "")
@@ -93,7 +99,7 @@
 		## Clean the R noweb file
 		cleanLyxRnw(file, encoding = encoding)
 		
-		## Weave the file
+		## Tangle (purl) the file
 		purl(file, ...)	
 	}
 }

Modified: pkg/svSweave/R/weaveLyxRnw.R
===================================================================
--- pkg/svSweave/R/weaveLyxRnw.R	2013-01-27 17:57:21 UTC (rev 517)
+++ pkg/svSweave/R/weaveLyxRnw.R	2013-01-27 20:44:40 UTC (rev 518)
@@ -1,13 +1,11 @@
 ## TODO: see the tables package for better looking tables, including in LaTeX!
-## TODO: merge both functions and use a tag in the document to decide
-##       if using sweave or knitr
 
 weaveLyxRnw <- function (file, driver = RweaveLatex(),
 syntax = getOption("SweaveSyntax"), encoding = "UTF-8", width = 80,
-useFancyQuotes = TRUE, ...)
+useFancyQuotes = TRUE, logFile = file.path(tempdir(), ".lyxSweave.log"), ...)
 {
 	## Run in LyX as the Sweave -> LaTex (from Sweave|pdflatex|plain) converter:
-	##> R -e svSweave::weaveLyxRnw(\"$$i\"[,driver=highlight::HighlightWeaveLatex()]) -q --vanilla
+	##> R -e svSweave::weaveLyxRnw(\"$$i\",log=\"/tmp/.lyxSweave.log\"[,driver=highlight::HighlightWeaveLatex()]) -q --vanilla
 
 	## Switch encoding (we do work with UTF-8 by default)
 	oenc <- options(encoding = encoding)
@@ -31,31 +29,38 @@
 		stop("You must provide the name of an existing .Rnw file to process!")
 	} else {
 		## Redirect output
-		unlink("/tmp/.lyxSweave.log")
+		unlink(logFile)
 		on.exit({
-			## Echo results
 			sink(type = "message")
 			sink()
-			try(cat(readLines("/tmp/.lyxSweave.log"), sep = "\n"), silent = TRUE)
+			## Echo results
+			try(cat(readLines(logFile), sep = "\n"), silent = TRUE)
 		}, add = TRUE)
-		con <- file("/tmp/.lyxSweave.log", open = "wt")
+		con <- file(logFile, open = "wt")
 		sink(con)
 		sink(con, type = "message")
 		cat("Weaving ", basename(file), " ...\n", sep = "")
 		
 		## Clean the R noweb file
-		cleanLyxRnw(file, encoding = encoding)
+		opts <- cleanLyxRnw(file, encoding = encoding)
 		
-		## Weave the file
-		Sweave(file, driver = driver, syntax = syntax, ...)
+		## Weave or Knit the file
+		if (!is.list(opts) || !length(opts$kind))
+			stop("no Sweave kind defined (must be Sweave or Knitr)")
+		cat("Processing the document using ", opts$kind, "\n", sep = "")
+		switch(opts$kind,
+			Sweave = Sweave(file, driver = driver, syntax = syntax, ...),
+			Knitr = knit(file, ...),
+			stop("Wrong kind of Sweave document: '", opts$kind, "'")
+		)	
 	}
 }
 
 knitLyxRnw <- function (file, encoding = "UTF-8", width = 80,
-useFancyQuotes = TRUE, ...)
+useFancyQuotes = TRUE, logFile = file.path(tempdir(), ".lyxSweave.log"), ...)
 {
 	## Run in LyX as the Sweave -> LaTex (from Sweave|pdflatex|plain) converter:
-	##> R -e svSweave::knitLyxRnw(\"$$i\") -q --vanilla
+	##> R -e svSweave::knitLyxRnw(\"$$i\",log=\"/tmp/.lyxSweave.log\") -q --vanilla
 
 	## Switch encoding (we do work with UTF-8 by default)
 	oenc <- options(encoding = encoding)
@@ -79,14 +84,14 @@
 		stop("You must provide the name of an existing .Rnw file to process!")
 	} else {
 		## Redirect output
-		unlink("/tmp/.lyxSweave.log")
+		unlink(logFile)
 		on.exit({
-			## Echo results
 			sink(type = "message")
 			sink()
-			try(cat(readLines("/tmp/.lyxSweave.log"), sep = "\n"), silent = TRUE)
+			## Echo results
+			try(cat(readLines(logFile), sep = "\n"), silent = TRUE)
 		}, add = TRUE)
-		con <- file("/tmp/.lyxSweave.log", open = "wt")
+		con <- file(logFile, open = "wt")
 		sink(con)
 		sink(con, type = "message")
 		cat("Weaving ", basename(file), " with knitr...\n", sep = "")

Modified: pkg/svSweave/man/cleanLyxRnw.Rd
===================================================================
--- pkg/svSweave/man/cleanLyxRnw.Rd	2013-01-27 17:57:21 UTC (rev 517)
+++ pkg/svSweave/man/cleanLyxRnw.Rd	2013-01-27 20:44:40 UTC (rev 518)
@@ -23,12 +23,16 @@
 cleanLyxRnw(RnwCon, RnwCon2 = RnwCon, encoding = "UTF-8")
 
 weaveLyxRnw(file, driver = RweaveLatex(), syntax = getOption("SweaveSyntax"),
-    encoding = "UTF-8", width = 80, useFancyQuotes = TRUE, \dots)
-knitLyxRnw(file, encoding = "UTF-8", width = 80, useFancyQuotes = TRUE, \dots)
+    encoding = "UTF-8", width = 80, useFancyQuotes = TRUE,
+    logFile = file.path(tempdir(), ".lyxSweave.log"), \dots)
+knitLyxRnw(file, encoding = "UTF-8", width = 80, useFancyQuotes = TRUE,
+    logFile = file.path(tempdir(), ".lyxSweave.log"), \dots)
 
 tangleLyxRnw(file, driver = Rtangle(), syntax = getOption("SweaveSyntax"),
-    encoding = "UTF-8", width = 80, useFancyQuotes = TRUE, annotate = TRUE, \dots)
-purlLyxRnw(file, encoding = "UTF-8", width = 80, useFancyQuotes = TRUE, \dots)
+    encoding = "UTF-8", width = 80, useFancyQuotes = TRUE, annotate = TRUE,
+    logFile = file.path(tempdir(), ".lyxSweave.log"), \dots)
+purlLyxRnw(file, encoding = "UTF-8", width = 80, useFancyQuotes = TRUE,
+    logFile = file.path(tempdir(), ".lyxSweave.log"), \dots)
 }
 
 \arguments{
@@ -44,17 +48,20 @@
     string with its name,  see \code{?Sweave} }
   \item{width}{ the width used for outputs, 80 characters by default }
   \item{useFancyQuotes}{ do we use fancy quotes in R outputs? }
-  \item{annotate}{ is the R code extracted from the .Rnw file annotated? } 
+  \item{annotate}{ is the R code extracted from the .Rnw file annotated? }
+  \item{logFile}{ the file to use to log results of weaving/tangling the document }
   \item{\dots}{ further arguments passed to the driver's setup function of
     \code{Sweave()} or \code{Stangle()} }
 }
 
 \value{
-  \code{NULL}, these functions are invoked for their side effects. The function
-  \code{weaveLyxRnw()} uses the standard Sweave driver, while
-  \code{knitLyxRnw()} does the same, but using the knitr driver. Similarly,
-  \code{purlLyxRnw()} is the knitr counterpart of \code{tangleLyxRnw()} standard
-  tangling function.
+  For \code{cleanLyxRnw()}, a list for Sweave options found in the document;
+  \code{NULL} for the other functions: these functions are invoked for their
+  side effects. The function \code{weaveLyxRnw()} uses the standard Sweave
+  driver (but it uses knitr for LyX documents that use the SciViews Knitr
+  module), while \code{knitLyxRnw()} does the same, but using the knitr driver.
+  Similarly, \code{purlLyxRnw()} is the knitr counterpart of
+  \code{tangleLyxRnw()} standard tangling function.
 }
 
 \author{ Philippe Grosjean <phgrosjean at sciviews.org> }

Modified: pkg/svSweave/man/svSweave-package.Rd
===================================================================
--- pkg/svSweave/man/svSweave-package.Rd	2013-01-27 17:57:21 UTC (rev 517)
+++ pkg/svSweave/man/svSweave-package.Rd	2013-01-27 20:44:40 UTC (rev 518)
@@ -12,8 +12,8 @@
   \tabular{ll}{
     Package: \tab svSweave\cr
     Type: \tab Package\cr
-    Version: \tab 0.9-7\cr
-    Date: \tab 2013-01-11\cr
+    Version: \tab 0.9-8\cr
+    Date: \tab 2013-01-27\cr
     License: \tab GPL 2 or above, at your convenience\cr
   }
   % TODO: add description of main functions here. Also add examples



More information about the Sciviews-commits mailing list