Note that there are some explanatory texts on larger screens.

plurals
  1. POIn knitr, figure heights do not obey my opts_chunk$set commands when sizes are changed on the fly
    primarykey
    data
    text
    <p>I am trying to create HTML files using R package knitr but I am finding the entire experience quite hard, possibly because the documentation is sparse when not using LaTeX (and when one does not know Sweave). The specific problem is this:</p> <p>I wish to produce two similar charts in the HTML file, but each must have a different height. So here is my RHTML file:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;TEST&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;!--begin.rcode opts_chunk$set(echo=FALSE, dpi=96, fig.width=9, fig.height=8, dev="png") print("Width 9, height 8") plot(rnorm(100), col="orange") end.rcode--&gt; &lt;hr&gt; &lt;!--begin.rcode opts_chunk$set(echo=FALSE, dpi=96, fig.width=9, fig.height=12, dev="png") print("Width 9, height 12") plot(rnorm(100), col="purple") end.rcode--&gt; &lt;hr&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>As you can see the two (simple) plots must have the same width (9) but different heights (8 then 12). This is trivially easy in R, but knitr seems to want to keep the aspect ratio the same. Here are the two plot outputs:</p> <p><img src="https://i.stack.imgur.com/Dn5Yp.png" alt="enter image description here"></p> <p><img src="https://i.stack.imgur.com/h9eVb.png" alt="enter image description here"></p> <p>As you can see, the lower one does not have the aspect ratio that I want, and they are different widths. </p> <p>Also, I am getting all this chunk output at the top of my html file that I thought I was suppressing with echo=FALSE....</p> <p><img src="https://i.stack.imgur.com/mN75f.png" alt="enter image description here"></p> <p>How might I fix all of this? Does opts_chunk$set set some kind of global option only once? What if I want to reset options each time I output a new graph? And how do I suppress <em>all</em> knitr package output?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COJust a suggestion on troubleshooting these things generally: make sure `cache = FALSE` and throw away the figures and cache subfolders which ensures they all get updated. Be sure that as many options are set in the `setup` chunk as possible. Then, only modify chunk options in the chunk where they change. Also, I always put `rm(list = ls())` in my set up chunk to make certain parameters written into the global environment don't hang around. Also, the typesetting produces intermediate files - throw those away too to force fresh copies. Finally, you might look at function `spin` in `knitr`.
      singulars
    2. COOkay just tried all that. rm(list = ls()); erased all the pngs in the figure directory, reloaded the browser, cleared its cache; no joy. Still the same. As I am not actually typesetting (it's just HTML), other than the "figure" directory, what else needs to be cleared that you know of? By the way, what is the "setup chunk"?
      singulars
    3. COIt's nothing formal, but many people use the first code chunk to set up both `knitr` and `R` options & settings. It would have `opts_chunk` and `library("whatever")` for example. Also, if you are relying on global variables you missing the virtues of reproducible research. If at all possible make your script generate or at least get these values from a `csv` or `RData` or something similar.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload