Put "choose" brackets in rmarkdown - r-markdown

I want to write about the binomial distribution in a rmd document but I cannot work out how to represent the large bracket which surround two values written above one another. (Reads x choose y)
Any thoughts?
Tried various configurations of "choose" and "binom" but no joy.

Using RStudio, this is what my .Rmd file looks like:
---
title: "Untitled"
author: "sahir"
date: "March 12, 2015"
output: html_document
---
$$P(X \leq k) = \sum_{y \leq k} \binom{n}{y} p^y (1-p)^{n-y}$$

Related

Creating two table of contents with different depths in R Markdown

I created my lecture notes in pdf format using RMarkdown but it has many subheaders. Because of this, I want to create two different table of contents: first table of contents with 1 depth, then table of contents in 4 depth respectively.
Adding both toc_depth: 1 and toc_depth: 4 did not work.
My YAML header:
---
title: "Lecture Notes"
author: "x"
output:
pdf_document:
highlight: tango
toc: true
toc_depth: 4 #depth table of contents
number_sections: true
documentclass: article
classoption: a4paper
fontsize: 12pt
geometry: "right=1cm, left=1cm, top=1cm, bottom=3cm"
---
As far as I am aware, I don't believe two Table of Contents is possible. One thing you can try is if you don't want a certain heading numbered, you can do
## Including Plots {-}
Which heads it has a header, but doesn't number it in the TOC. Here is the two output differences
without {-}
with {-}
These can be used in places like the Preface, about the authors, etc.
This is not a perfect solution (maybe some with more knowledge could automate this).
The solution works if the intermediate latex file is kept and a short table of contents is manually added using the shorttoc package.
Adding shortdoc:
...
\documentclass[
]{book}
\usepackage{shorttoc}
...
And than adding it between title and TOC:
\shorttableofcontents{〈title 〉}{〈depth 〉}

How to embed highcharter in rmarkdown to share (not a loop)

I cannot get two simple highcharter charts in an Rmarkdown file I want to share. It works fine and renders in my PC or Mac, but when I share the file, people only see text.
They are not part of a loop, as mentioned in few articles. They are just two charts. For the reproducible example, I literally just opened a new rmarkdown doc and selected html. I replaced the "summary table" of cars with highcharter code. I even tried htmltools::tagList(chart1, chart2) and it does not work.
I am supplying the code. If you please show me how to do it with one chart, I can do it with the second.
Thank you.
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```{r echo=FALSE}
library(magrittr)
library(highcharter)
highchart() %>%
hc_add_series(cars, type = "scatter", hcaes(speed, dist))
I apologize if the tick marks hide the code background, but the three chunks are wrapped with the three tickmarks at the beginning and end of each chunk.
Thank you again.
Yes the code below produces the file below the code. I now have an entire .html file that has the highchart self contained in the file.
---
output:
html_document:
self_contained: yes
mode: selfcontained
---
{r echo=FALSE}
library(magrittr)
library(highcharter)
highchart() %>%
hc_add_series(cars, type = "scatter", hcaes(speed, dist))

Automatically Change RMarkdown Text Styles from One to Another

Due to different journal requirements, I need to frequently change certain text styles within Rmarkdown from one kind to another. For instance, here is an example Rmarkdown document.
---
title: "Changing format of Rmarkdown"
author: "Paul Hargarten"
date: "5/9/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an $\mathcal{R}$ Markdown document. Markdown is a simple formatting syntax for authoring **HTML**, **PDF**, and **MS Word* documents. For more details on using $\mathcal{R}$ Markdown see <http://rmarkdown.rstudio.com>. $\matcal{R}$ is based on $\mathcal{S}$.
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. Calculate a `summary` as follows:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example: `r plot(pressure)`.
Without searching for the exact phrase, suppose that I would like to find and replace the following items:
1. Change items in bold ** ... ** to italics _ ... _.
2. Change items that look like $\mathcal{ ... }$ to bold ** ... **.
3. Change special font `...`, except those that start with r, to \code{ ... }.
4. Add dollar signs to `r ... ` => $`r ... `$.
Is this possible to use regex to make these formatting style changes in
Rmarkdown? Thanks!
This is something that LaTeX is good at, but it will be harder with Markdown.
If you were entirely in LaTeX, you could define your own macros based on the uses for that markup. For example,
\newcommand{\booktitle}[1]{\textbf #1}
used for book titles as \booktitle{The Book}. If you wanted to switch book titles to italic, you'd just change that definition.
Doing this in R Markdown is possible, but you wouldn't be able to mark book titles using **. You could do it (you can embed LaTeX in R Markdown), but it's ugly. For example,
---
title: Using LaTeX
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\newcommand{\booktitle}[1]{\textbf{#1}}
This is \booktitle{The Book}.
Once you're doing this, you might as well switch to Sweave-like *.Rnw format, or all the way to LaTeX.

Unable to force left align text in rmarkdown document

I'm using R markdown for a university assignment. I'd like to have questions number/letters left aligned on my document, but rmarkdown is adding spaces before "a)" and "b)", which I'd like to be aligned with the rest of my text.
---
title: "Untitled"
author: "dsn084"
date: "26 March 2017"
output: pdf_document
---
This is a paragraph.
a) this is part a.
b) this is part b.
And my output ...
As you are using output: pdf_document, the rmarkdown document will be converted into latex before pdf, so you can use latex instructions to accomplish that.
flushleft will do it. Example:
This is a paragraph.
\begin{flushleft}
a) this is part a.
b) this is part b.
\end{flushleft}

Read multiple files in rmarkdown

I use RMarkdown and would like to source multiple .R files before knitting the document. I split my .R files into "read.R" and "calc.R", but rmarkdown returns an error with this method. However if I combine read.R and calc.R into a single file ("combined.R") it runs with no problem.
Here's what I'd like to do.
Read this data in one .R file.
# Read.R
game <- c(1,2,3,4,5)
royals <- c(5,7,3,5,7)
mets <- c(4,1,9,3,2)
data <- data.frame(game,royals,mets)
Then calc in another .R file.
# Calc.R
data$winner <- ifelse(data$royals>data$mets,"royals","mets")
Then run rmarkdown. This doesn't work (sourcing separate files)...
---
title: "rmd_test"
author: "me"
date: "Monday, November 02, 2015"
output: word_document
---
```{r}
source("read.R")
source("calc.R")
print(data)
```
But sourcing a single combined file does...
# Combined.R
# Read data
game <- c(1,2,3,4,5)
royals <- c(5,7,3,5,7)
mets <- c(4,1,9,3,2)
data <- data.frame(game,royals,mets)
# Calc
data$winner <- ifelse(data$royals>data$mets,"royals","mets")
Here's the same rmarkdown file sourcing a single combined.R file.
---
title: "rmd_test"
author: "me"
date: "Monday, November 02, 2015"
output: word_document
---
```{r}
source("combined.R")
print(data)
```
I would like to split the files because my "read.R" files are fairly generic and stable, but the "calc.R" changes and is variable. So it is more convenient to split the two.
If you want to share code between different scripts or even projects. You should consider to put this in a separate package. While this is truly more effort at the beginning, this might pay off later, when your projects are growing or you want to share your code with others.
If you are a "R-package" newbie (like me), there is a very good book by Hadley Wickham available online: http://r-pkgs.had.co.nz/