# R Markdown - Questions and Answers

#1

I guess it is time for a markdown category. Here is my first question:

When I am creating equations, e.g. $X = coef*b$, how do I insert an object previously created as part of a coded analysis? In the example, I would like to replace coef with a value stored in coef[1]. Any suggestions?

Thanks,

Rene

#2

Sorry I don’t know that package so it might be stupid but have you tried to use the fonction print(coef[1])?

#3

Here is one solution using markdown

#4

Cool! Thanks! Already used your example…Not sure though if it is worth it to write equations in markdown that way. It gets painfully verbose:

$MRLM =\frac{e^{r round(coef(best.model.4)[1],3)+r round(coef(best.model.4)[2],3)*log(x)+r round(coef(best.model.4)[3],3)*log(x1)+r round(coef(best.model.4)[4],3)*log(x2)+r round(coef(best.model.4)[5],3)*log(x3)}}{(1+e^{r round(coef(best.model.4)[1],3)+r round(coef(best.model.4)[2],3)*log(x4)+r round(coef(best.model.4)[3],3)*log(x5)+r round(coef(best.model.4)[4],3)*log(x6)+r round(coef(best.model.4)[5],3)*log(x7))}}$

To get:

#5

Can you build the equation beforehand, in a chunk perhaps, then evaluate it when needed?

#6

I am not sure if I get you right…would this make the code less verbose? As far as I understand, the code would be just somewhere else…

#7

You could do all the rounding in 1 step. Even if it did not make the code any less verbose it would separate the building of the equation from using it. That would not interrupt the flow of the paragraph so much. But it is a long equation - you are not going to be able to get around that.

#8

Nice idea! Now I get it…thanks!

#9

Hello everyone,

I am rendering a docx in R markdown and I would like to learn more about aligning text and other contents. in this case I am trying to center my equations. I can’t work out how to do it. Following code:

<div id="LMMR" class="equation">
(@LMMR)
$log\frac{Band1}{Band2}=log(Band1)-log(Band2)$
</div>


outputs:

1. log Band1/Band2=log(Band1)-log(Band2)

I definitely want to keep the numbering.

Any suggestions?

Rene

#10

Hi Rene. Seeing as you are embedding HTML directly into the markdown, you could try changing the DIV’s CSS style using “style”. This is untested, but try:

<div id="LMMR" class="equation" style="width:800px; margin:0 auto;">
(@LMMR)
$log\frac{Band1}{Band2}=log(Band1)-log(Band2)$
</div>

#11

Hi Rene,

You just need to replace one dollar sign ($) with two dollar signs ($$) to centre the equation. Cheers, Drew (@LMMR)$$log\frac{Band1}{Band2}=log(Band1)-log(Band2)$$#12 Pfff…really? o.O Thanks for responding Drew and Josh! It is a bit puzzling what syntax is possible in markdown and then for what output type this is true… I will get there… Thanks! #13 RMarkdown is using Pandoc to do the conversion so if you look up documentation for pandoc, you will find what you need - http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#math. In short, it allows and$$ in most output formats but some do a terrible job of rendering it. PDF will be (by far) the best supported output for anything in$ or . Plus, using PDF output should (I haven’t actually tried it) support arbitrary LaTeX code - giving you total control over your output.

#14

Thanks, Robert! That’s very helpful! Ina told me about your meeting…great to have another enthusiastic peer on board. Welcome!

Cheers