问题
Question:
What is the current working solution to set the width of r code output in html files? I would like to set width to something big and use a slider in the html output.
options(width = XXX)
seems not to work anymore.
Example:
---
title: "Width test"
output:
html_document:
theme: default
---
```{r global_options, echo = FALSE, include = FALSE}
options(width = 999)
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE,
cache = FALSE, tidy = FALSE, size = "small")
```
```{r}
sessionInfo()
```
```{r}
dataM <- matrix(rnorm(100, 5, 2), ncol = 15)
dataM
```
Result:
sessionInfo() output on the screenshot above.
Related:
(options(width = 999)
is not working for me)
knitr: How to prevent text wrapping in output?
How to adjust the output width of RStudio Markdown output (to HTML)
回答1:
You can use this to make the pre
blocks scroll horizontally if it overflows.
---
title: "Width test"
output:
html_document:
theme: default
---
<style>
pre {
overflow-x: auto;
}
pre code {
word-wrap: normal;
white-space: pre;
}
</style>
```{r global_options, echo = FALSE, include = FALSE}
options(width = 999)
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE,
cache = FALSE, tidy = FALSE, size = "small")
```
```{r}
sessionInfo()
```
```{r}
dataM <- matrix(rnorm(100, 5, 2), ncol = 20)
dataM
```
For a scrollable height, create a container div with a max height and a overflow-y: auto;
or overflow-y: scroll;
Similar question/answer
---
title: "Height test"
output:
html_document:
theme: default
---
<style>
.pre-scrolly {
max-height: 150px;
overflow-y: auto;
}
</style>
<div class='pre-scrolly'>
```{r}
sessionInfo()
```
</div>
回答2:
You could reset width
and max-width
using custom CSS e.g. like this:
---
title: "Width test"
output:
html_document:
theme: default
---
<style>
.main-container { width: 1200px; max-width:2800px;}
</style>
```{r global_options, echo = FALSE, include = FALSE}
options(width = 999)
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE,
cache = FALSE, tidy = FALSE, size = "small")
```{r}
sessionInfo()
```
```{r}
dataM <- matrix(rnorm(100, 5, 2), ncol = 15)
dataM
```
回答3:
I ran into a similar problem but was using the Stata engine (more info here). In this case it turned out that it wasn't a problem with knitr itself but with my Stata settings.
The trick was to add a Stata-specific block after the initial setup block that sets the line width.
```{r setup_knitr, echo=FALSE, message=FALSE}
# This is the usual setup block needed to set up the Stata Engine
require(knitr)
statapath <- "C:/Program Files (x86)/Stata13/Stata-64.exe"
opts_chunk$set(engine="stata", engine.path=statapath, comment="")
```
```{r setup_stata, echo=FALSE,message=FALSE,collectcode=TRUE}
* Now that Stata engine is set up, this block sets up Stata options
set linesize 200
set more off
```
来源:https://stackoverflow.com/questions/36845178/width-of-r-code-chunk-output-in-rmarkdown-files-knitr-ed-to-html