RMarkdown - knitr - png size won't change - r

I'm trying to make a png image longer, but any setting I change has no effect. Following this, three different attempts are shown below. The options change the ggplot object in the last chunk properly. I am using RMarkdown and knitr.
```{r setup, include=FALSE, echo=FALSE}
knitr::opts_chunk$set(fig.width=9, fig.height=6)
library(RODBC)
library(data.table)
library(Matrix)
library(doParallel)
library(tidyverse)
library(gridExtra)
```
```{r , echo=FALSE, fig.height=50 }
knitr::include_graphics("data_sim_plot.png")
```
```{r , echo=FALSE, out.height=50, fig.height=50 }
knitr::include_graphics("data_sim_plot.png")
```
```{r , echo=FALSE, out.height=50, fig.height=50 }
knitr::opts_current$set(fig.height=20)
knitr::include_graphics("data_sim_plot.png")
```
```{r, echo=FALSE}
# a different ggplot object
readRDS("gg.rds")
```

```{r , echo=FALSE, out.width= "500px"}
knitr::include_graphics("index.jpg")
```{r , echo=FALSE, out.width= "800px" }
knitr::include_graphics("index.jpg")
They gave pictures with different length. Is this what you want?

Related

Plot a hierarchy tabs in R Markdown

I have been trying to plot a hierarchy tabs in R markdown but somehow when I knit the document it does not show the tabs. I was expecting: Score should have Plots and Plots123 as sub tab and Score1 should be aligned with Score tab. But my output does not show any tabs.
PS: (I have purposely written `` while defining a chunk because Stack overflow interprets '```' as code)
## Score {.tabset}
### Plots
``
{r pressure, echo=FALSE}
plot(pressure)
``
### Plots123
``{r pressure_1, echo=FALSE}
plot(pressure)
``
## Scores1 {.tabset}
``{r pressure_2, echo=FALSE}
plot(pressure)
``
Here's my version - this seems to work. Try this out exactly and let me know.
For rmarkdown, all sub-headers of the header with the .tabset attribute appear within tabs rather than as standalone sections. Also, trying additional spacing between headers.
---
title: "Test"
author: "Test"
date: "2/29/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Score {.tabset}
### Plots
```{r pressure, echo=FALSE}
plot(pressure)
```
### Plots123
```{r pressure_1, echo=FALSE}
plot(pressure)
```
## Scores1 {.tabset}
```{r pressure_2, echo=FALSE}
plot(pressure)
```

RNotebook not picking up default figure sizes

Say, I have the following RNotebook chunk that plots a figure:
```{r}
plot(cars)
```
Now, I want to plot it as a 10x10 figure. I could use this:
```{r fig.height = 10, fig.width = 10}
plot(cars)
```
and that works fine. But say I want to redefine global figure sizes and default to those. I tried using this:
```{r}
knitr::opts_chunk$set(fig.height = 10, fig.width = 10)
plot(cars)
knitr::opts_chunk$get()$fig.width
knitr::opts_chunk$get()$fig.height
```
but this doesn't resize the figure correctly and yet the default figure sizes have been changed when I check them. Can someone explain where I'm going wrong?
A little playing around and, with an html_document output type, it works if you put the opts_chunk call in the chunk ahead of where you want it to be used.
---
title: "R Notebook"
output:
html_document
---
```{r, setup}
knitr::opts_chunk$set(fig.width = 5)
```
```{r}
knitr::opts_chunk$set(fig.width = 8)
```
```{r}
plot(cars)
```
```{r}
knitr::opts_chunk$set(fig.width = 12)
```
```{r}
plot(cars)
```

why does kable not print when used within a function in rmarkdown

