Including git code in the rmarkdown documents while using bookdown - r

Is there any way possible to include code for different languages in Rmarkdown documents while authoring books using bookdown package? I have looked at the knitr option like engine with possible values like python, awk/gawk and the executable path can be set using engine.path.
```{r, engine='python'}
print "Will this code chunk be hidden?"
```
However, I would like to just insert code (eg: git) in the Rmarkdown document without executing it.
For example, like including in the markdown documents
```git
git init
```

If you don't want the code to be executed, you can embed it like:
```
code not executed
```
You can also have code highlighting in specific languages:
```css
my_css{}
```
For your git command, this is execution from terminal, so you can highlight it with:
```sh
git init
```

Related

Compile Bookdown to Markdown?

Is there any way to take a Bookdown project, and build it as Markdown instead of HTML or TeX?
I ask because I need to post-process the final Markdown output from Bookdown, in order to extract R and Python notebooks for download.
In more detail, I am using Bookdown to build a textbook that embeds notebooks to download, where the notebooks contain subsets of the code and text in the bookdown .Rmd files. For example, a single chapter could contain more than one notebook.
In order to do this, I put start and end comment markers in the RMarkdown input text to identify the section that will be a notebook, and then post-process the generated Markdown files to extract the notebook section. As in something like:
<!--- notebook: first_section.Rmd
-->
Some explanation, maybe using Bookdown extra markup such as #a_citation.
```{r}
a <- 1
a
```
<!--- end of notebook
-->
More markdown.
```{r}
# More code not in notebook.
b <- 2
```
Obviously I could use the input RMarkdown pages, but this would be ugly, because all the extended Bookdown markup such as citations, cross-references and so on, would appear in raw and ugly form in the generated notebook. So I'd really like to be able to get the final output Markdown, after merging, resolving of citations and cross references. Is there any way of doing that?
My question is similar to this as-yet unanswered question, but adds my motivation for an official solution to this problem.
With the latest version of bookdown on CRAN, you can use the output format bookdown::markdown_document2, e.g.,
output:
bookdown::markdown_document2:
base_format: rmarkdown::md_document
variant: gfm

How can I convert a Rmd document to a jupyter notebook

I would like to convert a rmarkdown .Rmd document to a jupyter notebook .ipynb.
I found that converting from jupyter to rmd is easy using as described in reference page but for some reason (...) the Rstudio team did not do the other way.
For instance I would like to convert
---
title: "Untitled"
author: "statquant"
date: "03/09/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
After checking, the silver bullet seems to be jupytext
It allows you to convert from or to markdown, rmarkdown, python, ipynb, etc.
This can actually allow you a pretty neat workflow
write a simple R script, script.R, that you can spin into a Rmd document
use knitr::spin('script.R', knit = FALSE) to transform it to Rmd
use jupytext --to notebook script.Rmd to create script.ipynb
share or execute the notebook
sos-rmarkdown provides yet another Rmarkdown to Jupyter notebook converter. The unique features include its support for inline expressions using markdown-kernel, use of multiple kernels in one notebook (using a SoS kernel) to accommodate code blocks in multiple languages, and the ability to execute generated notebook using sos-papermill. It also uses cell meta data to control the display of input and output of code blocks in Jupyter Lab and exported HTML reports.
To use this tool, you can install sos-rmarkdown from pip or conda-forge, then run the converter with command
sos convert input.Rmd output.ipynb
or use option --execute to execute the converted notebook
sos convert input.Rmd output.ipynb --execute
Disclaimer: I am the author of sos-rmarkdown.
Here is another way.
The detailed answer (to convert .rmd to .ipynb) is described here: https://gist.github.com/ramnathv/10012123
TL;DR
Use a 3rd-party Python package notedown with sed command as follows:
1) Install a 3rd-party python package which does the conversion for us
$ pip install notedown
2) Use the installed package to convert from your *.Rmd file (or *.md) to *.ipynb and run the terminal command:
$ notedown example.Rmd | sed '/%%r/d' > example.ipynb

Code / Process for running rmarkdown in base R

