davidgohel / ReporteRs

Note that ReporteRs has been removed from CRAN the 16th of July 2018 and is not maintained anymore. please migrate to officer.
244 stars 44 forks source link

Less greedy "Replace content" #149

Closed slarge closed 7 years ago

slarge commented 7 years ago

From the very useful "getting started" section about using bookmarks to replace content in Word files:

If the argument is used, content (plots, paragraphs or images) will replace the whole paragraph containing the bookmark.

I can see how replacing the entire paragraph can be useful, but I have several instances where a "less greedy" replace content function would be useful. For example, I have two formats that I want to preserve and add text from a database:

Figure [BOOKMARK_1] _The caption for this species [BOOKMARK2] should be in italics

mydoc <- addParagraph(mydoc, value = "5.3.1" , stylename = "small" , bookmark = "BOOKMARK_1" ) %>% addParagraph( "Gadus morhua", stylename = "small", bookmark = "BOOKMARK_2")

Given the current implementation of the addParagraph() function, I am not certain I can have such nuance. Would this be a possible functionality to add to ReporteRs? Thanks in advance.

davidgohel commented 7 years ago

If possible, I think the function should be something like replaceText as you're not dealing with whole paragraph but chunk of text inside a paragraph.

David

davidgohel commented 7 years ago

There is an existing solution for that issue, you have to use pot objects. Below an illustration with your example:

library(ReporteRs)
library(magrittr)

# various text formatting properties we will use----
base_text_prop <- textProperties(font.family = "Arial")
bold_text_prop <- chprop( base_text_prop, font.weight = "bold")
italic_text_prop <- chprop( base_text_prop, font.style = "italic")

# used variables ----
BOOKMARK_1 <- "5.3.1"
BOOKMARK_2 <- "Gadus morhua"

# concatenate chunk of formatted text -----
your_pot <- pot("Figure ", format = bold_text_prop) + 
  pot(BOOKMARK_1, format = bold_text_prop) + 
  pot(
    paste0("The caption for this species ", BOOKMARK_2," should be in italics"), 
    format = italic_text_prop)

# replace whole paragraph by the previous object -----
doc = docx( title = "My example",
            template = file.path( system.file(package = "ReporteRs"),
                                  "templates/bookmark_example.docx") ) %>% 
  addParagraph(your_pot, bookmark = "ANYDATA" ) %>% 
  writeDoc("bookmark.docx")
slarge commented 7 years ago

Thanks David, I agree that your suggestion solves the problem at hand. I think it would still be very useful to have a less greedy replace content for instances where you want to preserve automatic functions like pagination with concatenated values from R for a volume (or book) number or something. Thanks again for all your help.

   rm(list = ls())
  library(ReporteRs)
  library(magrittr)
  draftDoc <- docx(template = "~/git/ices-dk/AdviceTemplate/AdviceTemplate.docx")

  # styles(draftDoc)
  draftDoc <- draftDoc %>%
    addParagraph(value = "TEXT text \t\t text text",
                 stylename = "EcoregionHeader",
                 bookmark = "Ecoregion_Header") %>%
    addParagraph(value = "Other text \n\n text text",
                 stylename = "AdviceHeading",
                 bookmark = "Advice_Heading") %>%
    addParagraph(value = "Volume 1",
                 stylename = "Footer",
                 bookmark = "Volume_Number") %>%
    writeDoc(file = paste0("~/git/ices-dk/AdviceTemplate/tester.docx"))
  sessionInfo()

sessionInfo() R version 3.3.2 (2016-10-31) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows >= 8 x64 (build 9200)

locale: [1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 LC_MONETARY=English_United Kingdom.1252 [4] LC_NUMERIC=C LC_TIME=English_United Kingdom.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] tidyr_0.6.0 dplyr_0.5.0 icesSAG_1.1-0 icesSLD_0.0-0 ReporteRs_0.8.7.9003 ReporteRsjars_0.0.2 devtools_1.12.0

loaded via a namespace (and not attached): [1] Rcpp_0.12.7 magrittr_1.5 knitr_1.15 xml2_1.0.0 xtable_1.8-2 R6_2.1.2 httr_1.2.1 tools_3.3.2
[9] grid_3.3.2 rvg_0.1.1 R.oo_1.20.0 png_0.1-7 DBI_0.4-1 git2r_0.15.0 withr_1.0.2 htmltools_0.3.5
[17] lazyeval_0.2.0 assertthat_0.1 digest_0.6.10 tibble_1.2 rJava_0.9-8 shiny_0.14.2 bitops_1.0-6 R.utils_2.5.0
[25] RCurl_1.95-4.8 curl_2.2 memoise_1.0.0 mime_0.5 gdtools_0.1.3 R.methodsS3_1.7.1 XML_3.98-1.4 jsonlite_1.1
[33] httpuv_1.3.3

AdviceTemplate.docx

davidgohel commented 7 years ago

I do agree. Maybe later :)