diff --git a/DESCRIPTION b/DESCRIPTION index 8b147ca8..b0eda1bb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: officer Type: Package Title: Manipulation of Microsoft Word and PowerPoint Documents -Version: 0.4.5.012 +Version: 0.4.5.013 Authors@R: c( person("David", "Gohel", role = c("aut", "cre"), email = "david.gohel@ardata.fr"), diff --git a/R/custom_properties.R b/R/custom_properties.R index 19fbb867..14f060aa 100644 --- a/R/custom_properties.R +++ b/R/custom_properties.R @@ -1,28 +1,34 @@ #' @importFrom xml2 xml_children -read_custom_properties <- function( package_dir ){ +read_custom_properties <- function(package_dir) { filename <- file.path(package_dir, "docProps/custom.xml") - if( !file.exists(filename) ) { + if (!file.exists(filename)) { filename <- system.file(package = "officer", "template/custom.xml") } doc <- read_xml(filename) all_children <- xml_children(doc) + pid_values <- vapply(all_children, xml_attr, NA_character_, "pid") name_values <- vapply(all_children, xml_attr, NA_character_, "name") - value_values <- vapply(all_children, function(x) {as.character(xml_child(x, 1))}, NA_character_) + value_values <- vapply(all_children, function(x) { + as.character(xml_child(x, 1)) + }, NA_character_) + value_values <- gsub("", "", value_values, fixed = TRUE) value_values <- gsub("", "", value_values, fixed = TRUE) value_values <- gsub("", "", value_values, fixed = TRUE) str <- c(pid_values, name_values, value_values) - z <- matrix(str, ncol = 3, - dimnames = list(NULL, c("pid", "name", "value"))) + z <- matrix(str, + ncol = 3, + dimnames = list(NULL, c("pid", "name", "value")) + ) z <- list(data = z) class(z) <- "custom_properties" z } -`[<-.custom_properties` <- function( x, i, j, value ){ - if( !i %in% x$data[,"name"] ) { - if(nrow(x$data)<1) { +`[<-.custom_properties` <- function(x, i, j, value) { + if (!i %in% x$data[, "name"]) { + if (nrow(x$data) < 1) { pid <- 2 } else { pid <- max(as.integer(x$data[, "pid"])) + 1L @@ -30,37 +36,40 @@ read_custom_properties <- function( package_dir ){ new <- matrix(c(as.character(pid), i, value), ncol = 3) x$data <- rbind(x$data, new) } else { - x$data[x$data[,"name"] %in% i, j] <- value + x$data[x$data[, "name"] %in% i, j] <- value } x } -`[.custom_properties` <- function( x, i, j ){ - if(nrow(x$data) < 1) { +`[.custom_properties` <- function(x, i, j) { + if (nrow(x$data) < 1) { return(character()) } - if(missing(i)) { + if (missing(i)) { x$data[, j] } else { - x$data[x$data[,"name"] %in% i, j] + x$data[x$data[, "name"] %in% i, j] } } -write_custom_properties <- function(custom_props, package_dir){ +write_custom_properties <- function(custom_props, package_dir) { xml_props <- sprintf( "%s", - custom_props$data[,1], - custom_props$data[,2], - custom_props$data[,3]) + custom_props$data[, 1], + custom_props$data[, 2], + custom_props$data[, 3] + ) - xml_ <- c("", - "", - xml_props, - "") - props_dir = file.path(package_dir, "docProps") + xml_ <- c( + "", + "", + xml_props, + "" + ) + props_dir <- file.path(package_dir, "docProps") dir.create(props_dir, recursive = TRUE, showWarnings = FALSE) filename <- file.path(props_dir, "custom.xml") - writeLines(enc2utf8(xml_), filename, useBytes=TRUE) + writeLines(enc2utf8(xml_), filename, useBytes = TRUE) invisible() }