[英]How can I refer to an R code (chunk) on a different page using R markdown (reproducible example provided in the text)
Let's say I have an R markdown pdf document that have 2 pages.假设我有一个包含 2 页的 R markdown pdf 文档。
# page 1
R code that was used to perform the regression analysis can be found by clicking HERE.
What I want is when I click on "HERE" it refers/takes me to the code in the page two.我想要的是当我点击“这里”时,它引用/带我到第二页中的代码。 Suppose the page 2 contains the following code.
假设第 2 页包含以下代码。
# page 2
model = lm(y~x, data = data)
Any ideas?有任何想法吗?
You have to link the anchor to a heading, as far as I know.据我所知,您必须将锚链接到标题。 One workaround to link to a plot would be to add an empty heading below it.
链接到 plot 的一种解决方法是在其下方添加一个空标题。
Create an anchor next to an empty heading below the plot, like this:在 plot 下方的空标题旁边创建一个锚点,如下所示:
# {#YourAnchorNextToTheHeading}
. # {#YourAnchorNextToTheHeading}
。
Wrap the word/sentence you want the link into in square brackets []
, followed by your anchor wrapped around round brackets.将您想要链接的单词/句子包裹在方括号
[]
中,然后将锚点包裹在圆括号中。
Here is an example:这是一个例子:
# page 1
R code that was used to perform the regression analysis can be found by clicking [HERE](#page2).
\pagebreak
# page 2
```{r echo = FALSE}
plot(cars)
```
# {#page2}
EDIT: Adding color to the linked text:编辑:为链接文本添加颜色:
I found this on the the R Cookbook tutorial: :我 在 R Cookbook 教程中找到了这个:
Create a R function to write raw HTML or LaTeX code:创建 R function 以写入原始 HTML 或 Z5766DEA037E9097AC6869424B6FE 代码:
```{r echo=FALSE, include=FALSE}
colorize <- function(x, color) {
if (knitr::is_latex_output()) {
sprintf("\\textcolor{%s}{%s}", color, x)
} else if (knitr::is_html_output()) {
sprintf("<span style='color: %s;'>%s</span>", color,
x)
} else x
}
```
Then add it to the text ( make sure to wrap r colorize("HERE", "blue")
with back ticks (`) )然后将其添加到文本中(确保用反引号 (`) 包裹
r colorize("HERE", "blue")
) )
R code that was used to perform the regression analysis can be found by clicking r colorize("HERE", "blue")
.通过单击
r colorize("HERE", "blue")
可以找到用于执行回归分析的 R 代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.