r knit word document plots automatically re-sized - r

I am trying to add a plot to a word document. I would like the plot to maximize the area available when the page size is set to legal with narrow margins. I can set the fig.width and fig.height but it seems the plots get automatically re-sized to fit the default page size (letter) with normal margins.
Here is a sample .rmd file that produces the same results:
---
title: "plot-resize"
output: word_document
---
Plot with the height set to 3" and the width to 7.5":
```{r, echo = FALSE, fig.height=3, fig.width=7.5, warning=FALSE, message=FALSE}
plot(cars)
```
However when the word document is created the image is automatically
re-sized to 79% of this.
I can re-size the plot in word, but it would be nice to not have to.
Is there a way to set the page size and margins in the .rmd file?
Is there a way to ensure that the plots stay at the specified size even if they do not fit within the margins of the created word document?

You can redo the MS Word template file - see http://rmarkdown.rstudio.com/articles_docx.html - you would have to change your margins to narrow (0.5") in the MS Word template file you are using (Under the Layout ribbon). Then, right click on the figure and select size and position, and then adjust scale height and width to 100%. You would then have to save your template file (and don't forget to close it!) and then add this to your YAML:
title: "plot-resize"
output:
word_document:
reference_docx: mynew_template.docx

Related

Flexdashboard storyboard: including text above graphics in the main frame area

I want to include some text above the graphics in the main area of a frame in Flexdashboard storyboard. Here's the minimal example.
---
title: "Title"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
horizontal_layout: scroll
runtime: shiny
---
Storyboard name {.storyboard}
=====================================================================
### Storyboard frame 1
```{r}
renderPlot({
plot(rnorm(1e3))
})
```
>Footnote
***
Data commentary text.
When I add the text just above and outside the R chunk, the graph gets truncated at the bottom.
The graph needs to be inside renderPlot() to include interactive elements. It appears the problem is because the height parameter of renderPlot() is "auto", which automatically sets the height of the graphic to the height of the whole frame, and since the text is included, the height that it sets means the graph doesn't fit the area less the text and gets truncated. I found by experimenting the height can be set to a fixed size in pixels. I can set the height in pixels by trial-and-error that would look OK on my screen, but unfortunately it doesn't fully solve the problem, because users who open the app in a different screen resolution would get a messed up layout.
Personally speaking, fixing the height is not a big issue, fixing the width is the bigger problem for users with different screens. Both overflow height and width can be converted to a scrollable page, but it is always comfortable to scroll the document vertically but not horizontally, and this is what most websites are doing. Anyhow, if you don't want to fix the height of the plot with px, the easiest way is to add a scroll bar to the left column so users can scroll and see the hidden content:
Add this chunk and you are all set.
```{css}
.chart-wrapper .chart-stage {
overflow-y: auto;
}
```

How can I make an R Markdown figure chunk hyperlink to the raw image when rendering as HTML?

