Sweave中的自定义图形设备 [英] Custom graphical device in Sweave
问题描述
我的插入带有特殊字符的pdf图形的问题Sweave文档已经通过在Sweave自身之外创建pdf图并且然后导入来解决。
遵循Sweave文档,我写了一个自定义的图形设备,它应该以相同的方式构建PDF图形。但它不起作用。你能解释一下为什么下面的Sweave文件的第二个图形不起作用,而它应该像第一个那样完全创建?
\文档类{文章}
\ begin { document}
\SweaveOpts {concordance = TRUE}
<<<><<><设置,echo = FALSE>> =
mycairo< - function(name,width = 7 ,height = 7,...){
grDevices :: cairo_pdf(name,width = width,height = height)
}
mycairo.off< - function(){
cat(关闭mycairo \\\
)
隐形(grDevices :: dev.off())
}
@
\section {导出图}
plot(0,0,ylab = ylab,xlab = NA,cex.lab = 3)
不可见(dev.off())
@
%插入导出图
\includegraphics [width = 6cm] {exported_plot.pdf}
\section {直接图}
par(mar = c(6,6,0,6))
ylab <表达式(paste(,bar(italic(\\\ℓ)),(,phi [0],|,italic(list(x,y)),)))
plot(0,0,ylab = ylab,xlab = NA,cex.lab = 1)
@
\ end {document}
@ user20650善意地建议我将他/她的评论中给出的答案转换为官方的答案。 p>
在 cairo_pdf
中包含 pdf
文件扩展名就足够了,功能。然后用
mycairo< - function( name,width = 7,height = 7,...){
grDevices :: cairo_pdf(sprintf(%s.pdf,name),width = width,height = height)
}
请注意,不是指定 grdevice = mycairo $ c $您可以将其设置为全局选项:
\SweaveOpts {grdevice = mycairo}
My problem of inserting a pdf graphic with a special character in a Sweave document has been solved by creating the pdf plot outside Sweave itself and then importing it.
Following the Sweave documentation, I have written a custom graphical device which should construct the pdf graphic exactly in the same way. However it doesn't work. Can you explain me why the second graphic of the Sweave document below does not work whereas it should be created exactly as the first one ? Am I wrong to believe it should ?
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<setup, echo=FALSE>>=
mycairo <- function(name, width = 7, height = 7, ...) {
grDevices::cairo_pdf(name, width = width, height = height)
}
mycairo.off <- function() {
cat("shutting down mycairo\n")
invisible(grDevices::dev.off())
}
@
\section{Export plot}
<<Export_plot, echo=FALSE>>=
cairo_pdf("exported_plot.pdf")
par(mar=c(6,7,0,6))
ylab <- expression(paste("", bar(italic("\u2113")), "(",phi[0], "|", italic(list(x,y)), ")"))
plot(0,0, ylab=ylab, xlab=NA, cex.lab=3)
invisible(dev.off())
@
% insert exported plot
\includegraphics[width=6cm]{exported_plot.pdf}
\section{Direct plot}
<<mycairo_plot, echo=FALSE, fig=TRUE, pdf=TRUE, grdevice=mycairo, width=4, height=4>>=
par(mar=c(6,6,0,6))
ylab <- expression(paste("", bar(italic("\u2113")), "(",phi[0], "|", italic(list(x,y)), ")"))
plot(0,0, ylab=ylab, xlab=NA, cex.lab=1)
@
\end{document}
@user20650 kindly proposed me to convert the answer given in his/her comment to an official one.
It suffices to include the pdf
file extension in the cairo_pdf
function. Then replace the mycairo
function with:
mycairo <- function(name, width = 7, height = 7, ...) {
grDevices::cairo_pdf(sprintf("%s.pdf", name), width = width, height = height)
}
As a side note, instead of specifying grdevice=mycairo
in each figure chunk, you can also set it as a global option:
\SweaveOpts{grdevice=mycairo}
这篇关于Sweave中的自定义图形设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!