I have to repeat certain outputs in many rmarkdown reports and want to write a function to use for this.
Calling a function outputs plots ok when I knit the rmd file but not kable data frames.
For example
---
title: "Markdown example"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Markdown example
```{r mtcars}
make_outputs <- function(){
knitr::kable(head(mtcars))
plot(mtcars$mpg, mtcars$cyl)
hist(mtcars$cyl)
}
make_outputs()
```
Displays the plots but not the kable table.
You can do this by using print to print the kable output, setting the results="asis" of the code chunk and then using kable_styling from package kableExtra.
This works for me:
```{r mtcars, results='asis'}
library(kableExtra)
library(knitr)
make_outputs <- function(){
print(kable_styling(kable(head(mtcars))))
plot(mtcars$mpg, mtcars$cyl)
hist(mtcars$cyl)
}
make_outputs()
```
Using a return statement with all objects in a list can help here, You can try recordPlot or plot from base R to solve your problem, By putting each of these plots in list, I managed to get the plots along with your table. Changed your code a bit in return statement to plot each of the plots along with tables like this.
Option1:
Using list in return with all the objects binded together without using lapply in function call
---
title: "Markdown example"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Markdown example
```{r mtcars}
make_outputs <- function(){
return(list(hist(mtcars$cyl),
knitr::kable(head(mtcars)),
plot(mtcars$mpg, mtcars$cyl)))
}
make_outputs()
```
Another version (In case you don't want the code to print hist output to your html then you can use below function to suppress it.
make_outputs <- function(){
h1 <- plot(hist(mtcars$cyl, plot=FALSE))
h2 <- knitr::kable(head(mtcars))
h3 <- plot(mtcars$mpg, mtcars$cyl)
return(list(h1, h2, h3))
}
Option2:
Another (better version by using invisible function on lapply to suppress NULL printing, then using results='asis' option in the markdown settings as below gives a clean output than earlier.
---
title: "Markdown example"
output: html_document
---
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_knit$set(root.dir= normalizePath('..'))
knitr::opts_chunk$set(error = FALSE)
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Markdown example
```{r mtcars, results='asis'}
make_outputs <- function(){
return(list(plot(hist(mtcars$cyl, plot =FALSE)),
knitr::kable(head(mtcars)),
plot(mtcars$mpg, mtcars$cyl)))
}
invisible(lapply(make_outputs(), print))
```
This has given me a histogram, a scatter plot and a table in the knitted html document. Hope this helps, Not sure though if you wanted this way. Please let me know in case you wanted in any other way.
The problem seems to be related to knitr::kable incorrectly detecting the environment for the printing when it is embedded inside a function. This interferes with its ability to correctly figure out how to format. We can hack around this by placing the object to print in the top level environment before we print it.
---
title: "Markdown example"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
print_kable = function(x) {
print(kable_print_output <<- x)
cat('\n')
}
```
# Markdown example
```{r mtcars, results='asis'}
make_outputs <- function() {
print_kable(knitr::kable(head(mtcars)))
plot(mtcars$mpg, mtcars$cyl)
print_kable(knitr::kable(tail(mtcars)))
}
make_outputs()
```
I got something similar working by
moving the knitr::kable(head(mtcars)) inside a return() at the end of the function.
including results = 'asis'
e.g.
---
title: "Markdown example"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Markdown example
```{r results = 'asis'}
make_outputs <- function(){
print(plot(mtcars$mpg, mtcars$cyl))
print(hist(mtcars$cyl))
return(knitr::kable(head(mtcars)))
}
make_outputs()
```
If you use ggplot for plots, you will need to wrap your plot inside print()

Printing graph to a PDF file apart and a R Markdown output at same time

Supposing if I have this function to print a plot in a PDF file:
generatePlot<-function(values) {
pdf(file = "foo.pdf")
barplot(values, main = "A simple example")
dev.off()
}
And then I am doing this in a "test.Rmd", parameterizing r warning=FALSE, message=FALSE, echo=FALSE, that will output a PDF document:
tmp.values <- sample(10, 6)
generatePlot(tmp.values)
The problem is: plot just is appearing on "foo.pdf" and not on "test.pdf".
In the second one, I observe only the following:
## pdf
## 2
What do I have to do for the plot to be printed in both files?
Try the following:
---
title: "My HTML page"
output: pdf_document
---
```{r, warning=FALSE, message=FALSE, echo=FALSE}
generatePlot<-function(values) {
barplot(values, main = "A simple example")
dev.copy(pdf, "foo.pdf")
invisible(dev.off())
}
```
```{r warning=FALSE, message=FALSE, echo=F}
generatePlot(mtcars$mpg)
```
As you can see I am using dev.copy instead to make sure that the plot is printed on the default device first and then copied to the pdf device which saves the plot at the location of the Rmd document. In order to suppress the output of dev.off() use invisible().

More than three xtables on a page in knitr

I am trying to place more than three tables on one page of a pdf using knitr and xtable. The following code places the fourth table on the second page. If they are size appropriate, is there a way to include all of the tables on one page?
The following example places the fourth table on the second page, although there appears to be room on the first page.
---
title: "Example Code"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(xtable.comment = FALSE)
library(xtable)
```
```{r t1, results='asis'}
xtable(cars[1:2,])
```
```{r t2, results='asis'}
xtable(cars[3:4,])
```
```{r t3, results='asis'}
xtable(cars[5:6,])
```
```{r t4, results='asis'}
xtable(cars[7:8,])
```
I think this is due to one of the settings like totalnumber or \textfraction explained here on TEX.SE. However, I couldn't come up with settings that place all four tables on one page so far.
Therefore, if the tables don't need to actually float, I suggest loading the float package and using the position specifier H:
---
title: "Example Code"
output: pdf_document
header-includes:
- \usepackage{float}
---
```{r setup, include=FALSE}
options(xtable.table.placement = "H")
library(xtable)
```
```{r t1, results='asis'}
xtable(cars[1:2,])
```
```{r t2, results='asis'}
xtable(cars[3:4,])
```
```{r t3, results='asis'}
xtable(cars[5:6,])
```
```{r t4, results='asis'}
xtable(cars[7:8,])
```

Resources