最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Get figure and table caption numbering to match sub-section numbering using bookdown - Stack Overflow

programmeradmin0浏览0评论

I'm trying to generate a word doc report from tabular data using nested for() loops. The loops call child docs to render tables and images with appropriately numbered figure captions. After overcoming an initial lack of numbering at all (underscores were the culprit), my code does now generate figure captions for tables and images.

However, the figure captions only match the highest level of the section numbering. For example, in section 3.0, the figures and tables are numbered sequentially from Fig 3.1, 3.2, 3.3, etc. But each of these figures might be in sub-section 3.1.1 or 3.2.3 still retaining a numbering of 3.2 or 3.3 rather than Fig 3.1.1.1 or Fig 3.2.3.1.

Is there a way to get my figure and table captions to match the sub-section numbering?

Is this a limitation of bookdown? Is this a limitation of generating a report in word? I'd like to keep using word because the report needs to be editable by colleagues after generation. Is this an issue that can be fixed in the YAML header with some options?

I definitely hold my hands up as being new to bookdown and by no means an expert in R Markdown.

I've set up a github repo so that folks can access all the code:

My YAML header looks like this:

---
title: "Figure and Table Numbering Figuring"
author: "darwilliams"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output:
  bookdown::word_document2:
    toc: true
    number_sections: yes
    fig_caption: yes
editor_options:
  chunk_output_type: console
---

And my nested for() loops look like this (the test data structure makes sense, even if the ouput is silly):

#input data ----
test_data <- as.data.frame(HairEyeColor)

#nest data
test_nest <- test_data %>% 
  group_by(Sex) %>% 
  nest()

# Get image to use in loop ----
url <- ".png"
destfile <- file.path(getwd(),"R.png")

curl_download(url, destfile)

img <- "R.png"


# nested for() loops with child doc calls (see github link for child docs) ----

for(i in 1:length(test_nest$Sex)) {
  
  # This loop sets up the First Group: Sex of participants
  
  # print(paste0("i = ",i))
  
  current <- test_nest[i, ]
  
  # print(current$data)
  
  # set-up data for next loop ----
  
  # get data from current in tibble format
  current2 <- as.data.frame(current$data)
  
  # nest current2 data by Hair for use in next loop
  current3 <- current2 %>% 
    group_by(Hair) %>% 
    nest()
  
  # set up table ----
  
  ft <- current2 %>% 
    flextable()
    
  ft <- set_table_properties(ft, width = .5, layout = "autofit")
  
  ft <- set_caption(ft, paste0("Frequencies of hair and eye colour for ",current$Sex," study participants"),
                    autonum = run_autonum(seq_id = "tab", pre_label = "Table", bkm = i))
  
  # ft
  
  # use knit_child() to generate Sex of participants section ----
  child1 <- knitr::knit_child('ra-report-child-01.Rmd', quiet = TRUE)
  
  cat(child1, sep = '\n')
  
  for (j in 1:length(current3$data)){
    
    # This loop sets up the next sub-header section: Hair
    
    # print(paste0("j = ",j))
    
    # get row numbers of current3
    rownums <- 1:length(current3$Hair)
    
    # use child2 to generate Hair section ----
    child2 <- knitr::knit_child('ra-report-child-02.Rmd', quiet = TRUE)
    
    cat(child2, sep = '\n')

    
    # set-up data for next loop ----
    
    # get data from current3 in tibble format
    current4 <- as.data.frame(current3$data[j])
    
    # nest current4 data by Eye colour for use in next loop
    current5 <- current4 %>% 
      group_by(Eye) %>% 
      nest()
    
    for(k in 1:length(current5$Eye)){
      
      # This loop sets up Eye Colour
      
      # print(paste0("k = ", k))
      
      rownums2 <- 1:length(current5$Eye)
      
      # use child 3 to generate eye colour section ----
      
      child3 <- knitr::knit_child('ra-report-child-03.Rmd', quiet = TRUE,options = c(j,k))
       
      cat(child3, sep = '\n')
      
      current6 <- current5$data[k]
      
      # use child4 to print eye colour table ----
      child4 <- knitr::knit_child('ra-report-child-04.Rmd', quiet = TRUE)

      cat(child4, sep = '/n')
    }
  }
}

I'm sure my code could be more efficient, but I don't really care about that - the main thing is for the figure and table captions to match the sub-section numbering.

Thanks!

发布评论

评论列表(0)

  1. 暂无评论