代码之家  ›  专栏  ›  技术社区  ›  S.M.

从.RMD中的代码块中删除空白(将knitr编译为Beamer pdf)

  •  2
  • S.M.  · 技术社区  · 9 年前

    ---
    title: "Untitled"
    author: "Math 35"
    date: "October 14, 2016"
    output: beamer_presentation
    ---
    
    ```{r setup, echo=FALSE, include=FALSE}
    knitr::knit_hooks$set(mysize = function(before, options, envir) {
      if (before) 
        return(options$size)
    })
    knitr::opts_chunk$set(size='\\small')
    knitr::opts_chunk$set(warning=FALSE)
    knitr::opts_chunk$set(message=FALSE)
    knitr::opts_chunk$set(fig.align='center')
    ```
    
    ## Recap from Last Time: Continuous Random Variables
    -- Uniform Random Variable  $X\sim  U(a,b)$
    $$f_X(x) = \frac{1}{b-a}, a \leq x \leq b$$
    $$E[X] = \frac{a+b}{2}$$
    $$Var(X) = \frac{(b-a)^2}{12}$$
    ```{r, echo=FALSE, fig.height=3, fig.width=3.5}
    density <- dunif(x=seq(from=0, to=6, by=0.01), min=1, max=5)
    plot(seq(from=0, to=6, by=0.01), density, col="black", type="l", ylim=c(0, 0.5), lwd=4,  xlab="X ~ U(1,5)")
      lines(c(3,3), c(0,dunif(3, min=1, max=5)), col="red", lwd=2)
      text(2.9, 0.1, "E[X]=(1+5)/2 = 3", col="purple")
    ```
    

    当我在R Studio中“编织PDF”时,生成的幻灯片在文本和图之间有很大的空白。因此,图不适合幻灯片。我想删除此空白,以便所有内容都可以放在幻灯片上。

    以下是我尝试过的所有代码块选项,但都不起作用:

    • 带白色=真

    • 整洁=真实

    • 图保持=“高”

    • fig.keep='last'

    • 突出显示='false'

    我还研究了:

    • http://yihui.name/knitr/demo/output/ 这是指 https://github.com/yihui/knitr/issues/231 但是,由于这在一个我不熟悉的.Rnw文件中解决了这个问题,所以我无法让这个解决方案工作。我尝试将建议的标题放在.Rmd文件的顶部,但“Knit PDF”并没有完成编译。我可能做错了。

    • 我已经阅读了R Markdown参考指南,在那里我找到了我上面尝试的各种选项。

    我花了两个小时试图找出如何让一个数字在一张幻灯片上正确显示。任何帮助都将不胜感激。

    2 回复  |  直到 9 年前
        1
  •  1
  •   baptiste    9 年前

    你可以玩 par() 但部分问题来自乳胶方面。使用 \begin{center}\end{center}

    \centering
    ```{r, echo=FALSE, fig.height=1.5, fig.width=3.5}
    density <- dunif(x=seq(from=0, to=6, by=0.01), min=1, max=5)
    par(mar=c(2.5,2.5,0.5,0.5), mgp=c(1.5, 0.5, 0), bg="grey95")
    plot(seq(from=0, to=6, by=0.01), density, col="black", type="l", ylim=c(0, 0.5), lwd=4,  xlab="X ~ U(1,5)")
      lines(c(3,3), c(0,dunif(3, min=1, max=5)), col="red", lwd=2)
      text(2.9, 0.1, "E[X]=(1+5)/2 = 3", col="purple")
      ```
    

    enter image description here

        2
  •  1
  •   S.M.    9 年前

    感谢所有回应的人。我设置了代码块选项 out.width='65%' 这起了作用(同时改变yaxis以从0扩展到0.3而不是0到0.5):

    ---
    title: "Lecture 5 - The Normal Distribution"
    output:
      beamer_presentation: 
        highlight: null
    ---
    ```{r setup, echo=FALSE, include=FALSE}
    knitr::knit_hooks$set(mysize = function(before, options, envir) {
      if (before) 
        return(options$size)
    })
    knitr::opts_chunk$set(size='\\small')
    knitr::opts_chunk$set(echo=TRUE)
    knitr::opts_chunk$set(warning=FALSE)
    knitr::opts_chunk$set(message=FALSE)
    knitr::opts_chunk$set(fig.align='center')
    ```
    ## Recap from Last Time: Continuous Random Variables
    -- Uniform Random Variable  $X\sim  U(a,b)$
    $$f_X(x) = \frac{1}{b-a}, a \leq x \leq b$$
    $$E[X] = \frac{a+b}{2}$$
    $$Var(X) = \frac{(b-a)^2}{12}$$
    
    ```{r, echo=FALSE,out.width='65%'}
    density <- dunif(x=seq(from=0, to=6, by=0.01), min=1, max=5)
    plot(seq(from=0, to=6, by=0.01), density, col="black", type="l", ylim=c(0, 0.3), lwd=4,  xlab="X ~ U(1,5)")
      lines(c(3,3), c(0,dunif(3, min=1, max=5)), col="red", lwd=2)
      text(2.9, 0.1, "E[X]=(1+5)/2 = 3", col="purple")
    ```
    
    推荐文章