修改后,可以很好支持中文了(至少在中文的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 />