I would like to keep all the columns of the summary output in same line, do you have a solution?
as shown in the picture, the last column goes to a new line, how to avoid it?
Thanks a lot in advance!
Without a code example, I can't help edit the code. In your Rmarkdown initial set up chunk, put the code below
```
{r setup}
knitr::opts_chunk$set(echo = TRUE)
options(width = 1000)
```
the width = 1000 will need to be adjusted to meet your needs.
here is a link that might help you get more information
Related
I have an rmd chunk of r code with a loop. The structure of the code is like this:
```{r echo=FALSE, results="asis", out.width="100%"}
## out.width="100%"
## fig.width=12
## fig.height=(6+2*ceiling(6/4))
section_number <- 3
i = 1 ## for testing
while (i <= length(target_var_list)) {
target_var <- target_var_list[i]
data_segments <- data_segments(wrangled_devices, target_var)
# Code
exposure_chart_data <- monkeyr::get_exposure_chart_data(wrangled_obs, wrangled_devices, target_var)
exposure_plot <- monkeyr::get_exposure_plot(exposure_chart_data, target_var)
# knitr::opts_chunk$set(fig.height=(6+2*ceiling(data_segments/4)))
print(exposure_plot)
# print(exposure_plot, fig.height=(12+2*ceiling(data_segments/4)))
section_number <- section_number + 1
cat("\n\n\n")
i <- i + 1
}
```
I have commented out a few attempts I made to control the width and height of the plot. And I have commented out 2 attempts I made to control the knitr behaviour on a per plot basis.
The problem is that I can't find a reliable way to control the plot size that accommodates different lengths of the target_var_length.
It is possible to control the height at chunk level, but that is then fixed, and won't respond to each element in the loop. Here are some viz. What I would like is for the actual bar to be the same size in every case. So the case with 3 values would be 75% as wide as the 4. And the case with 7 would look be 2 rows, so twice the height of the 4. Do you see what I mean...
After quite a few hours of messing around with different approaches, here are some insights and an answer.
knitr::opts_chunk$set
I expected this to take effect on execution and change the chunk options for whatever elements follow. To change the plot height based on the number of rows / column in a facetted plot, I tried this:
knitr::opts_chunk$set(fig.height=(6+2*ceiling(data_segments/4)))
However it has no effect. The documentation bears this out. This actually sets the default chunk settings for subsequent chunks, and has no effect whatsoever on the current chunk. I encountered another function:
knitr::opts_current$set(fig.height=(6+2*ceiling(data_segments/4)))
The documentation as much as warns you off using this. And I found that it didn't achieve the expected results either in any case.
Blind Hope
I considered the possibility that I was overthinking this and left it up to blind hope by removing all efforts to control the height. Sometimes things just work out you know! ... They didn't.
Using an rmd child chunk
This is the approach that I finally got to work. It's a slightly horrible hack. My first effort was to create a separate rmd file for each plot:
```{r echo=FALSE, results="asis", out.width="100%", fig.height=(6+2*ceiling(data_segments/4))}
print(myPlot)
```
But that meant creating lots of new rmd plots. I have a major problem with how messy that would get. So I cleaned it up by using a single rmd file for any plot and lumped the code to call it into a fucntion.
resize_plot <- function(resizePlot, resizeHeight) {
resizePlot <- resizePlot
resizeHeight <- resizeHeight
res <- knitr::knit_child('resizePlot.rmd', quiet = TRUE)
cat(res, sep = '\n')
}
Now to insert a custom height plot I just call my new function:
resize_plot(exposure_plot, 3.25*ceiling(data_segments/4))
And the single rmd file just looks like this:
```{r echo=FALSE, results="asis", out.width="100%", fig.height=resizeHeight}
print(resizePlot)
```
And bingo - it looks perfect!
I am pretty new in R markdown and while creating a report "I like to hide data parsing code"
I have tried "echo=FALSE", but it does not doing the job in this particular case.
Any help would be much appreciated.
**Data Parsing**
```{r echo=FALSE}
library(ggplot2)
library(readr)
SU1 <- read_csv("......./SU.csv")
I like to hide the below chunk in the report
Thanks
Thanks.In fact i was able to solve it by adding Message= FALSE.
One of cells in my RMarkdown document
```{r echo=FALSE}
head(data,3) %>% knitr::kable(caption = "Pierwsze 3 wiersze ze zbioru danych Lista_1.csv", digits = 2, booktabs = T)
gives weird result after knitting to pdf:
Of course there shouldn't be "\begin{table}" ,"\caption{}" and "\end{table}" parts. I use knitr::kable often and it never worked this way. Does anyone know how to fix it?
Edit: I have also noticed that all section headers (like "##Section2") below the table are centered. They shouldn't.
I've just found this question: How do I prevent kable from leaving raw latex in the final document if I include a caption in a table?
and used tip from comment (format = pandoc). It worked for me.
In R markdown when using Beamer slides if you try to plot you need to specify the plot sizes (unlike in the reports) so that the plots fit on a page. This can often result in the plots appearing to be squashed together, as apposed to just a smaller version of the plot.
Is there some method to change the default slide size to alleviate this problem?
I have tried
header-includes:
- \usepackage[papersize={25.6cm,19.2cm}]{geometry}
in the yaml header, and I get the error
! LaTeX Error: Missing \begin{document}.
Using R Markdown I shouldn't need to use this though.
A reproducible example is shown below
---
title: "Plots look bad"
author: "Beavis"
date: "`r format(Sys.time(), '%d/%m/%Y')`"
output: beamer_presentation
header-includes:
- \usepackage{float}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(results = 'hide')
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(cache=TRUE)
knitr::opts_chunk$set(fig.height=3.5)
```
# Introduction
```{r}
pca <- prcomp(iris[,1:4])
biplot(pca)
```
Here, you if you run this the second slide looks like this As you can see the plot is rubbish. What is the best way to avoid this problem?
The biplot function uses par(pty = "s") to force a square plot, so it's not going to fill a rectangular slide. You can make it look better by asking the fig.height to be bigger, but then it will overflow the bottom of the slide. To prevent this, you can set both fig.height to a large number, and out.height (which will be a LaTeX measurement) to something that will fit on a slide. For example, using this chunk
```{r fig.height=10, out.height="0.8\\textheight"}
pca <- prcomp(iris[,1:4])
biplot(pca)
```
I see this output:
I'd recommend a smaller fig.height, but your preference may be different.
Both fig.height and out.height could be specified using knitr::opts_chunk$set as defaults for all slides if you want.
You could also specify out.width="\\textwidth" for a really ugly stretched plot that fills the slide, but I wouldn't recommend it.
I am producing an HTML output but I am having issues with the output width of R code output.
I'm able to adjust the figure width with no difficulty but when I try to write a data table or the factor loadings, R is outputting at a fixed width which is only about a third of my screen width. This results in the columns of the table being split up rather than all of the columns displayed in a single table.
Here is a reproducible example:
---
output: html_document
---
# Title
```{r echo = FALSE, fig.width=16, fig.height=6}
x = matrix(rnorm(100),ncol=10)
x
plot(x)
```
Add this at the start of your document:
```{r set-options, echo=FALSE, cache=FALSE}
options(width = SOME-REALLY-BIG-VALUE)
```
Obviously, replace SOME-REALLY-BIG-VALUE with a number. But do you really want to do all that horizontal scrolling?
Your output is probably being wrapped somewhere around 80 characters or so.
Also, you can temporarily change the local R options for a code chunk:
```{r my-chunk, R.options = list(width = SOME-BIG-VALUE)}
```