首页 > 解决方案 > 可以从文档开头以外的任何地方预览 R Markdown 吗?

问题描述

我目前正在用 R Markdown 写一些东西。每当我编写文档时,RStudio 的预览都会将我带回到文档的开头。有没有办法让这个新的预览显示一个更接近我默认工作的位置?例如,我可以在我的打字光标附近的位置进行预览吗?

评论提出了一些解决方法。到目前为止,我最好在 RStudio 的预览窗口提供的搜索栏中输入我正在工作的部分的部分编号。我会单击目录中的相关条目,但我使用output: github_document: toc: true number_sections: true的是 ,它正在等待其编号目录的补丁。

标签: rr-markdownrstudiopreview

解决方案


不像您想象的那么简单,但是可以使用 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("# %s\n\nSection text here.\n\n", 1:10), sep = "")
```

# GOTO

Scroll Here

```{r, results='asis'}
cat(sprintf("# %s\n\nSection text here.\n\n", 11:20), sep = "")
```

推荐阅读