Sweave 文档中 R 注释中对图形的动态引用

2023-12-05

我想找到一种使用 LaTeX 的方法\ref{}用于在 Sweave .Rnw 文件中的 R 代码中进行注释的标记。这里有两个例子,一个是印刷版的

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}

\begin{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 \ref{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 \ref{fig1} 
       aes(x = x, y = value) + 
       geom_line() + 
       facet_wrap( ~ variable, scale = "free_y")
ggsave(g, filename = "example1.jpeg")                    
@

\begin{figure}[h]
\centering
\includegraphics[width = 0.48\textwidth]{./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
@

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

\end{document}

pdf 是使用 R 命令构建的

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

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

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


这是解决此问题的一种方法,即重新定义Sinput源代码被包装的环境Sweave。默认情况下,这是一个简单的verbatim未经处理的环境latex为代币。诀窍是重新定义它以使用alltt允许在内部解析一些令牌的环境alltt环境。请注意,这可能会导致我不知道的不良副作用,因此请谨慎使用!

这是一个有效的可重现示例。如果你编译它,你将生成一个文件,其中ref{fig1}被图号代替。

\documentclass{article}
\usepackage{Sweave}
\usepackage{alltt}
\renewenvironment{Sinput}{\begin{alltt}}{\end{alltt}}

\begin{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 \ref{fig1}
dev.off()
@

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

\end{document}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Sweave 文档中 R 注释中对图形的动态引用 的相关文章

随机推荐

  • Google BigQuery 表补丁/更新不起作用

    Google Http Request object batchHeaders gt array 3 Content Type gt string application http Content Transfer Encoding gt
  • 如何在打印时去掉数组括号

    打印数组时如何去掉左右括号 var array 1 2 3 4 println array It prints 1 2 3 4 var arrayWithoutBracketsAndCommas array some code printl
  • 如何本地化 Windows 应用商店应用中的通知和组合框? (C#/XAML,多语言应用程序工具包)

    我在 Windows 应用商店应用程序本地化方面遇到一些问题 我能够本地化 xaml 内容 例如 TextBlock Text 或 Button Content 我正在以与此处所示相同的方式进行操作 但我不知道如何本地化以下内容 1 我的组
  • 我是否使用了太多 jQuery?我什么时候越线?

    最近我发现自己经常使用 jQuery 和 JavaScript 经常做与使用 CSS 之前相同的事情 例如 我使用 JavaScript jQuery 替换表格行颜色或创建按钮和链接悬停效果 这是可以接受的吗 或者我应该继续使用 CSS 来
  • 通过读取kafka的详细信息动态创建flink窗口

    假设 Kafka 消息包含 flink 窗口大小配置 我想读取来自 Kafka 的消息并在 flink 中创建一个全局窗口 问题陈述 我们可以使用 BroadcastStream 来处理上述场景吗 Or 还有其他方法可以支持上述情况吗 Fl
  • 数组交换 - 二维数组

    我正在研究交换二维数组中的索引 我似乎走在正确的轨道上 但它没有按照我想要的方式交换数组 第一行的索引j需要与第 2 行的索引交换j for int j 0 j lt array length j int temp array row1 j
  • HTML 表格导出至 Excel(XLS 或 CSV)

    我正在尝试将 HTML 表格内容导出到 Excel 我看见这个解决方案这有效但没有达到我的预期 因为我无法选择要复制的列 和它不适用于大桌子 还有另一种解决方案是通过js复制并手动粘贴到excel文件 但效果不太好 而且我不太喜欢这种方法
  • 是否存在批量检查区分大小写

    我需要检查给定文件是否存在 区分大小写 out txt 均为小写字母 文件是否存在于我运行脚本的位置 Code Case1 filename out txt if exist filename echo file exist else ec
  • 撤销 OAuth 访问令牌会导致 404 Not Found

    我正在开发一个与 GitHub 集成的应用程序 但在 注销 之前经过身份验证的用户时遇到问题 当我尝试撤销用户的授权令牌时 我从 API 收到 404 Not Found 响应 根据文档 看来我应该能够做一个DELETE请求https ap
  • 按第一行对 numpy 二维数组进行排序,保留列

    在 python 中 我有一个以下形式的 numpy 数组 4 8 2 0 5 3 1 6 8 1 2 2 6 0 3 9 7 6 7 8 5 8 1 1 4 我想按第一行的值从左到右按升序对其进行排序 同时保持整个列完好无损 实际的数组的
  • 无法在 bash 脚本中运行 adb 命令

    我正在尝试从 adb shell 启动 Android 网络共享设置 这样做的主要目的是通过运行 shell 脚本来启用 USB 网络共享模式 我在 Ubuntu 终端 12 04 上使用以下命令集 adb shell am start n
  • Sublime Text 是否有查找打开文件的快捷方式(Eclipse Ctrl + E)?

    Ctrl P of Sublime Text lets me find a file from all project files However there are too many duplicated names I m lookin
  • OpenGL 窗口未打开

    我有来自 OpenGLBook openglbook com 的代码 它可以编译 但无法加载 我完全不知道为什么它没有加载 代码如下 main cpp include main h Methods int main int argc cha
  • C++等待用户输入[重复]

    这个问题在这里已经有答案了 在控制台应用程序中等待用户输入的最佳方法是什么 std cout lt lt press any key to exit wait for user to hit enter or another key 有多种
  • 使用for循环的索引访问变量的名称

    假设我有 4 个字符串 private string string 1 string 2 string 3 string 4 然后假设我有一个 for 循环 如何通过for循环的索引访问变量名 这是我正在谈论的内容的一个想法 for int
  • Oozie 抑制 shell 作业操作的日志记录?

    我有一个运行 shell 脚本的简单工作流程 见下文 shell 脚本运行 pyspark 脚本 该脚本将文件从本地移动到 hdfs 文件夹 当我运行 shell 脚本本身时 它工作得很好 日志在 shell 脚本中通过 gt spark
  • 像关系 SQL 数据库一样使用 JSon (Javascript)

    所以我有一个如下所示的 JSON 变量 var peopleList 1 Name Lisa item1 Name of Item 1 2 Name Marty 3 Name Jordan item1 Name of Item 1 item
  • 如何显示文本视图几秒钟然后使其不可见?

    这个网站是最好的 它对我帮助很大 我是创建 android applecation 的初学者 这是我第一次在这里问问题 我的问题是如何显示一个文本视图5秒并使其消失 当我搜索时我发现了一些代码 但我不知道如何使用它或者也许我使用它以错误的方
  • 输入[类型=文件]验证

    如何检查输入文件是否不为空 我试过 image file click function if image file val alert Chose a file return false 但没有成功 The click事件被触发before
  • Sweave 文档中 R 注释中对图形的动态引用

    我想找到一种使用 LaTeX 的方法 ref 用于在 Sweave Rnw 文件中的 R 代码中进行注释的标记 这里有两个例子 一个是印刷版的 http cm bell labs com cm ms departments sia proj