I would like all of my graphics to link to a full image view once rendered (as the page layout constrains the width and I's like users to be able to zoom in). The chunks are rendered as static images, how do I wrap those in an anchor to the image?
e.g.
```{r "totals", echo=FALSE}
qplot(mtcars$mpg, fill = mtcars$cyl, binwidth = 2)
Renders as
![plot of chunk totals](totals-1.png)
But I want something like
[![plot of chunk totals](totals-1.png)](totals-1.png)
I can wrap the chunk in a markdown link, but that seems a bit clunky and I wondered if there is a way to dynamically code it in the chunk so the image name comes from the chunk name in the same way?

How to make scrollable slides in an ioslides presentation with rmarkdown

I am using RMarkdown to create an ioslide presentation with shiny.
Some of my slides do not actually fit on one page and are truncated.
Since this is a HTML output, I would like to add a scroll bar to make my long slides scrollable.
I have been googling a lot and found a partial solution to make R code chunks scrollable. However I want to make my slides scrollable regardless of the content.
This is a toy Rmd example giving slides not fitting on one page:
---
title: "Untitled"
date: "30 October 2018"
output: ioslides_presentation
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Shiny Presentation
- A very long
- and boring
- list of
- bullet points
- just a
- toy example
- obviously
- not over yet
- almost
- not quite
- finally
- out of frame!
I would like to make this slide scrollable since it does not fit on one page.
Edit: I am not sure why this is being heavily downvoted - would appreciate a constructive comment :) In the meantime, I did remove the css tag which may have brought people not familiar with rmarkdown.
Self-answer:
The bit of CSS that will make the slide scrollable (both horizontally and vertically but you just have to remove one line if only vertical scrolling is needed) is:
slides > slide {
overflow-x: auto !important;
overflow-y: auto !important;
}
Note that the slide gets a height from ioslide so there is no need to specify a height (and in fact it seems to introduce visual glitches if you do). Using auto instead of scroll makes sure a scrollbar only appears when there is a need.
You can either add this CSS directly in the Rmd in between <style> tags or put the CSS in a separate file (e.g. scrollable_slides.css).
The CSS file can then be added to the Rmd like this (assuming scrollable_slides.css is in the same directory as the Rmd):
---
title: "..."
output:
ioslides_presentation:
css: 'scrollable_slides.css'
runtime: shiny
---

RMarkdown table captions breaks page bottom margin

Whenever I use captions in tables,
like
kable(df[1:10, c(7,1:6)], caption = "This is a caption")
the tables and content in the pdf generated by knitr are pushed below the limits of the bottom margin, thus becoming unreadable. Sometimes entire sections are missing, hidden off margins.
Also, plots positions go crazy: they are printed anywhere but the right place in the pdf.
using results="asis" in chunk options doesn't help.
Using pander causes the same problems.
If I remove all table captions and use some \newpage in the .rmd,
the pdf margins are fine.
Is there a safe way to use table captions?
The pdf in question is here: see page 14 for an entire section missing and table hiding in the bottom margins. Also, the plots are where they want, like if they had proper needs...
github repo
this is kind of a anti-climax,
but as it happens,
this problem was being caused by chunks that printed var values to the document, something like:
```{r}
sampled.values <- sample(1:100, 10)
sampled.values
```
when rendered through render(), this code chunk prints the value of sampled.values and this ruins the pdf pagination.
That's it: all page bottom margins are ok now that I removed all those var calls.

Resizing images in RMarkdown

I'm trying to convert a R markdown .Rmd document to .pdf. Unfortunately, the images are too large. Is there any way to change the size of the image? I Can't use html, this is markdown to pdf.
Use this at the beginning of a chunk:
Decimals assigned to fig.height and fig.width are interpreted as inches. Other units of measure also allowed if explicit.
```{r, echo=FALSE, fig.height=2.7, fig.width=9}
#your R code here
```
I found a comfortable solution by the combination of fig.height, fig.width, dpi and out.width.
You can set global parameters at the top by:
knitr::opts_chunk$set(out.width="400px", dpi=120)
You can overwrite these properties in any chunk, just set the parameters you need.
dpi increases the quality image, so you have to adjust by the other parameters.
out.width adjust the size once the image is created.
Decreasing values in fig.height and fig.width will cause the text/numbers to be bigger (same as reducing image window in Rstudio)
There is a simple way to resize Images and still being able to add Captions. Use the following syntax within your RMarkdown code and place the Image's Caption beneath the Image:
<!-- Einbinden von Bildern in RMarkdown -->
\begin{figure}
\centerline{\includegraphics[width=0.5\textwidth]{your_image_name.png}}
\caption{Entitäten zur Persistierung der Special Notifications}
\end{figure}
To scale the image just adapt the width-value from 0.5 to some other percentage-value fitting your needs.
If you don't want to center the images, just remove the \centerline - Command with it's opening and closing brackets {}.
To the best of my knowledge rmarkdown html formats come with Bootstrap. I add the img-responsive with some javascript (at the bottom of my document).
<script>
var d = document.document.getElementsByTagName("img");
d.className += " img-responsive";
</script>

Resources