Skip to content

Automating ordering of figures and tables #239

Description

@lisaailloud-NOAA

We have custom code in the SE to automate the ordering of figures and tables based on in-text citation. This feature would be nice to have in asar/stockplotr. Maybe you can adapt our approach to work with asar?

The figure number is generated in-text using the function add_figure (see code below) :
Figure r add_figure("FigInitSearch")

If you want to reference multiple figures but not be redundant ("Figures 3-5" instead of "Figures 3,4,5") you can use silent =T
Figures r add_figure("FigLS3New") r add_figure("FigLS4New", silent=T)-r add_figure("FigLS5New"))

The function reads from have an excel file (TablesAndFigures.xlsx) that lists all the tables available and their location (and other features like caption and size)

Image
add_figure <- function(name,setup_location="TablesAndFigures.xlsx",silent=FALSE){
  #First check to see if the figure counter exists. 
  #If it does increment by 1. Otherwise create the counter
  if(exists("MyFigs")){
    if(!'included' %in% names(MyFigs)){
      MyFigs['included'] <<- rep(0,length(MyFigs[,1]))
    }
  }else{
    MyFigs <<- as.data.frame(read_excel(setup_location,sheet="Figures")) 
    #MyFigs <<- read.csv(file=setup_location)
    MyFigs[,'included'] <<- rep(0,length(MyFigs[,1]))
  }
  
  fig_sub <- MyFigs[MyFigs[,'Number']==MyFigs[MyFigs[,'Call']==name,'Number'][1],,drop=FALSE]
  
  if(length(fig_sub[,1])==0){
    stop(paste0("Error: you referenced a figure call ( ",name," ) that doesn't exist in you figure setup data frame."))
  }else{
    if(fig_sub[,'included'][1]==0){
      if(exists("fig_count")){
        fig_count <<- fig_count+1
      }else{
        fig_count <<- 1
      }
      
      if(fig_sub[,'Number'][1]!=fig_count){
        MyFigs[MyFigs[,'Number']==fig_count,'Number'] <<- rep((max(as.numeric(MyFigs[,'Number']))+1),length(MyFigs[MyFigs[,'Number']==fig_count,'Number']))
        MyFigs[MyFigs[,'Number']==fig_sub[,'Number'][1],'included'] <<- rep(1,length(fig_sub[,1]))
        MyFigs[MyFigs[,'Number']==fig_sub[,'Number'][1],'Number'] <<- rep(fig_count,length(fig_sub[,1]))
      }else{
        MyFigs[MyFigs[,'Number']==fig_sub[,'Number'][1],'included'] <<- rep(1,length(fig_sub[,1]))
      }
      
      fig_num <- fig_count
    }else{
      fig_num <- fig_sub[,'Number'][1]
    }
  }
  
  
  if(silent==TRUE){
    return("")
  }else{
    return(fig_num)
  }
}


We also have custom code to specify the figure layout on the page (e.g. if we want two figures side by side or one above the other). Happy to share that as well if it's of interest.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions