Title: | Insert R Data into 'Word' Documents |
---|---|
Description: | Populate data from an R environment into '.doc' and '.docx' templates. Create a template document in a program such as 'Word', and add strings encased in guillemet characters to create flags («example»). Use getDictionary() to create a dictionary of flags and replacement values, then call docket() to generate a populated document. |
Authors: | Jonathan Conrad [aut, cre], Ian Conrad [ctb] |
Maintainer: | Jonathan Conrad <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.20 |
Built: | 2025-03-10 02:47:38 UTC |
Source: | https://github.com/jonathanconrad98/docket |
Scans the input template file for specified flags as defined in the dictionary, and replaces them with corresponding data. Repeats the process for each column, generating a new document for each column which is saved as the file name and path listed in row 1
batchDocket(filename, batchDictionary)
batchDocket(filename, batchDictionary)
filename |
The file path to the document template. Supports .doc and .docx formats |
batchDictionary |
A data frame where each row represents a flag to be replaced in the template document and each column represents a final document to be generated |
Generates new .doc or .docx files with the flags replaced by the specified data for that column
# Path to the sample template file included in the package template_path <- system.file("batch_document", "batchTemplate.docx", package="docket") output_paths <- as.list(paste0(dirname(template_path), paste0("/batch document", 1:5, ".docx"))) # Create a dictionary by using the getDictionary function on the sample template file result <- getBatchDictionary(template_path, output_paths) result[2,2:ncol(result)] <- Sys.getenv("USERNAME") #Author name result[3,2:ncol(result)] <- as.character(Sys.Date()) result[4,2:ncol(result)] <- 123 result[5,2:ncol(result)] <- 456 result[6,2:ncol(result)] <- 789 result[7,2:ncol(result)] <- sum(as.numeric(result[4:6,2])) # Verify that the result dictionary is valid if (checkBatchDictionary(result) == TRUE) { batchDocket(template_path, result) for (i in 1:length(output_paths)) { if (file.exists(output_paths[[i]])) { print(paste("docket", i, "Successfully Created")) } } }
# Path to the sample template file included in the package template_path <- system.file("batch_document", "batchTemplate.docx", package="docket") output_paths <- as.list(paste0(dirname(template_path), paste0("/batch document", 1:5, ".docx"))) # Create a dictionary by using the getDictionary function on the sample template file result <- getBatchDictionary(template_path, output_paths) result[2,2:ncol(result)] <- Sys.getenv("USERNAME") #Author name result[3,2:ncol(result)] <- as.character(Sys.Date()) result[4,2:ncol(result)] <- 123 result[5,2:ncol(result)] <- 456 result[6,2:ncol(result)] <- 789 result[7,2:ncol(result)] <- sum(as.numeric(result[4:6,2])) # Verify that the result dictionary is valid if (checkBatchDictionary(result) == TRUE) { batchDocket(template_path, result) for (i in 1:length(output_paths)) { if (file.exists(output_paths[[i]])) { print(paste("docket", i, "Successfully Created")) } } }
Validates that the input batch dictionary meets the following requirements: #' #'
1. It is a data frame
2. Column 1 is named "flag"
3. Column 1 contains flags with starting and ending wings: « »
4. Row 1 contains the file names and paths of the populated output documents
checkBatchDictionary(batchDictionary)
checkBatchDictionary(batchDictionary)
batchDictionary |
A data frame where each row represents a flag to be replaced in the template document and each column represents a final document to be generated |
Logical. Returns 'TRUE' if the batch dictionary meets requirements for processing. Returns 'FALSE' otherwise
# Path to the sample template file included in the package template_path <- system.file("batch_document", "batchTemplate.docx", package="docket") output_paths <- as.list(paste0(dirname(template_path), paste0("/batch document", 1:5, ".docx"))) # Create a dictionary by using the getDictionary function on the sample template file result <- getBatchDictionary(template_path, output_paths) result[2,2:ncol(result)] <- Sys.getenv("USERNAME") #Author name result[3,2:ncol(result)] <- as.character(Sys.Date()) result[4,2:ncol(result)] <- 123 result[5,2:ncol(result)] <- 456 result[6,2:ncol(result)] <- 789 result[7,2:ncol(result)] <- sum(as.numeric(result[4:6,2])) # Verify that the result dictionary is valid if (checkBatchDictionary(result) == TRUE) { print("Valid Batch Dictionary") }
# Path to the sample template file included in the package template_path <- system.file("batch_document", "batchTemplate.docx", package="docket") output_paths <- as.list(paste0(dirname(template_path), paste0("/batch document", 1:5, ".docx"))) # Create a dictionary by using the getDictionary function on the sample template file result <- getBatchDictionary(template_path, output_paths) result[2,2:ncol(result)] <- Sys.getenv("USERNAME") #Author name result[3,2:ncol(result)] <- as.character(Sys.Date()) result[4,2:ncol(result)] <- 123 result[5,2:ncol(result)] <- 456 result[6,2:ncol(result)] <- 789 result[7,2:ncol(result)] <- sum(as.numeric(result[4:6,2])) # Verify that the result dictionary is valid if (checkBatchDictionary(result) == TRUE) { print("Valid Batch Dictionary") }
Verifies that the input dictionary meets the following conditions #'
1. It is a two-column data frame
2. Column 1 is named "flag"
3. Column 1 contains flags with starting and ending wings: « »
checkDictionary(dictionary)
checkDictionary(dictionary)
dictionary |
A data frame where each row represents a flag in the template document and its replacement value |
Logical. Returns 'TRUE' if the dictionary meets requirements for processing. Returns 'FALSE' otherwise
# Path to the sample template included in the package template_path <- system.file("template_document", "Template.docx", package="docket") # Create a dictionary by using the getDictionary function on the sample template file result <- getDictionary(template_path) # Insert data into the template dictionary result[1,2] <- Sys.getenv("USERNAME") #Author name result[2,2] <- as.character(Sys.Date()) # Date report created result[3,2] <- 123 result[4,2] <- 456 result[5,2] <- 789 result[6,2] <- sum(as.numeric(result[3:5,2])) # Verify that the result dictionary is valid if (checkDictionary(result) == TRUE) { print("Valid Dictionary") }
# Path to the sample template included in the package template_path <- system.file("template_document", "Template.docx", package="docket") # Create a dictionary by using the getDictionary function on the sample template file result <- getDictionary(template_path) # Insert data into the template dictionary result[1,2] <- Sys.getenv("USERNAME") #Author name result[2,2] <- as.character(Sys.Date()) # Date report created result[3,2] <- 123 result[4,2] <- 456 result[5,2] <- 789 result[6,2] <- sum(as.numeric(result[3:5,2])) # Verify that the result dictionary is valid if (checkDictionary(result) == TRUE) { print("Valid Dictionary") }
Scans the input template file for specified flags as defined in the dictionary, and replaces them with corresponding data. The edited content is then saved to a new document
docket(filename, dictionary, outputName)
docket(filename, dictionary, outputName)
filename |
The file path to the document template |
dictionary |
A data frame where each row represents a flag in the template document and its replacement value |
outputName |
The file path and name for the saved output document |
Generates a new .doc or .docx file with the flags replaced by the specified data
# Path to the sample template included in the package template_path <- system.file("template_document", "Template.docx", package="docket") output_path <- paste0(dirname(template_path), "/output document.docx") # Create a dictionary by using the getDictionary function on the sample template file result <- getDictionary(template_path) # Insert data into the template dictionary result[1,2] <- Sys.getenv("USERNAME") #Author name result[2,2] <- as.character(Sys.Date()) # Date report created result[3,2] <- 123 result[4,2] <- 456 result[5,2] <- 789 result[6,2] <- sum(as.numeric(result[3:5,2])) # Verify that the result dictionary is valid if (checkDictionary(result) == TRUE) { docket(template_path, result, output_path) if (file.exists(output_path)) { print("Docket Successfully Created") } }
# Path to the sample template included in the package template_path <- system.file("template_document", "Template.docx", package="docket") output_path <- paste0(dirname(template_path), "/output document.docx") # Create a dictionary by using the getDictionary function on the sample template file result <- getDictionary(template_path) # Insert data into the template dictionary result[1,2] <- Sys.getenv("USERNAME") #Author name result[2,2] <- as.character(Sys.Date()) # Date report created result[3,2] <- 123 result[4,2] <- 456 result[5,2] <- 789 result[6,2] <- sum(as.numeric(result[3:5,2])) # Verify that the result dictionary is valid if (checkDictionary(result) == TRUE) { docket(template_path, result, output_path) if (file.exists(output_path)) { print("Docket Successfully Created") } }
Scans the input file for strings enclosed by flag wings: « ». Creates a replacement value column for each document to be generated
getBatchDictionary( filename, outputFiles, dictionaryLength = length(outputFiles) )
getBatchDictionary( filename, outputFiles, dictionaryLength = length(outputFiles) )
filename |
The file path to the document template |
outputFiles |
A list of the file names and paths for the populated templates |
dictionaryLength |
Number of columns in the batch dictionary. Defaults to the number of output files. Cannot be shorter than the count of 'outputFiles' |
Data frame for populating data into the template with row 1 containing the output file names
# Path to the sample template file included in the package template_path <- system.file("batch_document", "batchTemplate.docx", package="docket") output_paths <- as.list(paste0(dirname(template_path), paste0("/batch document", 1:5, ".docx"))) # Create a dictionary by using the getDictionary function on the sample template file result <- getBatchDictionary(template_path, output_paths) print(result)
# Path to the sample template file included in the package template_path <- system.file("batch_document", "batchTemplate.docx", package="docket") output_paths <- as.list(paste0(dirname(template_path), paste0("/batch document", 1:5, ".docx"))) # Create a dictionary by using the getDictionary function on the sample template file result <- getBatchDictionary(template_path, output_paths) print(result)
Scans the input file for strings enclosed by flag wings: « », then creates an empty dictionary with corresponding replacement values for each flag.
getDictionary(filename)
getDictionary(filename)
filename |
The file path to the document template. Supports .doc and .docx |
A data frame where each row represents a flag in the template document and its replacement value
# Path to the sample template file included in the package template_path <- system.file("template_document", "Template.docx", package="docket") # Create a dictionary by using the getDictionary function on the sample template file result <- getDictionary(template_path) print(result)
# Path to the sample template file included in the package template_path <- system.file("template_document", "Template.docx", package="docket") # Create a dictionary by using the getDictionary function on the sample template file result <- getDictionary(template_path) print(result)