Skip to content

Commit ae998af

Browse files
committed
docs: move other examples to inst/examples
1 parent 787ca0a commit ae998af

31 files changed

+307
-215
lines changed

R/docx_comments.R

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,7 @@
1717
#' * "commented_text" - a list column of characters containing the
1818
#' commented text. Elements can be vectors of length > 1 if a comment
1919
#' spans multiple paragraphs or runs or of length 0 if the commented text is empty.
20-
#' @examples
21-
#' bl <- block_list(
22-
#' fpar("Comment multiple words."),
23-
#' fpar("Second line")
24-
#' )
25-
#'
26-
#' a_par <- fpar(
27-
#' "This paragraph contains",
28-
#' run_comment(
29-
#' cmt = bl,
30-
#' run = ftext("a comment."),
31-
#' author = "Author Me",
32-
#' date = "2023-06-01"
33-
#' )
34-
#' )
35-
#'
36-
#' doc <- read_docx()
37-
#' doc <- body_add_fpar(doc, value = a_par, style = "Normal")
38-
#'
39-
#' docx_file <- print(doc, target = tempfile(fileext = ".docx"))
40-
#'
41-
#' docx_comments(read_docx(docx_file))
20+
#' @example inst/examples/example_docx_comments.R
4221
#' @export
4322
docx_comments <- function(x) {
4423
stopifnot(inherits(x, "rdocx"))

R/docx_replace.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ footers_replace_img_at_bkm <- function(x, bookmark, value) {
191191
#' i.e.the bookmark will be lost as a side effect.
192192
#' @inheritParams body_add_gg
193193
#'
194-
#' @example inst/examplesexample_body_replace_gg_at_bkm.R
194+
#' @example inst/examples/example_body_replace_gg_at_bkm.R
195195
body_replace_gg_at_bkm <- function(
196196
x,
197197
bookmark,

R/docx_settings.R

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,7 @@
3737
#' feature in 'Microsoft Word'.
3838
#' @param auto_hyphenation whether to enable auto hyphenation, default is FALSE.
3939
#' @param unit unit for `default_tab_stop` and `hyphenation_zone`, one of "in", "cm", "mm".
40-
#' @examples
41-
#' txt_lorem <- rep(
42-
#' "Purus lectus eros metus turpis mattis platea praesent sed. ",
43-
#' 50
44-
#' )
45-
#' txt_lorem <- paste0(txt_lorem, collapse = "")
46-
#'
47-
#' header_first <- block_list(fpar(ftext("text for first page header")))
48-
#' header_even <- block_list(fpar(ftext("text for even page header")))
49-
#' header_default <- block_list(fpar(ftext("text for default page header")))
50-
#' footer_first <- block_list(fpar(ftext("text for first page footer")))
51-
#' footer_even <- block_list(fpar(ftext("text for even page footer")))
52-
#' footer_default <- block_list(fpar(ftext("text for default page footer")))
53-
#'
54-
#' ps <- prop_section(
55-
#' header_default = header_default, footer_default = footer_default,
56-
#' header_first = header_first, footer_first = footer_first,
57-
#' header_even = header_even, footer_even = footer_even
58-
#' )
59-
#'
60-
#' x <- read_docx()
61-
#'
62-
#' x <- docx_set_settings(
63-
#' x = x,
64-
#' zoom = 2,
65-
#' list_separator = ",",
66-
#' even_and_odd_headers = TRUE
67-
#' )
68-
#'
69-
#' for (i in 1:20) {
70-
#' x <- body_add_par(x, value = txt_lorem)
71-
#' }
72-
#' x <- body_set_default_section(
73-
#' x,
74-
#' value = ps
75-
#' )
76-
#' print(x, target = tempfile(fileext = ".docx"))
40+
#' @example inst/examples/example_docx_set_settings.R
7741
docx_set_settings <- function(
7842
x,
7943
zoom = 1,

R/fortify_docx.R

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -766,16 +766,7 @@ summarise_as_paragraph <- function(data, preserve = FALSE) {
766766
#' - `align`: Paragraph alignment (character).
767767
#' - `level`: Numbering level (integer). `NA` if not a numbered list.
768768
#' - `num_id`: Numbering definition ID (integer). `NA` if not a numbered list.
769-
#' @examples
770-
#' example_docx <- system.file(
771-
#' package = "officer",
772-
#' "doc_examples/example.docx"
773-
#' )
774-
#' doc <- read_docx(example_docx)
775-
#'
776-
#' docx_summary(doc)
777-
#'
778-
#' docx_summary(doc, detailed = TRUE)
769+
#' @example inst/examples/example_docx_summary.R
779770
#' @export
780771
docx_summary <- function(
781772
x,

R/ph_location.R

Lines changed: 10 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,7 @@ is_ph_location <- function(x) {
189189
#' @param x a location for a placeholder.
190190
#' @param doc an rpptx object
191191
#' @param ... unused arguments
192-
#' @examples
193-
#' doc <- read_pptx()
194-
#' doc <- add_slide(doc,
195-
#' layout = "Title and Content",
196-
#' master = "Office Theme"
197-
#' )
198-
#' fortify_location(ph_location_fullsize(), doc)
192+
#' @example inst/examples/example_fortify_location.R
199193
#' @seealso [ph_location()], [ph_with()]
200194
#' @family functions for officer extensions
201195
#' @keywords internal
@@ -236,24 +230,7 @@ fortify_location <- function(x, doc, ...) {
236230
#' will be identified with that label in the *Selection Pane* of PowerPoint.
237231
#' This label can then be reused by other functions such as `ph_location_label()`.
238232
#' It can be set with argument `newlabel`.
239-
#' @examples
240-
#' doc <- read_pptx()
241-
#' doc <- add_slide(doc, "Title and Content")
242-
#' doc <- ph_with(doc, "Hello world",
243-
#' location = ph_location(width = 4, height = 3, newlabel = "hello")
244-
#' )
245-
#' print(doc, target = tempfile(fileext = ".pptx"))
246-
#'
247-
#' # Set geometry and outline
248-
#' doc <- read_pptx()
249-
#' doc <- add_slide(doc, "Title and Content")
250-
#' loc <- ph_location(
251-
#' left = 1, top = 1, width = 4, height = 3, bg = "steelblue",
252-
#' ln = sp_line(color = "red", lwd = 2.5),
253-
#' geom = "trapezoid"
254-
#' )
255-
#' doc <- ph_with(doc, "", loc = loc)
256-
#' print(doc, target = tempfile(fileext = ".pptx"))
233+
#' @example inst/examples/example_ph_location.R
257234
ph_location <- function(
258235
left = 1,
259236
top = 1,
@@ -312,16 +289,7 @@ fortify_location.location_manual <- function(x, doc, ...) {
312289
#' @param ... unused arguments
313290
#' @family functions for placeholder location
314291
#' @inherit ph_location details
315-
#' @examples
316-
#' doc <- read_pptx()
317-
#' doc <- add_slide(doc, "Title and Content")
318-
#' doc <- ph_with(doc, "Title",
319-
#' location = ph_location_type(type = "title")
320-
#' )
321-
#' doc <- ph_with(doc, "Hello world",
322-
#' location = ph_location_template(top = 4, type = "title")
323-
#' )
324-
#' print(doc, target = tempfile(fileext = ".pptx"))
292+
#' @example inst/examples/example_ph_location_template.R
325293
#' @export
326294
ph_location_template <- function(
327295
left = 1,
@@ -394,33 +362,7 @@ fortify_location.location_template <- function(x, doc, ...) {
394362
#' @param ... unused arguments
395363
#' @family functions for placeholder location
396364
#' @inherit ph_location details
397-
#' @examples
398-
#' # ph_location_type demo ----
399-
#'
400-
#' loc_title <- ph_location_type(type = "title")
401-
#' loc_footer <- ph_location_type(type = "ftr")
402-
#' loc_dt <- ph_location_type(type = "dt")
403-
#' loc_slidenum <- ph_location_type(type = "sldNum")
404-
#' loc_body <- ph_location_type(type = "body")
405-
#'
406-
#'
407-
#' doc <- read_pptx()
408-
#' doc <- add_slide(doc, "Title and Content")
409-
#' doc <- ph_with(x = doc, "Un titre", location = loc_title)
410-
#' doc <- ph_with(x = doc, "pied de page", location = loc_footer)
411-
#' doc <- ph_with(x = doc, format(Sys.Date()), location = loc_dt)
412-
#' doc <- ph_with(x = doc, "slide 1", location = loc_slidenum)
413-
#' doc <- ph_with(x = doc, letters[1:10], location = loc_body)
414-
#'
415-
#' loc_subtitle <- ph_location_type(type = "subTitle")
416-
#' loc_ctrtitle <- ph_location_type(type = "ctrTitle")
417-
#' doc <- add_slide(doc, layout = "Title Slide")
418-
#' doc <- ph_with(x = doc, "Un sous titre", location = loc_subtitle)
419-
#' doc <- ph_with(x = doc, "Un titre", location = loc_ctrtitle)
420-
#'
421-
#' fileout <- tempfile(fileext = ".pptx")
422-
#' print(doc, target = fileout)
423-
#'
365+
#' @example inst/examples/example_ph_location_type.R
424366
ph_location_type <- function(
425367
type = "body",
426368
type_idx = NULL,
@@ -522,26 +464,7 @@ fortify_location.location_type <- function(x, doc, ...) {
522464
#' @param ... unused arguments
523465
#' @family functions for placeholder location
524466
#' @inherit ph_location details
525-
#' @examples
526-
#' # ph_location_label demo ----
527-
#'
528-
#' doc <- read_pptx()
529-
#' doc <- add_slide(doc, layout = "Title and Content")
530-
#'
531-
#' # all ph_label can be read here
532-
#' layout_properties(doc, layout = "Title and Content")
533-
#'
534-
#' doc <- ph_with(doc, head(iris),
535-
#' location = ph_location_label(ph_label = "Content Placeholder 2")
536-
#' )
537-
#' doc <- ph_with(doc, format(Sys.Date()),
538-
#' location = ph_location_label(ph_label = "Date Placeholder 3")
539-
#' )
540-
#' doc <- ph_with(doc, "This is a title",
541-
#' location = ph_location_label(ph_label = "Title 1")
542-
#' )
543-
#'
544-
#' print(doc, target = tempfile(fileext = ".pptx"))
467+
#' @example inst/examples/example_ph_location_label.R
545468
ph_location_label <- function(ph_label, newlabel = NULL, ...) {
546469
x <- list(ph_label = unname(ph_label), label = unname(newlabel))
547470
class(x) <- c("location_label", "location_str")
@@ -633,11 +556,7 @@ fortify_location.location_label <- function(x, doc, ...) {
633556
#' @param newlabel a label to associate with the placeholder.
634557
#' @param ... unused arguments
635558
#' @family functions for placeholder location
636-
#' @examples
637-
#' doc <- read_pptx()
638-
#' doc <- add_slide(doc, "Title and Content")
639-
#' doc <- ph_with(doc, "Hello world", location = ph_location_fullsize())
640-
#' print(doc, target = tempfile(fileext = ".pptx"))
559+
#' @example inst/examples/example_ph_location_fullsize.R
641560
ph_location_fullsize <- function(newlabel = "", ...) {
642561
x <- list(label = newlabel)
643562
class(x) <- c("location_fullsize", "location_str")
@@ -671,12 +590,7 @@ fortify_location.location_fullsize <- function(x, doc, ...) {
671590
#' @param newlabel a label to associate with the placeholder.
672591
#' @param ... unused arguments
673592
#' @family functions for placeholder location
674-
#' @examples
675-
#' doc <- read_pptx()
676-
#' doc <- add_slide(doc, "Title and Content")
677-
#' doc <- ph_with(doc, "Hello left", location = ph_location_left())
678-
#' doc <- ph_with(doc, "Hello right", location = ph_location_right())
679-
#' print(doc, target = tempfile(fileext = ".pptx"))
593+
#' @example inst/examples/example_ph_location_left.R
680594
ph_location_left <- function(newlabel = NULL, ...) {
681595
x <- list(label = newlabel)
682596
class(x) <- c("location_left", "location_str")
@@ -715,12 +629,7 @@ fortify_location.location_left <- function(x, doc, ...) {
715629
#' @param newlabel a label to associate with the placeholder.
716630
#' @param ... unused arguments
717631
#' @family functions for placeholder location
718-
#' @examples
719-
#' doc <- read_pptx()
720-
#' doc <- add_slide(doc, "Title and Content")
721-
#' doc <- ph_with(doc, "Hello left", location = ph_location_left())
722-
#' doc <- ph_with(doc, "Hello right", location = ph_location_right())
723-
#' print(doc, target = tempfile(fileext = ".pptx"))
632+
#' @example inst/examples/example_ph_location_left.R
724633
ph_location_right <- function(newlabel = NULL, ...) {
725634
x <- list(label = newlabel)
726635
class(x) <- c("location_right", "location_str")
@@ -763,21 +672,7 @@ fortify_location.location_right <- function(x, doc, ...) {
763672
#' @param ... not used.
764673
#' @family functions for placeholder location
765674
#' @inherit ph_location details
766-
#' @examples
767-
#' doc <- read_pptx()
768-
#' doc <- add_slide(doc, "Comparison")
769-
#' plot_layout_properties(doc, "Comparison")
770-
#'
771-
#' doc <- ph_with(doc, "The Title", location = ph_location_id(id = 2)) # title
772-
#' doc <- ph_with(doc, "Left Header", location = ph_location_id(id = 3)) # left header
773-
#' doc <- ph_with(doc, "Left Content", location = ph_location_id(id = 4)) # left content
774-
#' doc <- ph_with(doc, "The Footer", location = ph_location_id(id = 8)) # footer
775-
#'
776-
#' file <- tempfile(fileext = ".pptx")
777-
#' print(doc, file)
778-
#' \dontrun{
779-
#' file.show(file) # may not work on your system
780-
#' }
675+
#' @example inst/examples/example_ph_location_id.R
781676
ph_location_id <- function(id, newlabel = NULL, ...) {
782677
ph_id <- id # for disambiguation, store initial value
783678

@@ -867,16 +762,7 @@ fortify_location.location_id <- function(x, doc, ...) {
867762
#' | `2` | Length 1 integer | `ph_location_id(2)` |
868763
#' | `c(0,0,4,5)` | Length 4 numeric, optionally named, `c(top=0, left=0, ...)` | `ph_location(0, 0, 4, 5)` |
869764
#'
870-
#' @examples
871-
#' resolve_location("left")
872-
#' resolve_location("right")
873-
#' resolve_location("fullsize")
874-
#' resolve_location("body")
875-
#' resolve_location("body [1]")
876-
#' resolve_location("<some label>")
877-
#' resolve_location(2)
878-
#' resolve_location(c(0, 0, 4, 5))
879-
#'
765+
#' @example inst/examples/example_resolve_location.R
880766
resolve_location <- function(x) {
881767
if (is_ph_location(x)) {
882768
return(x)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
library(officer)
2+
3+
bl <- block_list(
4+
fpar("Comment multiple words."),
5+
fpar("Second line")
6+
)
7+
8+
a_par <- fpar(
9+
"This paragraph contains",
10+
run_comment(
11+
cmt = bl,
12+
run = ftext("a comment."),
13+
author = "Author Me",
14+
date = "2023-06-01"
15+
)
16+
)
17+
18+
doc <- read_docx()
19+
doc <- body_add_fpar(doc, value = a_par, style = "Normal")
20+
21+
docx_file <- print(doc, target = tempfile(fileext = ".docx"))
22+
23+
docx_comments(read_docx(docx_file))
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
library(officer)
2+
3+
txt_lorem <- rep(
4+
"Purus lectus eros metus turpis mattis platea praesent sed. ",
5+
50
6+
)
7+
txt_lorem <- paste0(txt_lorem, collapse = "")
8+
9+
header_first <- block_list(fpar(ftext("text for first page header")))
10+
header_even <- block_list(fpar(ftext("text for even page header")))
11+
header_default <- block_list(fpar(ftext("text for default page header")))
12+
footer_first <- block_list(fpar(ftext("text for first page footer")))
13+
footer_even <- block_list(fpar(ftext("text for even page footer")))
14+
footer_default <- block_list(fpar(ftext("text for default page footer")))
15+
16+
ps <- prop_section(
17+
header_default = header_default,
18+
footer_default = footer_default,
19+
header_first = header_first,
20+
footer_first = footer_first,
21+
header_even = header_even,
22+
footer_even = footer_even
23+
)
24+
25+
x <- read_docx()
26+
27+
x <- docx_set_settings(
28+
x = x,
29+
zoom = 2,
30+
list_separator = ",",
31+
even_and_odd_headers = TRUE
32+
)
33+
34+
for (i in 1:20) {
35+
x <- body_add_par(x, value = txt_lorem)
36+
}
37+
x <- body_set_default_section(
38+
x,
39+
value = ps
40+
)
41+
print(x, target = tempfile(fileext = ".docx"))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
library(officer)
2+
3+
example_docx <- system.file(
4+
package = "officer",
5+
"doc_examples/example.docx"
6+
)
7+
doc <- read_docx(example_docx)
8+
9+
docx_summary(doc)
10+
11+
docx_summary(doc, detailed = TRUE)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
library(officer)
2+
3+
doc <- read_pptx()
4+
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
5+
fortify_location(ph_location_fullsize(), doc)

0 commit comments

Comments
 (0)