Sweave 文档中 R 注释中对图形的动态引用 [英] Dynamic references to figures in a R comment within Sweave document

查看:16
本文介绍了Sweave 文档中 R 注释中对图形的动态引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一种方法来使用 LaTeX ef{} 标记在 Sweave .Rnw 文件中的 R 代码中进行注释.这里有两个例子,一个在打印中

I would like to find a way to use the LaTeX ef{} markup to comment in the R code within a Sweave .Rnw file. Here are two examples, one in print

http://cm.bell-labs.com/cm/ms/departments/sia/project/nlme/UGuide.pdf

和一个用来工作的:

.Rnw 文件

% File: example.Rnw

documentclass{article}
usepackage{fullpage}
usepackage{graphics}
usepackage{Sweave} 
usepackage[margin = 10pt, font=small, labelfont={bf}]{caption}

egin{document}

Here is an example file to show what I want to do.  I would like to figure out how to use the LaTeX reference command to reference a figure being generated by R code.  Note in the R code, in a comment there is a reference to the figure, but of course the output file shows a verbatim copy of the LaTeX markup.  Does anyone know how to get something for Figure 
ef{fig2}?

<< example plot >>=
library(reshape)
library(ggplot2)

n <- 100
lambda <- 1 / 3 
x <- seq(0, qexp(0.999, rate = lambda), length = n)
q1.a <- data.frame(x =   x,
                   f =   dexp(x, rate = lambda),
                   F =   pexp(x, rate = lambda))

q1.a <- melt(q1.a, id.vars = 'x')
g <- ggplot(q1.a) +                                     # Produces 
ef{fig1} 
       aes(x = x, y = value) + 
       geom_line() + 
       facet_wrap( ~ variable, scale = "free_y")
ggsave(g, filename = "example1.jpeg")                    
@

egin{figure}[h]
centering
includegraphics[width = 0.48	extwidth]{./example1}
caption{Exponential Distribution based plots.}
label{fig1}
end{figure}

Here is more of what I would like to see:

<< example plot 2 >>=
ggsave(g + geom_point(), filename = "example2.jpeg")    # Produces Figure 2
@

egin{figure}
centering
includegraphics[width = 0.48	extwidth]{./example2}
caption{Exponential Distribution based plots with points and lines.}
label{fig2}
end{figure}

end{document}

pdf 是使用 R 命令构建的

and the pdf is build with the R commands

Sweave(file = 'example.Rnw',
       engine = "R",
       keep.source = 'TRUE',
       echo = 'TRUE',
       results = 'verbatim')

tools::texi2dvi(file  = "example.tex",
                pdf   = TRUE,
                clean = TRUE)

任何关于如何做到这一点的见解都会很棒.

Any insight on how do this would be great.

推荐答案

这里有一种方法可以通过重新定义 Sinput 环境来解决这个问题,在该环境中源代码被 Sweave.默认情况下,它是一个简单的 verbatim 环境,latex 不会为令牌处理.诀窍是重新定义它以使用 alltt 环境,该环境允许在 alltt 环境中解析一些标记.请注意,这可能会导致我不知道的不良副作用,因此请谨慎使用!

Here is one way to solve this issue by redefining the Sinput environment in which source code is wrapped by Sweave. By default, it is a simple verbatim environment which is not processed by latex for tokens. The trick is to redefine it to use the alltt environment which allows some tokens to be parsed inside the alltt environment. Note that this might lead to unwanted side effects that I am not aware of, so use with caution!

这是一个有效的可重现示例.如果你编译它,你会生成一个文件,其中 ref{fig1} 被数字替换.

Here is a reproducible example that works. If you compile it, you will generate a file where ref{fig1} is replaced by the figure number.

documentclass{article}
usepackage{Sweave}
usepackage{alltt}

enewenvironment{Sinput}{egin{alltt}}{end{alltt}}

egin{document}

In this document, we will create a plot using `R`, and reference its position in 
the source code.

<<produce-plot, results = hide>>=
pdf('example1.pdf')
plot(1:10, 1:10)     # Produces Figure 
ef{fig1}
dev.off()
@

egin{figure}
includegraphics{example1.pdf}
caption{Figure 1}
label{fig1}
end{figure}

end{document}

这篇关于Sweave 文档中 R 注释中对图形的动态引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