可以让RMarkdown从文档开头以外的任何地方进行预览吗?
我目前正在用 R Markdown 写一些东西。每当我编织文档时,RStudio 的预览都会带我回到文档的最开头。有没有办法让这个新预览显示的位置更接近我默认工作的位置?例如,我可以在我的输入光标附近的位置进行预览吗?
评论提出了一些解决方法。到目前为止,我最好只在 RStudio 的预览窗口提供的搜索栏中输入我正在工作的部分的部分编号。我会单击目录中的相关条目,但我使用output: github_document: toc: true number_sections: true,它正在等待对其编号目录的补丁。
回答
并不像您想象的那么简单,但是可以使用 javascript 来控制显示 html 文档的哪个部分:
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{js}
location.hash = "#goto";
```
```{r, results='asis'}
cat(sprintf("# %snnSection text here.nn", 1:10), sep = "")
```
# GOTO
Scroll Here
```{r, results='asis'}
cat(sprintf("# %snnSection text here.nn", 11:20), sep = "")
```
- 即使这不是原始海报所要求的,在 R Markdown 中使用 javascript 块的一个很好的例子!