Programmatically displaying equations using RMarkDown - r

Evening all.
I am stumped. I have an RMarkDown document within which I need to be able to loop over a chunk a number of times, once for each item in a list. THIS I can do readily and it all works bar one part...
I need to be able to build and display a formula which changes with each chunk iteration. Outside the chunk (i.e. directly in the document) this is simple, with;
\[\alpha = (k/(k-1)) * \frac{(Var(T_{P})-\sum Var)}{Var(T_{P})} \]
I would expect it to be something like...
cat("\\frac{(Var(T_{P})-\\sum Var)}{Var(T_{P})}")
BUT within the chunk this blows apart.
Can anyone shed light here?
Thanks

As baptiste points out it's the math-mode wrapping that makes the difference. Changing the line to cat("$\\frac{(Var(T_{P})-\\sum Var)}{Var(T_{P})}$") sorts it just lovely.
As a tip, using $$ centers the equation on the line which is also useful.

Related

How can I split R/Stan code chunks across two slides (quarto + revealjs)?

I have some large code chunks that exceed the length of one slide. I want the code to easily split across two slides. This comes up when I want to display a large function that I can't easily split across two R code chunks. I also regularly print my slides to pdf for later annotation, so the scrollable code isn't helpful.
Is there any straightfoward way to do this?
I tried adding something like knitr::asis_output("\n\n---\n\n") into an R code chunk to manually add the slide break, following Inserting a slide break within a code chunk in rmarkdown
but this didn't work.

Indentation of bullet points in R pagedown resume

I want to create a resume using the {pagedown} resume template. Overall, it's a great fit, but when I use bullet points, I would like to align the second line of text to the first one.
Here is the current state:
I would like to align the w in the second line to the E in the first one.
I guess, this needs to be set somewhere in the resume.css, but I have not found anything related (but I don't really know CSS, so that means nothing).
Any ideas how to accomplish that?

R Markdown, GGplot showing up above header

I know that its best to include a reproducible example, but I'm not sure how I would do that in this instance and unfortunately the data and rest of the .rmd document includes some PHI so I can't include that, but hopefully you can still help me:
I have a document I'm knitting to PDF in R. Here is a screenshot of my code with the headings above the graph:
When I knit it, here's the output:
I.e. the graph is showing up above the "Count" heading. What the heck? I've tried various odd formatting tricks (adding blank line in between by Visuals header and my count header, etc..) and nothing.
The graph right below it has the same problem (Percent graph is above percent heading), but all the rest of the graphs in my document (18 page pdf) show up where they are supposed to, and here is my code at the top of the document:
Anyone experienced something similar? Any idea where to even start fixing it?

Remove plot element without replotting everything else in R (Shiny)

This is not an specific question, but mostly a "looking for suggestions", since any kind of answer is welcome.
I built a huge website using pure shiny in R, but a really small thing is bothering me..
I'm plotting some elements over an image, let's say.. rectangles (could be anything), and interact with those elements using their position on the image. One of the actions is deleting some of those elements, but -as far as my understanding of R plotting system is- that can't be done, since an element plotted in R can't be removed, so my workaround is basically "plotting everything else" but the element i just deleted.
So, my question is, is there any way to "remove" an object in a plot without having to replot everything else (basically not making a new plot with everything but the deleted element)?
Thanks in advance!

Is there a way to replace the legend or axis without redrawing the whole plot?

Sometimes I will place a legend at a particular location on the plot, let's say topright, and then I see that it covers an important part of the plot. Is there a way I can switch it to topleft or some other place without having to run all the other commands first? Sometimes I have the same problem with the axes, I misspell a word and then I have to issue all the commands again.
I eventually place everything in an R script which means this becomes less of a problem, but sometimes I want to quickly test something in console. Please tell me I have overlooked a basic command that does this.
There is no such thing, but you're on the right track - a script is definitely the way to go. I would also recommend R Studio, a free R IDE, which gives you several displays, one for scripts, one for the console, one for your plots - it's great! It makes working with scripts as easy as interacting with the traditional R console.
As Gabriel told you, a script is the best way. However, the following link could help you:
Using Inkscape to Post-edit Labels in R Graphs

Resources