R Markdown, GGplot showing up above header - r

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?

Related

Can you show the HTML output of an rmarkdown block without knitting the whole document?

I've created a R markdown notebook, to document the results from experiments. With increasing experiments and clinical data, the time to knit the document has increased significantly.
I create quite a number of plots, knit the document and share the HTML with my colleagues.
The question I have is: Is there a way to show the final plots in the way they appear in the HTML documents in RStudio without knitting the document?
It is a pain to try out different dimensions and having to knit the output again and again..
Or is trial and error the only way?
In the plots window, you can go to "Export > Save as Image"; this allows you to change the size of the plot in real time and see what it looks like. It's not quite perfect for this purpose as it shows the size of the plot in pixels, and you'll need it in inches for your Rmd, but you should be able to figure out what conversion is needed on your system. There is an Export > Save as PDF, as well, which does have the output size in inches, but doesn't allow you to resize the plot in real time, though it can "preview" a plot at a given size. Either way, you can play with the sizes of your plots this way (and know what those sizes are) without knitting the whole document.
That's the beautiful thing about R notebooks, you can run all the data analysis and wrangling in a previous R chunk, and then use the next chunk just as a visualization chunk to mess with dimensions. if you use this button it will only run the chunk NOT knit the whole document.

Export images from R to word

I have a code in R with a lot of plots. Copying those plots to a word document manually is too much work, so I am looking for some code that exports those plots directly to a word document. I have tried to use the officer package, but I do not understand it well. I think that with a simple example it will be easier to understand it for me.
For example, suppose I have a code with two easy plots:
plot(1,2)
plot(0,0)
How can I create a word document in which this two plots appear? Thank you very much!

Interactive plots on local .html via .rmd or Shiny

I'm trying to build a .html file via RStudio in to have following function(simplified).
plot1:a simple time series plot of $y_t$, where user can manually pull/drag each dot to change it's values a each time point.
plot2:a time series dependent on $y_t$, such as $f(y_t)=2*y_t+1$, once the value in plot1 changed, the plot2 will also change accordingly.
I want the .html self contained, not cloud based. I'm thinking of plotly, shininy/knitr, but I'm not sure if I'm in the direction or how to connect the dots. Hope anyone can point me to the right direction.
A self-contained/client-side html report sounds well-suited for flexdashboard. It's based on R Markdown, and therefore can accommodate Shiny elements.
I don't know if you can modify values with a mouse, but you can certainly modify values with sliders and other inputs, as well as have a second plot react to a first plot.
You may see some more possibilities in the gallery.

Programmatically displaying equations using RMarkDown

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.

tableGrob not fully shown between two plots using grid.arrange in R

My specific issue is evident when I output 3 plots to a html report using grid.arrange, sometimes the last plot (which is a tableGrob) is cropped.
I am using 3 separate grid.arrange() statements to place a boxplot, a time series plot and a tableGrob in a html report using knitr.
Code being used to output is:
grid.arrange(p1,top=main)
grid.arrange(p2TS)
if(nrow(over5)>0){
tg=tableGrob(over5,theme=tt3,rows = NULL)
tg$widths = unit(rep(1/4, ncol(tg)), "npc")
grid.arrange(tg)
}
This displays perfectly when the tg object has less than roughly 10 rows of data.
However in testing larger datasets I found that the grid.arrange(tg) outputs in a fixed height "window" in the report which is not desirable.
My question is how can I change the height of the grid.arrange(tg) ouput to match the height of the tg object contained inside (with some top and bottom margin)??
I would appreciate understanding the mechanism by which these properties are changed so I can fine tune my output. I would have a 3x1 grid display all 3 outputs in one line of code if possible but all plots get condensed and unreadable so that is why I have 3 separate grid.arrange() statements. I Guess my real issue is I don't understand how there properties are manipulated here.
Help is greatly appreciated.
I found a workaround to use kable which prints a HTML table to the report instead of grid.arrange() with tableGrob() :
grid.arrange(p1,top=main)
grid.arrange(p2TS)
if(nrow(tg)>0){
print(knitr::kable(tg))
}
Also as I was running this code within a for loop I needed to use a workaround on this issue page which requires me to wrap the kable in a print() statement.

Resources