I Need Help With a Template I Created

I created a template at on my GitHub at GitHub - Amarakon55/amaRyaml: Amarakon's personal YAML for R Markdown. The package is called 'amaRyaml' and the template is called 'pdf'. How do I actually use this template? I tried this:

output: amaRyaml::pdf

But it didn't work. It said:

Error: 'pdf' is not an exported object from 'namespace:amaRyaml'
Execution halted

shell returned 1

How do I fix this?

Hi @Amarakon,

You have created an RStudio template (for an R Markdown file, see Chapter 17 Document Templates | R Markdown: The Definitive Guide), this is not the same as a new output format which is specified in the YAML front-matter as output: mypkg::my_cool_format. Output formats are exported R functions that return an object with the class "rmarkdown_output_format".

If your RStudio template is working correctly, you should be able to open RStudio and then go to File > New File > R Markdown... > From Template, and then find your template and choose "OK". This should open your R Markdown template and you should be good.

Since you have so much custom configuration in your YAML front matter of amaRyaml/skeleton.Rmd at master · Amarakon55/amaRyaml · GitHub you may consider actually creating a new output format that hides these technical details inside a custom TeX template.

See Chapter 18 Creating New Formats | R Markdown: The Definitive Guide for more details.

Hello. I don't use the RStudio IDE, I actually edit my files in VIM. So I basically can't use RStudio to use my template. I want to explore the second option of creating a new output format. I looked at this:

It says to write a function and provides an example with a code block. However, I'm not sure on which file should I put that code for the function.

Within your R package and within the R sub-directory, you should create a script that contains an R function for your new output format. I think it makes sense to build your custom format on top of bookdown::pdf_document2() since you use this in your YAML frontmatter. So you will create your own function that modifies this base format in various ways to achieve your desired specification.

# amaRyaml/R/pdf_custom.R

#' My Custom Format
#'
#' @param ...
#'
#' @export

# Accept whatever configurable arguments you want...
pdf_custom <- function() {
  # Set up other custom format options...
  
  # Define the base format you will build upon
  base <- bookdown::pdf_document2(
    number_sections = TRUE,
    toc_depth = 3,
    latex_engine = 'xelatex',
    # pass in whatever other arguments you want...
  )
  base
}

You may wish to look at the custom formats in a package like {rticles} (rticles/R at main · rstudio/rticles · GitHub) which defines many PDF based formats.

1 Like

Can I also include options that aren't for the output? Like the author, documentclass, classoption, etc. I want those to be in the file too.

@Amarakon there is two types of template:

  1. Those that are only skeleton Rmd files with contains default configuration and some resources to use with it, and that will use any existing rmarkdown format. The skeleton will usually contain some YAML configuration and use asoutput_format an existing R function for a package. A lot of things can be customize this way already.

  2. Template that requires a custom format. Usually, this is required when some advanced processing are needed (pre-processor / post-processor), or to simplify the usage of the template as the custom format will be able to set different default value for an existing format without the user having to specify those values in YAML.

Often when 2. is done, there is also a template of type 1. to show how it works.

Regarding distribution, in case 1. this could live in a repo to clone / download or in R Package. For case 2, it needs to be in a R package so that it can be called during rendered

output: mycustom::format

I don't know exactly which one suits you best. It seems from your repository that the type 1. is ok. You have a skeleton and some resources, you could bundle that in a package and if you don't use RStudio, the function rmarkdown::draft() will allow to create new document based on that template . 16.8 R Markdown templates in R packages | R Markdown Cookbook

Hope it helps

1 Like

I don't want to use rmarkdown::draft(), so I created my own function. You can check my GitHub again: GitHub - Amarakon55/amaRyaml: Amarakon's personal YAML for R Markdown

I have the inst directory which has the necessary files including skeleton.Rmd. Also I wasn't sure if I should have set create_dir to true or false but I went with true.

I also have an R directory that defines the functions. Here is an example document:

---
title: Example
output: amaRyaml::eisvogel
---

Lorem ipsum.

It actually works, but I just have one problem: It's not reading the skeleton.Rmd file! The eisvogel function works, but this specific part in eisvogel.R is not working for some reason:

  pdf_document_format("eisvogel",
    number_sections = number_sections, toc_depth = toc_depth, latex_engine = latex_engine, ...
  )

