Debian Codegraph @

R/draft.R

raw
1 #' Create a new document based on a template
2 #'
3 #' Create (and optionally edit) a draft of an R Markdown document based on a
4 #' template.
5 #'
6 #' The \code{draft} function creates new R Markdown documents based on
7 #' templates that are either located on the filesystem or within an R package.
8 #' The template and its supporting files will be copied to the location
9 #' specified by \code{file}.
10 #' @param file File name for the draft
11 #' @param template Template to use as the basis for the draft. This is either
12 #' the full path to a template directory or the name of a template directory
13 #' within the \code{rmarkdown/templates} directory of a package.
14 #' @param package (Optional) Name of package where the template is located.
15 #' @param create_dir \code{TRUE} to create a new directory for the document
16 #' (the "default" setting leaves this behavior up to the creator of the
17 #' template).
18 #' @param edit \code{TRUE} to edit the template immediately
19 #' @return The file name of the new document (invisibly).
20 #' @note An R Markdown template consists of a directory that contains a
21 #' description of the template, a skeleton Rmd file used as the basis for new
22 #' documents, and optionally additional supporting files that are provided
23 #' along with the skeleton (e.g. a logo graphic).
24 #'
25 #' If the template directory is contained within a package then it should be
26 #' located at \code{inst/rmarkdown/templates}. For example, a package named
27 #' \pkg{pubtools} that wanted to provide a template named
28 #' \code{quarterly_report} would need to provide the following files within
29 #' the \code{pubtools/inst/rmarkdown/templates} directory:
30 #'
31 #' \code{quarterly_report/template.yaml} \cr
32 #' \code{quarterly_report/skeleton/skeleton.Rmd} \cr
33 #'
34 #' The \code{template.yaml} file should include a \code{name} field. If you
35 #' want to ensure that a new directory is always created for a given template,
36 #' then you can add the \code{create_dir} field to the \code{template.yaml}
37 #' file. For example:
38 #'
39 #' \code{create_dir: true} \cr
40 #'
41 #' The \code{skeleton/skeleton.Rmd} file should include the initial contents
42 #' you want for files created from this template. Additional files can be
43 #' added to the \code{skeleton} directory, for example:
44 #'
45 #' \code{skeleton/logo.png} \cr
46 #'
47 #' These files will automatically be copied to the directory containing the
48 #' new R Markdown draft.
49 #' @examples
50 #' \dontrun{
51 #' rmarkdown::draft("Q4Report.Rmd",
52 #' template="/opt/rmd/templates/quarterly_report")
53 #'
54 #' rmarkdown::draft("Q4Report.Rmd",
55 #' template="quarterly_report", package="pubtools")
56 #' }
57 #' @export
58 draft <- function(file,
59 template,
60 package = NULL,
61 create_dir = "default",
62 edit = TRUE) {
63
64 # resolve package file
65 if (!is.null(package)) {
66 template_path = pkg_file("rmarkdown", "templates", template,
67 package = package)
68 if (!nzchar(template_path)) {
69 stop("The template '", template, "' was not found in the ",
70 package, " package")
71 }
72 } else {
73 template_path <- template
74 }
75
76 # read the template.yaml and confirm it has the right fields
77 template_yaml <- file.path(template_path, "template.yaml")
78 if (!file.exists(template_yaml)) {
79 stop("No template.yaml file found for template '", template, "'")
80 }
81
82 template_meta <- yaml_load_file(template_yaml)
83 if (is.null(template_meta$name) || is.null(template_meta$description)) {
84 stop("template.yaml must contain name and description fields")
85 }
86
87 # see if this template is asking to create a new directory
88 if (identical(create_dir, "default"))
89 create_dir <- isTRUE(template_meta$create_dir)
90
91 # create a new directory if requested
92 if (create_dir) {
93
94 # remove .Rmd extension if necessary
95 file <- xfun::sans_ext(file)
96
97 # create dir (new dir only)
98 if (dir_exists(file))
99 stop("The directory '", file, "' already exists.")
100 dir.create(file)
101
102 # reconstitute the file path
103 file <- file.path(file, basename(file))
104 }
105
106 # Ensure we have an Rmd extension
107 if (!identical(tolower(xfun::file_ext(file)), "rmd"))
108 file <- paste(file, ".Rmd", sep = "")
109
110 # Ensure the file doesn't already exist
111 if (file.exists(file))
112 stop("The file '", file, "' already exists.")
113
114 # copy all of the files in the skeleton directory
115 skeleton_files <- list.files(file.path(template_path, "skeleton"),
116 full.names = TRUE)
117 to <- dirname(file)
118 for (f in skeleton_files) {
119 if (file.exists(file.path(to, basename(f))))
120 stop("The file '", basename(f), "' already exists")
121 file.copy(from = f, to = to, overwrite = FALSE, recursive = TRUE)
122 }
123
124 # rename the core template file
125 file.rename(file.path(dirname(file), "skeleton.Rmd"), file)
126
127 # invoke the editor if requested
128 if (edit) utils::file.edit(file)
129
130 # return the name of the file created
131 invisible(file)
132 }
133
134 #' List available R Markdown template in a package
135 #'
136 #' List the available template by name that can be used with [draft()] to create
137 #' a new document for R Markdown from a package.
138 #'
139 #' @param package Package to list template from. Default to **rmarkdown**
140 #' @param full_path Set to `TRUE` to get the full path to the available templates
141 #'
142 #' @return A character vector of templates name to use within [draft()]. If
143 #' `full_path = TRUE`, it returns the full path to the templates.
144 #' @export
145 #' @md
146 #' @examples
147 #' # List rmarkdown templates & create a draft
148 #' available_templates()
149 #'
150 #' # List rticles templates
151 #' available_templates("rticles")
152 available_templates <- function(package = "rmarkdown", full_path = FALSE) {
153 template_folder <- pkg_file("rmarkdown", "templates", package = package)
154 if (!dir_exists(template_folder)) return(invisible(character()))
155 templates <- list.files(template_folder, full.names = TRUE)
156 # exclude file in folder if any
157 templates <- templates[utils::file_test('-d', templates)]
158 if (full_path) return(templates)
159 basename(templates)
160 }