Does clicking the knit button expose the filename to output function call?

I am currently working on writing a package that provides a new TeX template for writing assignments. I have created a new output format that I include in the YAML header as:

---
output: pdf_assignment
---

This new output format is essentially a wrapper around bookdown::pdf_document2 which uses this new TeX template.

pdf_assignment <- function(...){
  
  base <- bookdown::pdf_document2(
    template = find_resource('assignment', file = 'template.tex'),
    highlight = 'pygments',
    df_print = 'tibble',
    pandoc_args = c('--metadata', 'link-citations=true',
                    paste0('--csl=', find_resource('assignment', 'vancouver.csl'))),
    ...
  )
  
  base
  
}

However, I want to be able to run a function before actually rendering the document. I want to use Ben Marwick's wordcountaddin::word_count() function to check the word count and pass it to the TeX template so it can be included in the document. For example:

pdf_assignment <- function(...){
  
  wc <- wordcountaddin::word_count(file)
  
  base <- bookdown::pdf_document2(
    template = find_resource('assignment', file = 'template.tex'),
    highlight = 'pygments',
    df_print = 'tibble',
    pandoc_args = c('--metadata', 'link-citations=true',
                    paste0('--csl=', 
                           find_resource('assignment', 'vancouver.csl')),
                    '-V', paste0('word_count=', word_count)),
    ...
  )
  
  base
  
}

However, I need the path to the R Markdown file in order to run the word count. Is this filename somehow exposed to my pdf_assignment function when I "click" knit in RStudio?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.