Your example document does not look like your template skeleton.Rmd. You may not have correctly understood how template works. They are Rmd document stored in a package that could be retrieved as template (model / example) to build on too for creating a new Rmd document.

A skeleton.Rmd won't be used in the process of rendering another Rmd document. Maybe a template is not what you need for what you want to do. Possibly a custom output format is enough to set some knitr option, and pass some Pandoc variable that you may need.

With a skeleton, user of your package will be able to retrieve the included document easily so that they can have the YAML and some content already there as template in the new Rmd they will be creating.

rmarkdown::draft() is used to create a new document by a user based on a template. I don't see the equivalent function in your R code. But I may have missed it. Which function is that ?

The template won't be read by your function. As I am saying above, it is stored inside the package so that a use can easily create a new document based on that.

Have you look at the doc ? We can clarify things if this is not. Some more resources if you do not have found it yet

Create a new document based on a template — draft • rmarkdown & R Markdown Document Templates

1 Like

Okay, I now understand. I tried rmarkdown::draft("file.Rmd", template = "eisvogel") but it gave me this error:

Error in rmarkdown::draft(file.Rmd, template = eisvogel) :
  object 'eisvogel' not found
Execution halted

Then I tried rmarkdown::draft("file.Rmd", template = "eisvogel", package = "amaRyaml") and it gave me a similar error:

Error in rmarkdown::draft(file.Rmd, template = eisvogel, package = amaRyaml) :
  object 'amaRyaml' not found
Execution halted

The error seems pretty clear to me

It seems you are missing some quotes to pass string to rmarkdown::draft arguments. You are passing R variable name currently, and those objects are not defined. Hence object not found.

Try what you wrote here

Do you have similar error this way ?

Yes, it says "object amaRyaml not found". I also tried with the rticles package. Still the same issue. rmarkdown::draft() is not working even though I am providing it with all the information it needs according to this. This has to be a problem with the rmarkdown package itself or something on my end.

This error is generally thrown when you used amaRyaml instead of "amaRyaml". I explained that above. Did you really retried adding the quotes " around ?

rmarkdown is working fine I believe, for rticles

# create temp dir
dir.create(tmp_dir <- tempfile())
# Empty
list.files(tmp_dir)
#> character(0)
# Create template
xfun::in_dir(tmp_dir, {
  rmarkdown::draft("Test.Rmd", template = "acm", package = "rticles", edit = FALSE)
})
# It is there
list.files(tmp_dir, recursive = TRUE)
#> [1] "Test/acm-sig-proceedings.csl" "Test/acm_proc_article-sp.cls"
#> [3] "Test/Makefile"                "Test/README.md"              
#> [5] "Test/sensys-abstract.cls"     "Test/sigproc.bib"            
#> [7] "Test/Test.Rmd"

# clean example
unlink(tmp_dir, recursive = TRUE)

and same with your package

# create temp dir
dir.create(tmp_dir <- tempfile())
# Empty
list.files(tmp_dir)
#> character(0)
# install your package
remotes::install_github("Amarakon55/amaRyaml")
#> Using github PAT from envvar GITHUB_PAT
#> Skipping install of 'amaRyaml' from a github remote, the SHA1 (85894f69) has not changed since last install.
#>   Use `force = TRUE` to force installation
# create a template
xfun::in_dir(tmp_dir, {
  rmarkdown::draft("file.Rmd", template = "eisvogel", package = "amaRyaml", edit = FALSE)
})
# It is there
list.files(tmp_dir, recursive = TRUE)
#> [1] "file.Rmd"                            
#> [2] "fonts/LiberationSerif-Bold.ttf"      
#> [3] "fonts/LiberationSerif-BoldItalic.ttf"
#> [4] "fonts/LiberationSerif-Italic.ttf"    
#> [5] "fonts/LiberationSerif-Regular.ttf"

# clean example
unlink(tmp_dir, recursive = TRUE)

Hope it helps.

When I copied and pasted rmarkdown::draft("file.Rmd", template = "eisvogel", package = "amaRyaml", edit = FALSE), it actually worked. Don't know why it didn't work before. Maybe I forgot to wrap something in quotes. Thanks so much for helping me.

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.