[R] using dvi with latex object: directory not correctly set, maybe due to error in shQuote()
Pfaff, Bernhard Dr.
Bernhard_Pfaff at fra.invesco.com
Wed Dec 17 16:25:22 CET 2008
Hello Marco,
as might not be evident at first sight, but have you set the environment variable "R_SHELL"? If you spot at the dvi method for latex you will find a call to sys(), which will call shell() and if the argument shell is unset then the contents of "R_SHELL" will be used. Hence, what does:
Sys.getenv("R_SHELL")
yield at your machine? I reckon "" will be returned. Therefore as a first step:
Sys.setenv(R_SHELL = "cmd.exe") or permanently in your R environment file. Having done so and running dvi(latex.obj) now produces at least not the warning that everything beyond "cd" is skipped and the command via paste is parsed. The next problem is the path the randomly generated file that latex cannot handle. The following alternative might work for you too:
dvi.latex2 <- function (object, prlog = FALSE, nomargins = TRUE, width = 5.5, height = 7, ...)
{
fi <- object$file
sty <- object$style
if (length(sty))
sty <- paste("\\usepackage{", sty, "}", sep = "")
if (nomargins)
sty <- c(sty, paste("\\usepackage[paperwidth=", width,
"in,paperheight=", height, "in,noheadfoot,margin=0in]{geometry}",
sep = ""))
tmp <- tempfile(tmpdir = tempdir())
tmptex <- paste(tmp, "tex", sep = ".")
infi <- readLines(fi, n = -1)
cat("\\documentclass{report}", sty, "\\begin{document}\\pagestyle{empty}",
infi, "\\end{document}\n", file = tmptex, sep = "\n")
sc <- if (under.unix) {
"&&"
} else {
"&"
}
shell(paste("cd", shQuote(tempdir()), sc, optionsCmds("latex"),
"-interaction=scrollmode", shQuote(tmp)), translate = TRUE)
if (prlog)
cat(scan(paste(tmp, "log", sep = "."), list(""), sep = "\n")[[1]],
sep = "\n")
fi <- paste(tmp, "dvi", sep = ".")
structure(list(file = fi), class = "dvi")
}
And therefore:
tbl.loc <- matrix(1:4, ncol=2)
latex.obj <- latex(tbl.loc)
tempdir <- function(){"H:/PROJECTS/data"}
Sys.getenv("R_SHELL")
Sys.setenv(R_SHELL = "cmd.exe")
Sys.getenv("R_SHELL")
## options(xdvicmd='dviout') set appropriately I use TeXLive and have not yap installed;
## working with MikTeX there should be no need to change the default viewer
dvi.latex2(latex.obj)
## It might be the case that the dvi file is not displayed immediately after production but can be opened
## manually
Does this work for your? Probably it is also a good idea to address this problem directly to the package maintainer (already cc'ed).
Best,
Bernhard
>
>Dear friends of R,
>
>I want to produce a pdf file with the contents of a matrix. I
>employ the latex command in combination with dvi, both
>contained in the Hmisc package. It seems to me that the
>function does not correctly set the directory.
>
>> tbl.loc <- matrix(1:4, nc=2)
>> latex.obj <- latex(tbl.loc)
>> dvi(latex.obj)
>warning: extra args ignored after 'cd'
>H:\PROJECTS\data
>warning: extra args ignored after 'yap'
>
>When I have a look at the function dvi.latex I find the
>following line which, I guess, is meant to set the new
>directory and to run latex.
>
> sys(paste("cd", shQuote(tempdir()), sc, optionsCmds("latex"),
> "-interaction=scrollmode", shQuote(tmp)), output = FALSE)
>
>Running just the piece shQuote(tempdir()) returns
>> shQuote(tempdir())
>[1] "\"C:\\DOKUME~1\\ferimawi\\LOKALE~1\\Temp\\Rtmpr4CG3A\""
>> tempdir()
>[1] "C:\\DOKUME~1\\ferimawi\\LOKALE~1\\Temp\\Rtmpr4CG3A"
>
>Is the leading "\" causing the problem? How can I fix the problem?
>
>The R-help dealt with a related problem some while ago but I
>do not think that it resolves my problem:
>http://finzi.psych.upenn.edu/R/Rhelp02a/archive/62975.html
>
>I am using Windows XP, R version 2.7.2 (2008-08-25) and Hmisc
>version 3.4-4.
>
>Thanks in advance for your help.
>Regards
>Marco
>
>Marco Willner
>Senior Analyst Quantitative Asset Allocation
>Feri Finance AG
>Haus am Park
>Rathausplatz 8-10
>D-61348 Bad Homburg v.d.H
>Tel: +49 (6172) 916 3037
>Fax: +49 (6172) 916 1037
>E-Mail: marco.willner at feri.de
>Internet: www.feri.de
>Handelsregister des Amtsgerichts Bad Homburg v.d.H. (HRB 7473)
>Vorstände: Michael Stammler (Sprecher), Dr. Matthias Klöpper,
>Dr. Helmut Knepel, Dr. Heinz-Werner Rapp, Arndt Thorn
>Vorsitzender des Aufsichtsrates: Dr. Uwe Schroeder-Wildberg
>Disclaimer:
>Diese Nachricht enthält vertrauliche und/oder ausschließlich
>für den Adressaten bestimmte Informationen. Wenn Sie nicht der
>vorgesehene Adressat dieser E-Mail oder dessen Vertreter sein
>sollten, so beachten Sie bitte, dass jede Form der
>Kenntnisnahme, Veröffentlichung, Vervielfältigung oder
>Weitergabe des Inhalts dieser E-Mail und der E-Mail selber
>unzulässig ist. Sollten Sie diese E-Mail irrtümlich erhalten
>haben, so bitten wir Sie, den Absender unverzüglich durch
>Antwort-E-Mail oder Anruf unter +49 (6172) 916-0 zu
>informieren und diese Nachricht zu löschen. Soweit nicht
>anderweitig angegeben, ist diese Nachricht weder ein Angebot
>noch die Einholung eines Angebots zum Kauf oder Verkauf von
>Investitionen jedweder Art. Wir senden und empfangen E-Mails
>nur auf der Grundlage, dass wir nicht für Datenkorruption,
>Abfangen von Daten, nicht autorisierte Änderungen,
>Verfälschung und Viren und deren Konsequenzen haften.
>This message contains confidential and/or privileged ...{{dropped:15}}
More information about the R-help
mailing list