【问题标题】:Unpredictable figure size in RMD chunk in code loopUnpredictable figure size in RMD chunk in code loop
【发布时间】:2022-12-02 04:11:21
【问题描述】:

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

【问题讨论】:

    标签: r-markdown


    【解决方案1】:

    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 thedefaultchunk 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 appraoch 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 <- exposure_plot
        resizeHeight <- 3.25*ceiling(data_segments/4)
        res <- knitr::knit_child('resizePlot.rmd', quiet = TRUE)
        cat(res, sep = '
    ')
    }
    

    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)
    

    【讨论】:

      猜你喜欢
      • 2011-09-26
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      • 2022-12-27
      • 2021-07-07
      • 2022-12-02
      • 2022-12-02
      • 2022-12-01
      相关资源
      最近更新 更多