• R语言
  • 最近对odfWeave作了一个patch

修改后,可以很好支持中文了(至少在中文的windows下是这样)。共享一下。


<br />
"odfWeave" <- <br />
function(file, dest, workDir=odfTmpDir(), control=odfWeaveControl())<br />
{ <br />
   #configure<br />
   currentLoc <- getwd()<br />
   zipCmd <- control$zipCmd<br />
   zipCmd <- gsub("$$file$$", shellQuote(basename(file)), zipCmd, fixed=TRUE)<br />
#########################################################################################<br />
currentEncoding <- localeToCharset() # get Charset for iconv()<br />
op <- options(warn=-1)               # supress warning msg<br />
on.exit(options(op))                 # restore the setting<br />
#########################################################################################<br />
   currentLocale <- c(Sys.getlocale("LC_CTYPE"), Sys.getlocale("LC_COLLATE"))<br />
   Sys.setlocale("LC_CTYPE", "C")<br />
   Sys.setlocale("LC_COLLATE", "C")<br />
<br />
   if(dirname(dest) == ".") dest <- paste(currentLoc, "/", dest, sep = "")<br />
   <br />
   verbose <- control$verbose<br />
<br />
   #check for an unzipping utility<br />
   if(all(zipCmd == c("zip -r $$file$$ .", "unzip -o $$file$$")))<br />
   {<br />
      errorText <- paste(<br />
         "unzip could not be found.",<br />
         "If installed, check your path.",<br />
         "If not installed, either go to",<br />
         "[url]www.info-zip.org[/url] and install or",<br />
         "use another utility (like jar)")<br />
      if(.Platform$OS.type == "windows")<br />
      {<br />
         zipTest <- class(try(system("unzip", intern=TRUE, invisible=TRUE),<br />
            silent=TRUE))<br />
         if(class(zipTest) == "try-error") stop(errorText)<br />
      } else {<br />
         zipTest <- system("unzip", intern = TRUE)[1]<br />
         if(is.na(zipTest) || length(grep("UnZip", zipTest)) == 0)<br />
            stop(errorText)<br />
      }<br />
   }<br />
<br />
   # create temp dir<br />
   if(!file.exists(workDir))<br />
   {<br />
      announce(verbose, "  Creating ", workDir, "\n")<br />
      dir.create(workDir, showWarnings = TRUE, recursive = FALSE)<br />
      if(!file.exists(workDir)) stop("Error creating working directory")<br />
   }<br />
<br />
   announce(verbose, "  Setting wd to ", workDir, "\n")<br />
   <br />
   workingCopy <- paste(basename(file), sep = "")  <br />
   <br />
   # copy file to the tmp dir<br />
   if(!file.exists(file)) stop(paste(file, "does not exist"))<br />
   announce(verbose, "  Copying ", file, "\n")<br />
   if(!file.copy(file, paste(workDir, "/", workingCopy, sep = ""), overwrite = TRUE)) stop("Error copying odt file")<br />
   <br />
   setwd(workDir)<br />
<br />
   # unpack the file<br />
   announce(verbose, "  Decompressing ODF file using", zipCmd[2], "\n")<br />
   if(.Platform$OS.type == "windows")<br />
   {<br />
      if(system(zipCmd[2], invisible = TRUE) != 0) stop("Error unzipping file")<br />
   } else {<br />
      if(system(zipCmd[2]) != 0) stop("Error unzipping odt file")<br />
   }<br />
<br />
   # remove original file<br />
   announce(verbose, "\n  Removing ", workingCopy, "\n")<br />
   file.remove(workingCopy)<br />
   if (file.exists(workingCopy)) stop("Error removing original file")<br />
<br />
   #configure Pictures directory<br />
   if(!file.exists(paste(workDir, "/Pictures", sep = "")))<br />
   {<br />
      announce(verbose, "  Creating a Pictures directory\n")<br />
      picDir <- dir.create("Pictures", showWarnings = TRUE, recursive = FALSE)<br />
      if(!picDir)  stop("Error creating Pictures directory")<br />
   }<br />
   assign(<br />
      "picPath",  <br />
      paste(workDir, "/Pictures", sep = ""), <br />
      env = .odfEnv)<br />
<br />
   # find xml files<br />
   fileList <- list.files(workDir)<br />
   xmlFiles <- fileList[grep(".xml", tolower(fileList), fixed = TRUE)]<br />
   if(length(xmlFiles) == 0) stop("Something is wrong - no xml")<br />
<br />
   # load xml into list<br />
   xmlContents <- vector(mode = "character", length = length(xmlFiles))<br />
   for(i in seq(along = xmlContents)) {<br />
xmlContents[i] <- readXML(xmlFiles[i], verbose = control$verbose)<br />
   }<br />
   names(xmlContents) <- xmlFiles<br />
<br />
   # add style information, if any, to styles.xml and content.xml<br />
   styles <- getStyleDefs()<br />
   styleInfo <- xmlContents["styles.xml"]<br />
   xmlContents["styles.xml"] <- addStyleDefs(styleInfo, styles, "styles", control$verbose)<br />
<br />
   styleInfo <- xmlContents["content.xml"]<br />
   xmlContents["content.xml"] <- addStyleDefs(styleInfo, styles, "content", control$verbose)<br />
<br />
   #get indexes and lengths of sweave expression matches<br />
   stags <- tagsIdxs(xmlContents, verbose=control$verbose)<br />
   #for convenience<br />
   tagsFound <- tagsExist(stags)<br />
<br />
   if(any(tagsFound)) {<br />
      announce(verbose, "\n  Sweave tags found in: ", xmlFiles[tagsFound], "\n")<br />
      sweaveFiles <- xmlFiles[tagsFound]<br />
      sweaveContents <- xmlContents[tagsFound]<br />
<br />
      for(j in seq(along = sweaveFiles)) {<br />
         announce(verbose, "  Removing xml around <<>>= for ", sweaveFiles[j], "\n")<br />
         if(!is.null(sweaveContents[j])) sweaveContents[j] <-<br />
         processXml(sweaveContents[j], stags[,j])<br />
         # write processed lines to Rnw file<br />
         rnwFileName <- (gsub("[Xx][Mm][Ll]", "Rnw", sweaveFiles[j]))<br />
         announce(verbose, "  Writing ", sweaveFiles[j], " to ", rnwFileName, "\n")<br />
         rnwFile <- file(rnwFileName, "wb")<br />
################################################################################################################  <br />
if (sweaveFiles[j]=="content.xml") {                                                                         <br />
                                    writeLines(iconv(sweaveContents[[j]],"UTF-8",currentEncoding), rnwFile)   <br />
                                    close(rnwFile)                                                            <br />
} else                                                                                                      <br />
################################################################################################################  <br />
         writeXML(sweaveContents[[j]], rnwFile)<br />
<br />
         #nuke the xml file<br />
         announce(verbose, "\n  Removing ", sweaveFiles[j], "\n")<br />
         file.remove(sweaveFiles[j], recursive = TRUE)<br />
         if (file.exists(sweaveFiles[j])) stop("Error removing xml file file")<br />
<br />
         #Sweave results to new xml file<br />
         announce(verbose, "  Sweaving ",rnwFileName, "\n\n")<br />
<br />
         Sys.setlocale("LC_CTYPE", currentLocale[1])<br />
         Sys.setlocale("LC_COLLATE", currentLocale[2])<br />
         Sweave(<br />
            file =   paste(rnwFileName, sep = ""),<br />
            output = paste(sweaveFiles[j], sep = ""),<br />
            quiet = !control$verbose,<br />
            driver = RweaveOdf(), control = control)<br />
####################################################################################################################################        <br />
         outfile.path <- paste(sweaveFiles[j], sep = "")  <br />
         if (outfile.path=="content.xml"){                                                                                       <br />
              outfile.content <- iconv(paste(readLines(outfile.path),collapse="\n"),currentEncoding,"UTF-8")              <br />
              writeLines(outfile.content,file(outfile.path))<br />
         }                                                                                                                       <br />
####################################################################################################################################       <br />
<br />
         Sys.setlocale("LC_CTYPE", "C")<br />
         Sys.setlocale("LC_COLLATE", "C")<br />
<br />
         # remove sweave file<br />
         announce(verbose, "  Removing ", rnwFileName, "\n")<br />
         file.remove(rnwFileName, recursive = TRUE)<br />
         if (file.exists(rnwFileName)) stop("Error removing xml file file")<br />
      }<br />
   }<br />
<br />
   # if there was no Sweave tags in styles.xml, write that out too<br />
   if(!tagsFound[which(xmlFiles == "styles.xml")]) {<br />
      styleFile <- file("styles.xml", "wb")<br />
      sink(styleFile)<br />
      cat(xmlContents[[which(xmlFiles == "styles.xml")]])<br />
      sink()<br />
      close(styleFile)<br />
   }<br />
<br />
   announce(verbose, "\n\  Packaging file using", zipCmd[1], "\n")<br />
   if(.Platform$OS.type == "windows") {<br />
      if(system(zipCmd[1], invisible = TRUE) != 0)  stop("Error zipping file")<br />
   } else {<br />
      if(system(zipCmd[1]) != 0) stop("Error zipping file")<br />
   }<br />
<br />
   # copy final file to destination<br />
   if(!file.exists(workingCopy))  stop(paste(workingCopy, "does not exist"))<br />
   announce(verbose, "  Copying ", workingCopy, "\n")<br />
   if(!file.copy(workingCopy, dest, overwrite = TRUE))  stop("Error copying odt file")<br />
<br />
   announce(verbose, "  Resetting wd\n")<br />
   setwd(currentLoc)<br />
<br />
   Sys.setlocale("LC_CTYPE", currentLocale[1])<br />
   Sys.setlocale("LC_COLLATE", currentLocale[2])<br />
<br />
   assign(<br />
      "picPath",  <br />
      NA, <br />
      env = .odfEnv)<br />
<br />
   # delete working dir<br />
   if(control$cleanup)<br />
   {<br />
      announce(verbose, "  Removing ", workDir, "\n")<br />
      unlink(workDir, recursive = TRUE)<br />
      if (file.exists(workDir)) unlink(shQuote(workDir), recursive=TRUE)<br />
      if (file.exists(workDir)) unlink(shellQuote(workDir), recursive=TRUE)<br />
      if (file.exists(workDir)) stop("Error removing work dir")<br />
   }<br />
   invisible(NULL)<br />
}<br />
<br />
很长的###############之间的内容是我增加的。
不太懂sweave processing的意思,是输出ODF的吗?
是的。可以将文字和code都写在一个odf文档中,用odfWeave对odf进行sweave后,得到的文档就是code的结果以及原来你写的文字。



