RMarkdown table captions breaks page bottom margin - r

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.

Related

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?

Placement of pictures in markdown/bookdown

I am writing a text in bookdown with Rstudio and want to include a picture within the text like this:
Some text goes here
```{r, fig.cap="\\label{fig:figs}figlabel"}
knitr::include_graphics("images/image.png")
```
Some other text goes here.
However, when I render the book with bookdown::render_book("index.Rmd"), The inserted picture is placed on the next page rather than where it is placed in the text. I want it to be placed between the two sentences, but it is placed below the last one.
Is there a way to control where in the text the image is rendered? I have tried to look at chunk options for images, and also in the bookdown documentation, but neither seem to document ways to control placement of figures.
LaTeX will try to find an optimal location for the figure. You can force the placement of floating figures and tables with \FloatBarrier. Note that doing so, you might end up with a lot of white space on the bottom of the page.
Some text goes here. See fig. \#ref(fig:my-fig)
```{r my-fig, fig.cap="fig caption"}
knitr::include_graphics("images/image.png")
```
\FloatBarrier
Some other text goes here.

Rmarkdown Image Skips Ahead of Text

I am putting together an Rmarkdown PDF document with the following YAML settings:
---
output:
pdf_document:
fig_caption: true
fig_crop: true
toc_depth: 3
header-includes:
- \usepackage{hyperref}
---
Within the body of the document I've inserted a few PNG images, using the following syntax
Paragraph 1..........
![Caption](path/image.png)
Paragraph 2....
And when the document is rendered, the image appears as expected within the text, between Paragraph 1 and Paragraph 2. However, I am getting some unpredictable results where the rendered image appears after Paragraph 2 in some cases and I can't manage to solve it.
I have run into the same problem. It appears if you wrap the image in "paragraph" tags, the image will be in-line with the text.
Paragraph 1...
<p>
![](image.png)
</p>
Paragraph 2...
When tackling similar issues, I've used \FloatBarrier (from the placeins package) to control positioning. I'm not the most experienced knitr rmarkdown LaTeX user, but I've had success with that before.
Basically, the images "float"; you can control what the can't float past by inserting a barrier. That description is crude, but you might find the technique effective.
\newline seems to work.
History and Overview of R
![R programming](Images/R.PNG)\newline
will insert vertical space
to keep your figure captions make sure the ![] is still in a separate paragraph (separated by blank lines above and below) in the Rmarkdown document
### Heading 1 ![This is my figure caption](`r fig_var`){width=400px}
### Heading 2
where fig_var is an r variable that contains the full path to the figure image
One option is to add
\newpage
to act in a similar way as the Floatbarrier. It is not to elegant but seems to work.
For greater clarity consider the example from above:
Paragraph 1..........
![Caption](path/image.png)
Paragraph 2....
To avoid the image to move in front of the second paragraph, you could do the following:
Paragraph 1..........
![Caption](path/image.png)
\newpage
Paragraph 2....
There is no correct answer to this.
Try adding fig.show='hold' to keep your images where they should be
The paragraphs might be skipped because Latex will try and fit the text/images with least space.
I sorted my issue out using (1) and to "work with" (2), you can use
\pagebreak in an appropriate position depending on what is before and after paragraphs 1 and 2.
This can be done only after seeing the pdf result, by better fitting of the image in question into potentially a next page (more space). Of course, it would also mean adding the page break elsewhere (e.g before or after any of the p1, p2 or the image).

r knit word document plots automatically re-sized

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

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