All my codes developed in base R and I don't want to use RStudio, however I want to use rmarkdown feature in base R which is available in Rstudio.
I have downloaded rmarkdown package in base r, but not able to derive a code to publish my work
All the output of my codes written in R should be view able through web browser.
First make sure you're using .Rmd as your file extension. If not, rename it to a .Rmd extension. Make sure you have Pandoc installed on your OS.
Next, add the following to the top of the file:
---
title: "Your notebook title"
output: html_document
---
output: could take any value. You can pass in the value of ioslides_presentation for example if you want but it looks like html_document fits the criteria of what you want pretty well.
Once you have that, write your code in any editor (or the R console if you prefer). Use the code chunks and markdown text formatting as you normally would:
```{r}
plot(1:10)
```
In my base R Console, this is how mynotebook.Rmd looks like:
Finally, use the render() function from rmarkdown. You can either attach it and run render():
library(rmarkdown)
render("mynotebook.Rmd")
Or, run rmarkdown::render("mynotebook.Rmd").
Notice that the use of RStudio is not required at all since Pandoc is the document converter performing this task. For the so inclined, this is what its documentation has to say:
When you run render, R Markdown feeds the .Rmd file to knitr,
which executes all of the code chunks and creates a new markdown (.md)
document which includes the code and it's output.
The markdown file generated by knitr is then processed by pandoc
which is responsible for creating the finished format.
This may sound complicated, but R Markdown makes it extremely simple
by encapsulating all of the above processing into a single render
function.

How to prevent urlencoding of liquid tags?

Building a post for a Jekyll site from an RMarkdown document and running into a problem with pandoc's "helpful" URL encoding of links. I hit the RStudio "Knit" button on this document:
---
output: md_document
---
```{r setup, include=FALSE}
knitr::opts_knit$set(base.url = '{{ site.baseurl }}/')
knitr::opts_chunk$set(fig.path = 'assets/images/')
```
```{r sin_plot}
plot(1:100, sin(1:100))
```
And I get this markdown document:
plot(1:100, sin(1:100))
![](%7B%7B%20site.baseurl%20%7D%7D/assets/images/sin_plot-1.png)
What I need is this markdown document:
plot(1:100, sin(1:100))
![]({{ site.baseurl }}/assets/images/sin_plot-1.png)
The liquid tag will be handled by Jekyll, but pandoc is getting in the way.
Issue opened with rmarkdown
Since you don't need Pandoc at all, you don't need to use rmarkdown, either. You can just use "vanilla" knitr (note that rmarkdown = knitr + Pandoc), i.e., knitr::knit() instead of rmarkdown::render().
For Jekyll websites, blogdown may make your life a little easier since it supports Jekyll, too: https://bookdown.org/yihui/blogdown/jekyll.html You won't need to hit the Knit button but just blogdown::serve_site() and enjoy LiveReload. If you have never used blogdown before, you need to read at least Chapter 1 of the blogdown book (but ignore Hugo there).
As noted in the other answer, the "Knit" button calls rmarkdown::render and is unavoidably knitr + Pandoc. To give the RStudio users contributing to this blog a button to push, I moved everything to a simple Makefile.
RMD := $(shell find _posts/ -name '*.Rmd')
PORT = 4321
export GEM_HOME = ~/.gem
.PHONY: all
all: Gemfile.lock $(RMD:%.Rmd=%.md)
bundle exec jekyll build --drafts --baseurl=/p/$(PORT)
Gemfile.lock:
bundle install
%.md: %.Rmd
Rscript --vanilla -e "knitr::knit('$<', '$#')"
For a live preview, the RStudio users can leave a servr::httw('_site') process running in their console and hit the "Build" button to update.

Ending a document in r Markdown and continuing with code

I am writing a report using r markdown. However, after this report is produced I would like to continue to analyse the data without having to open up a new editor window.
I was wondering if there is a simple command I could use to express the end of the document?
Thanks.
It appears that you are not necessarily interested in ending your markdown document but in hiding your results. The code below will enable you to continue the analysis in the same window and to exclude it from appearing in the core document when compiled.
```{r results='hide', message=FALSE, warning=FALSE}
# Stuff that you want to do
```
For a more detailed explanation, you may want to have a look at the Chunk Options in the knitr documentation.

Resources