当然,还可以对latex文件进行Sweave的。



属于literal propramming的一种方法。
今天在FreeBSD上面测试了一下,修改了一下。


<br />
<br />
"odfWeave" <- <br />
function(file, dest, encoding=NULL, workDir=odfTmpDir(), control=odfWeaveControl())<br />
#增加encoding=NULL,如果R的workshop中有对象包括一些非ASCII,非UTF-8的字符对象,则通过encoding设定这些编码,从而进行编码转换。<br />
#一般encoding的取值为NULL,或者等于localeToCharset()。<br />
#第三次进行修改,在FreeBSD和windows上面测试通过<br />
{ <br />
   #configure<br />
   currentLoc <- getwd()<br />
   zipCmd <- control$zipCmd<br />
   zipCmd <- gsub("$$file$$", shellQuote(basename(file)), zipCmd, fixed=TRUE)<br />
#########################################################################################<br />
#encoding #force eval<br />
op <- options(warn=-1)               # supress warning msg<br />
on.exit(options(op))                 # restore the setting<br />
#########################################################################################<br />
   currentLocale <- c(Sys.getlocale("LC_CTYPE"), Sys.getlocale("LC_COLLATE"))<br />
   Sys.setlocale("LC_CTYPE", "C")<br />
   Sys.setlocale("LC_COLLATE", "C")<br />
<br />
   if(dirname(dest) == ".") dest <- paste(currentLoc, "/", dest, sep = "")<br />
   <br />
   verbose <- control$verbose<br />
<br />
   #check for an unzipping utility<br />
   if(all(zipCmd == c("zip -r $$file$$ .", "unzip -o $$file$$")))<br />
   {<br />
      errorText <- paste(<br />
         "unzip could not be found.",<br />
         "If installed, check your path.",<br />
         "If not installed, either go to",<br />
         "[url]www.info-zip.org[/url] and install or",<br />
         "use another utility (like jar)")<br />
      if(.Platform$OS.type == "windows")<br />
      {<br />
         zipTest <- class(try(system("unzip", intern=TRUE, invisible=TRUE),<br />
            silent=TRUE))<br />
         if(class(zipTest) == "try-error") stop(errorText)<br />
      } else {<br />
         zipTest <- system("unzip", intern = TRUE)[1]<br />
         if(is.na(zipTest) || length(grep("UnZip", zipTest)) == 0)<br />
            stop(errorText)<br />
      }<br />
   }<br />
<br />
   # create temp dir<br />
   if(!file.exists(workDir))<br />
   {<br />
      announce(verbose, "  Creating ", workDir, "\n")<br />
      dir.create(workDir, showWarnings = TRUE, recursive = FALSE)<br />
      if(!file.exists(workDir)) stop("Error creating working directory")<br />
   }<br />
<br />
   announce(verbose, "  Setting wd to ", workDir, "\n")<br />
   <br />
   workingCopy <- paste(basename(file), sep = "")  <br />
   <br />
   # copy file to the tmp dir<br />
   if(!file.exists(file)) stop(paste(file, "does not exist"))<br />
   announce(verbose, "  Copying ", file, "\n")<br />
   if(!file.copy(file, paste(workDir, "/", workingCopy, sep = ""), overwrite = TRUE)) stop("Error copying odt file")<br />
   <br />
   setwd(workDir)<br />
<br />
   # unpack the file<br />
   announce(verbose, "  Decompressing ODF file using", zipCmd[2], "\n")<br />
   if(.Platform$OS.type == "windows")<br />
   {<br />
      if(system(zipCmd[2], invisible = TRUE) != 0) stop("Error unzipping file")<br />
   } else {<br />
      if(system(zipCmd[2]) != 0) stop("Error unzipping odt file")<br />
   }<br />
<br />
   # remove original file<br />
   announce(verbose, "\n  Removing ", workingCopy, "\n")<br />
   file.remove(workingCopy)<br />
   if (file.exists(workingCopy)) stop("Error removing original file")<br />
<br />
   #configure Pictures directory<br />
   if(!file.exists(paste(workDir, "/Pictures", sep = "")))<br />
   {<br />
      announce(verbose, "  Creating a Pictures directory\n")<br />
      picDir <- dir.create("Pictures", showWarnings = TRUE, recursive = FALSE)<br />
      if(!picDir)  stop("Error creating Pictures directory")<br />
   }<br />
   assign(<br />
      "picPath",  <br />
      paste(workDir, "/Pictures", sep = ""), <br />
      env = .odfEnv)<br />
<br />
   # find xml files<br />
   fileList <- list.files(workDir)<br />
   xmlFiles <- fileList[grep(".xml", tolower(fileList), fixed = TRUE)]<br />
   if(length(xmlFiles) == 0) stop("Something is wrong - no xml")<br />
<br />
   # load xml into list<br />
   xmlContents <- vector(mode = "character", length = length(xmlFiles))<br />
   for(i in seq(along = xmlContents)) {<br />
xmlContents[i] <- readXML(xmlFiles[i], verbose = control$verbose)<br />
   }<br />
   names(xmlContents) <- xmlFiles<br />
<br />
   # add style information, if any, to styles.xml and content.xml<br />
   styles <- getStyleDefs()<br />
   styleInfo <- xmlContents["styles.xml"]<br />
   xmlContents["styles.xml"] <- addStyleDefs(styleInfo, styles, "styles", control$verbose)<br />
<br />
   styleInfo <- xmlContents["content.xml"]<br />
   xmlContents["content.xml"] <- addStyleDefs(styleInfo, styles, "content", control$verbose)<br />
<br />
   #get indexes and lengths of sweave expression matches<br />
   stags <- tagsIdxs(xmlContents, verbose=control$verbose)<br />
   #for convenience<br />
   tagsFound <- tagsExist(stags)<br />
<br />
   if(any(tagsFound)) {<br />
      announce(verbose, "\n  Sweave tags found in: ", xmlFiles[tagsFound], "\n")<br />
      sweaveFiles <- xmlFiles[tagsFound]<br />
      sweaveContents <- xmlContents[tagsFound]<br />
<br />
      for(j in seq(along = sweaveFiles)) {<br />
         announce(verbose, "  Removing xml around <<>>= for ", sweaveFiles[j], "\n")<br />
         if(!is.null(sweaveContents[j])) sweaveContents[j] <-<br />
         processXml(sweaveContents[j], stags[,j])<br />
         # write processed lines to Rnw file<br />
         rnwFileName <- (gsub("[Xx][Mm][Ll]", "Rnw", sweaveFiles[j]))<br />
         announce(verbose, "  Writing ", sweaveFiles[j], " to ", rnwFileName, "\n")<br />
         rnwFile <- file(rnwFileName, "wb")<br />
################################################################################################################  <br />
if (sweaveFiles[j]=="content.xml" & capabilities("iconv") & !is.null(encoding)) {                                                  <br />
                                    writeLines(iconv(sweaveContents[[j]],"UTF-8",encoding), rnwFile)  <br />
                                    close(rnwFile)                                                           <br />
} else                                                                                                       <br />
################################################################################################################  <br />
         writeXML(sweaveContents[[j]], rnwFile)<br />
<br />
         #nuke the xml file<br />
         announce(verbose, "\n  Removing ", sweaveFiles[j], "\n")<br />
         file.remove(sweaveFiles[j], recursive = TRUE)<br />
         if (file.exists(sweaveFiles[j])) stop("Error removing xml file file")<br />
<br />
         #Sweave results to new xml file<br />
         announce(verbose, "  Sweaving ",rnwFileName, "\n\n")<br />
<br />
         Sys.setlocale("LC_CTYPE", currentLocale[1])<br />
         Sys.setlocale("LC_COLLATE", currentLocale[2])<br />
         Sweave(<br />
            file =   paste(rnwFileName, sep = ""),<br />
            output = paste(sweaveFiles[j], sep = ""),<br />
            quiet = !control$verbose,<br />
            driver = RweaveOdf(), control = control)<br />
<br />
####################################################################################################################################        <br />
         outfile.path <- paste(sweaveFiles[j], sep = "")  <br />
         if (outfile.path=="content.xml" & capabilities("iconv") & !is.null(encoding)){<br />
             outfile.content <- iconv(paste(readLines(outfile.path),collapse="\n"),encoding,"UTF-8")              <br />
             writeLines(outfile.content,file(outfile.path))<br />
                         }                                                                                                                      <br />
####################################################################################################################################       <br />
<br />
         Sys.setlocale("LC_CTYPE", "C")<br />
         Sys.setlocale("LC_COLLATE", "C")<br />
<br />
         # remove sweave file<br />
         announce(verbose, "  Removing ", rnwFileName, "\n")<br />
         file.remove(rnwFileName, recursive = TRUE)<br />
         if (file.exists(rnwFileName)) stop("Error removing xml file file")<br />
      }<br />
   }<br />
<br />
   # if there was no Sweave tags in styles.xml, write that out too<br />
   if(!tagsFound[which(xmlFiles == "styles.xml")]) {<br />
      styleFile <- file("styles.xml", "wb")<br />
      sink(styleFile)<br />
      cat(xmlContents[[which(xmlFiles == "styles.xml")]])<br />
      sink()<br />
      close(styleFile)<br />
   }<br />
<br />
   announce(verbose, "\n\  Packaging file using", zipCmd[1], "\n")<br />
   if(.Platform$OS.type == "windows") {<br />
      if(system(zipCmd[1], invisible = TRUE) != 0)  stop("Error zipping file")<br />
   } else {<br />
      if(system(zipCmd[1]) != 0) stop("Error zipping file")<br />
   }<br />
<br />
   # copy final file to destination<br />
   if(!file.exists(workingCopy))  stop(paste(workingCopy, "does not exist"))<br />
   announce(verbose, "  Copying ", workingCopy, "\n")<br />
   if(!file.copy(workingCopy, dest, overwrite = TRUE))  stop("Error copying odt file")<br />
<br />
   announce(verbose, "  Resetting wd\n")<br />
   setwd(currentLoc)<br />
<br />
   Sys.setlocale("LC_CTYPE", currentLocale[1])<br />
   Sys.setlocale("LC_COLLATE", currentLocale[2])<br />
<br />
   assign(<br />
      "picPath",  <br />
      NA, <br />
      env = .odfEnv)<br />
<br />
   # delete working dir<br />
   if(control$cleanup)<br />
   {<br />
      announce(verbose, "  Removing ", workDir, "\n")<br />
      unlink(workDir, recursive = TRUE)<br />
      if (file.exists(workDir)) unlink(shQuote(workDir), recursive=TRUE)<br />
      if (file.exists(workDir)) unlink(shellQuote(workDir), recursive=TRUE)<br />
      if (file.exists(workDir)) stop("Error removing work dir")<br />
   }<br />
   invisible(NULL)<br />
}<br />
<br />
LZ From

Fudan University ?
[quote]引用第5楼bjt2007-03-21 16:56发表的“”:

LZ From

Fudan University ?[/quote]



不打紧的问题,英雄不问出身嘛:)