Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can R Markdown be made to preview from anywhere other than the start of the document?

I'm currently writing something in R Markdown. Whenever I Knit the document, RStudio's preview takes me back to the very beginning of the document. Is there a way to make this new preview display a location closer to where I've been working by default? For example, can I make it preview at a location near where my cursor for typing is?

The comments have suggested a number of workarounds. So far, my best is to just type the section number of the section where I'm working in to the search bar that RStudio's preview window provides. I'd click on the relevant entry in the table of contents, but I use output: github_document: toc: true number_sections: true, which is waiting on a patch to its numbered tables of contents.

like image 823
J. Mini Avatar asked Jul 31 '21 23:07

J. Mini


People also ask

How do I Preview Markdown in R?

There are two ways to render an R Markdown document into its final output format. If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it.

What is the difference between R Markdown and Markdown?

R Markdown is an extension of the markdown syntax. R Markdown files are plain text files that typically have the file extension . Rmd . They are written using an extension of markdown syntax that enables R code to be embedded in them in a way which can later be executed.

Why is creating an R Markdown file document a useful thing to do?

rmarkdown will preserve the text, code results, and formatting contained in your original . Rmd file. Conversion lets you do your original work in markdown, which is very easy to use. You can include R code to knit, and you can share your document in a variety of formats.


1 Answers

Not quite as simple as you had in mind, but it is possible to use javascript to control what section of an html document is displayed:

---
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 = "")
```
like image 125
the-mad-statter Avatar answered Oct 22 '22 11:10

the-mad-statter