简体   繁体   English

如何使用 R markdown 在不同的页面上引用 R 代码(块)(文本中提供的可重复示例)

[英]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") )

page 1第 1 页

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.

相关问题 如何使用 Knitr 块选项删除 R Markdown HTML output 周围的边框框 - How to remove border box around R Markdown HTML output using Knitr chunk options 为什么我无法在 R Markdown 文档中获取代码 colors 进行打印? - Why can't I get colors of code in an R Markdown document to print? 是否可以使用垂直滚动条在R markdown中以块显示多个图像 - Is it possible to display multiple images in a chunk in R markdown with a vertical scrollbar 在 r markdown 中多次使用相同的文本部分 - Using the same text section several times in r markdown 如何将R Markdown块放在不同的HTML选项卡中? - How to put R Markdown chunks in different HTML-tabs? 在单独的div标签中单击时,如何使每个标签显示不同的文本块? - How can I make each a tag display a different chunk of text when clicked in a separate div tag? 编织/渲染 R Markdown 输出:我可以阻止代码在 PDF 输出中显示但允许在 HTML 输出中折叠代码吗? - Knitting/Rendering R Markdown Output: Can I prevent code from showing in PDF output but allow code folding in HTML output? R Markdown 不使用 R Studio 编织到 Word - R Markdown not knitting to Word using R Studio 我如何使用jquery / css做可点击按钮效果? 提供的例子 - how can i do the clickable button effect using jquery/css? example provided CSS 和 R markdown(预订)。 如何在不更改整体模板的情况下使用 CSS 构建 css 盒子? - CSS and R markdown (bookdown). How can I use a CSS to build css boxes without changing the overall template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM