Width of R code chunk output in RMarkdown files knitr-ed to html

前端 未结 3 2073
忘了有多久
忘了有多久 2020-12-30 05:39

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

相关标签:
3条回答
  • 2020-12-30 05:43

    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>
    

    0 讨论(0)
  • 2020-12-30 05:47

    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
    ```
    
    0 讨论(0)
  • 2020-12-30 05:54

    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
    ```
    

    0 讨论(0)
提交回复
热议问题