Giter VIP home page Giter VIP logo

r2rtf's People

Contributors

brianlang avatar elong0527 avatar fb-elong avatar fenguoerbian avatar howardbaek avatar kongfans53 avatar nanxstats avatar wangben718 avatar yihui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

r2rtf's Issues

base example first 2 columns get concatenated

With simple export to rtf, even with the base example for some reason the first 2 columns are concatenated. It looks ok till rtf_body() output but then for some reason appears concatenated after the write_rtf() function in the output
library(r2rtf)
head(iris) %>%
rtf_body() %>% # Step 1 Add attributes
rtf_encode() %>% # Step 2 Convert attributes to RTF encode
write_rtf(file = "ex-tbl.rtf") # Step 3 Write to a .rtf file
Screen Shot 2021-09-17 at 6 53 47 PM

Page number

An option to include pagination below each table (eg, "Page 1 of 2") is needed.

Total page number not displaying correctly when multiple pages are combined

We are creating a table with one subject per page using the method of issue #80 (creating one subject per file, and then combining the files), and we want to put the page number (Page X of Y) in the rtf_title(). But the total page number (\\totalpage) in the title starts over at each Subject ID. The page numbering in the rtf_page_header() works correctly.

How can we have the page numbering in the title work like the page numbering in the page header (1 of 3, 2 of 3, 3 of 3)?

Sample code:

library(r2rtf)
library(dplyr)


data(r2rtf_adae)

ae_t1 <- r2rtf_adae %>%
  filter(USUBJID %in% c("01-701-1383", "01-701-1444")) %>%
  mutate(SUBLINEBY = TRTA) %>%   
  select(USUBJID, ASTDY, AEDECOD, SUBLINEBY) 

# split data by USUBJID
x <- split(ae_t1, ae_t1$USUBJID)

# create one AE listing file per subject.
x_encode <- lapply(x, function(tbl){
  
  tbl %>%
    arrange(SUBLINEBY, USUBJID, ASTDY) %>%
    rtf_page(orientation = "landscape", nrow = 15) %>%
    rtf_page_header() %>%
    rtf_title(c("AE Listing", 
                "Page \\pagenumber of \\totalpage")) %>%
    rtf_body( 
      subline_by = "SUBLINEBY",
      group_by = c("USUBJID", "ASTDY")) %>% 
    rtf_encode()
  
})

# Combine other RTF body to the first one  
x_encode[[1]]$body <- paste(lapply(x_encode, function(x) x$body), collapse = r2rtf:::as_rtf_new_page())

x_encode[[1]] %>% write_rtf("output.rtf")

The page numbering in the page header is correct, but the total page number in the title is not correct:

image

Release r2rtf v0.3.0

Documentation

  • Seek review from current users
  • Review all Vignettes

Prepare for release

  • Check current CRAN check results
  • Check licensing of included files
  • Review pkgdown reference index for, e.g., missing topics
  • Bump version
  • Update cran-comments.md (optional)
  • Update NEWS.md
  • Review pkgdown website
  • urlchecker::url_check()
  • Check with local machine
  • Check with GitHub Actions
  • Check with win-builder

Submit to CRAN

  • Draft GitHub release
  • Submit to CRAN via web form
  • Approve emails

Wait for CRAN

  • Accepted 🎉
  • Post on r-packages mailing list
  • Tweet

Vertical Align Column Headers

Is there a way to vertically align column headers in a table? The headers default to top aligned and I would like to bottom align them.

Concatenating multiple tables with footnote on every page

If I concatenate multiple tables where the first table in the list contains the footnote and encode it with the parameter page_footnote = 'all', it will only print the footnote on the last page of a multi page report. Is this expected behavior when concatenating multiple tables or should the footnote display on every page?

check the usage of `as.vector`

From CRAN maintainer

Dear maintainers,

This concerns the CRAN packages

Note that your package have started failing their checks with the latest development versions of R, 'R-devel' for short.

The reason is that we/I have introduced a new
as.vector.data.frame() method, and saying in NEWS

    • as.vector() gains a data.frame method which returns a
      simple named list, also obeying a long standing      
      ‘FIXME’ to enable as.vector(<data.frame>, mode="list").
      This will break code relying on 'as.vector(<data.frame>)' to
      return the unchanged data frame.

The last sentence applies to your respective packages where you call as.vector()  on a data frame -- which in R <= 4.1.x returns the data frame *unchanged* howeve now (in R-devel) returns a list (with the data frame
columns) but not a data frame anymore.
That's why your code now fails.
The solution will be to simply *NOT* call  as.vector(.) in that part of the code ..

Please let me/us know if you need help fixing your respective
package(s) to work flawlessly on the latest version of R, R-devel.
Note that this change will also make your code very slightly faster:
you should never have called as.vector(.) on a data frame: it does not do anything (but cost a few nanoseconds ..).

Double top and bottom borders

Whenever I have kept no borders for left and right, I have seen that the double borders get applied to top and bottom of the report. Please see the rtf file in the zip file.

I am attaching my all data file, R program file and rtf files.
tbl_14_1_4.zip

Can these double borders of top and bottom be made as single borders? I saw all the vignette examples and did not get any solution to my problem.

How to show multiple lines in page header or footer?

Is there a way to add multiple lines of words (or a empty underline) with different alignments in the page header and footer? Or is there a way to insert a table (with multiple lines or top/bottom border) in the page header or footer?

assemble plain rtf files

Here is a draft example function to combine multiple RTF files into one.

Known limitation: the current implementation does not recognize the page size of each file. (e.g. portrait vs. landscape)

#' Assemble RTF files into one single RTF file 
#' 
#' @param path a vector of RTF file path
#' 
#' @export
rtf_assemble <- function(path){
  
  rtf <- lapply(path, readLines)
  
  n <- length(rtf)
  start <- c(1, rep(2, n - 1))
  end <- vapply(rtf, length, numeric(1))
  end[-n] <- end[-n] - 1
  
  for(i in 1:n){
    rtf[[i]] <- rtf[[i]][start[i]:end[i]]
    if(i < n) rtf[[i]] <- c(rtf[[i]], r2rtf:::as_rtf_new_page())
  }
  rtf <- do.call(c, rtf)
  
  rtf
  
}

dummy example:

file <- c( "tmp1.rtf", "tmp2"))

rtf_assemble(file) %>% 
  r2rtf::write_rtf(file.path(path, "tmp1.rtf"))

Inserted row breaks appear as Times New Roman 12-pt

After inserting row breaks between groups,
I noticed that the row breaks themselves are using the RTF default of Times New Roman 12-pt.
image

Reprex provided below.

I did find a workaround, using this code to reset the default font size from 12 to whatever was used in the rtf_body:

xyz_encode <- iris_rtf %>% 
  rtf_encode()

# set default font size for Times New Roman
fs <- format(2*attr(iris_rtf, "text_font_size")[1])
xyz_encode$start <- paste0(xyz_encode$start, "\\f0\\fs",fs)

xyz_encode %>% 
  write_rtf("output/l-iris.rtf")

I was wondering if you might entertain the idea of adding an option to set the default font size in r2rtf?
Thanks for reading this.
Robert

library(dplyr)
library(tibble)
library(r2rtf)

data("iris")

set.seed(34567)
sample_iris <- iris %>% 
  group_by(Species) %>% 
  slice_sample(n = 5) %>%          # take 5 rows of each Species
  do(add_row(., .before = 1)) %>%  # add a blank row before each group
  ungroup()

iris_rtf <- sample_iris %>% 
  select(Species, everything()) %>% 
  rtf_page(orientation = "landscape", 
           border_first = "single", border_last = "single",
           margin = c(1.0, 1.0, 1.0, 1.0, 0.5, 0.5) ) %>% 
  
  rtf_title( title=paste("\\b\\fs24","Iris data by Species","\\b0"), 
             subtitle = paste("\\fs20","Page \\pagenumber of \\totalpage"),
             text_justification = "l",
             text_convert = TRUE  ) %>%
  
  rtf_colheader(colheader = c(" Species | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width "), 
                text_format = "b", 
                col_rel_width = c(rep(2,5)),
                cell_justification = "l",
                text_justification = c("l",rep("d",4)),
                border_left = "", border_right = ""  ) %>%
  
  rtf_body(col_rel_width = c(rep(2,5)),
           cell_justification = "l",
           text_justification = c("l",rep("d",4)),
           text_font = 9,  # Courier New
           border_left = "", border_right = "") 

iris_rtf %>% 
  rtf_encode() %>% 
  write_rtf("output/l-iris.rtf") 

Release r2rtf v0.3.2

Documentation

  • Seek review from current users
  • Review all Vignettes

Prepare for release

  • Check current CRAN check results
  • Check licensing of included files
  • Review pkgdown reference index for, e.g., missing topics
  • Bump version
  • Update cran-comments.md (optional)
  • Update NEWS.md
  • Review pkgdown website
  • urlchecker::url_check()
  • Check with local machine
  • Check with GitHub Actions
  • Check with win-builder

Submit to CRAN

  • Draft GitHub release
  • Submit to CRAN via web form
  • Approve emails

Wait for CRAN

  • Accepted 🎉
  • Post on r-packages mailing list
  • Tweet

How to add a paragraph after the table?

r2rtf does not officially support this feature. But here is a way to achieve it.

library(r2rtf)
library(dplyr)

program_note <- "this is a program note"
program_note_encode <- r2rtf:::rtf_paragraph(r2rtf:::rtf_text(program_note, color = "blue"), justification = "l")

source_note <- "this is a source"
source_note_encode <- r2rtf:::rtf_paragraph(r2rtf:::rtf_text(source_note, color = "black"), justification = "c")
  
iris %>% head() %>% 
      rtf_body() %>% 
      rtf_source(c(source_note_encode, program_note_encode), text_color = "blue") %>%   
      rtf_encode() %>% 
      write_rtf("tmp.rtf")

r2rtf needs to work properly with `group_by`

### Preliminaries
rm(list=ls())
require(tidyverse)
require(r2rtf)

# Get data from package
dat <- select(r2rtf_adsl, SUBJID, ARM, SEX, AGEGR1, WEIGHTBL)

# Calculate mean weight by ARM and SEX
stats <- dat %>% group_by(ARM, SEX, AGEGR1) %>% summarize(N=n(), WEIGHT.MEAN=mean(WEIGHTBL))

# New header
hdr <- c("Arm | Gender | Age Group | N Subjects | Weight")

# Doesn't update header
stats %>%
  rtf_colheader(colheader = hdr, text_format="b") %>%
  rtf_body() %>%
  rtf_encode() %>%
  write_rtf(file = "rtf_colheader_test1.rtf")

# Updates header
as_tibble(stats) %>%
rtf_colheader(colheader = hdr, text_format="b") %>%
rtf_body() %>%
rtf_encode() %>%
write_rtf(file = "rtf_colheader_test2.rtf")

Broken link on the pkgdown site

Maybe this is a placeholder page but we should make the links work.

Steps to reproduce:

Page shows a 404 and links on the page are not working (probably because pkgdown uses relative paths).

pageby_header = FALSE does not hide page header for all pages

When we set pageby_header = FALSE, the page header is not hidden. It is displayed at the beginning of each new page_by variable, and then hidden on subsequent pages.

For example, we want to create a table with one subject per page, but we don’t want to show the page header.

Here is code for a simple table:

library(r2rtf)
library(dplyr)


data(r2rtf_adae)

ae_t1 <- r2rtf_adae %>%
  filter(USUBJID %in% c("01-701-1383", "01-701-1444")) %>%
  mutate(SUBLINEBY = TRTA) %>% 
  mutate(PAGEBY = USUBJID) %>%    
  select(USUBJID, ASTDY, AEDECOD, SUBLINEBY, PAGEBY) 

ae_tbl <- ae_t1 %>%
  arrange(SUBLINEBY, USUBJID, ASTDY) %>%
  rtf_page(orientation = "landscape", nrow = 15) %>%
  rtf_page_header() %>%
  rtf_title("AE Listing") %>%
  rtf_body( 
    subline_by = "SUBLINEBY",
    page_by = "PAGEBY",
    group_by = c("USUBJID", "ASTDY"),
    new_page = TRUE,
    pageby_header = FALSE) 

ae_tbl %>%
  rtf_encode() %>%
  write_rtf("rtf/pageby-ae-listing.rtf")

The pageby_header argument is set to FALSE, but the page header still displays on pages 1 and 3, where a new subject ID starts. The page header is hidden on page 2, which is a continuation of the subject ID (01-701-1383) from page 1.

pageby-ae-listing_actual.pdf

We would like the page header to be hidden for all pages, like this:

pageby-ae-listing_desired.pdf

Is there a way we can do that? Thank you!

Margins options

The ability to specify page margins or an option to choose the following set of margins is needed.

8.5 x 11 (US letter) paper
  | Portrait | Landscape
Top | 1.0" | 1.5"
Bottom | 0.75" | 1.0"
Left | 1.5" | 0.75"
Right | 1.0" | 1.0"

Is it available for Chinese Characters?

I saved Rscript with UTF-8 to allow Chinese Character then it will report as below
image
It seems that I cannot input footnotes in Chinese, so I removed these footnotes, then it could output an RTF file but with all Chinese strings are unreadable.

Improve program discovery in rtf_convert_format()

We can potentially improve rtf_convert_format() to:

  1. Make it not libreoffice-version-dependent.
    This can be easily fixed by not calling libreoffice7.1. Although you can specify a minimum version, see next.

  2. Make the program discovery robust to different systems types and libreoffice versions.
    rmarkdown::find_pandoc() is a good example I can think of, it uses rmarkdown:::find_program().

  3. Optimize the call parameters.
    Maybe look into this and this.

This update will need testing on different infrastructures but I guess it's doable.

rtf_source() generates single-thickness bottom border on last page

Hi Yilong,

I have all the borders turned off in this reprex below and yet I still see a single-thickness border appear on the last page.

library(dplyr)
library(stringr)
library(r2rtf)

# create mtcars2 from mtcars
data(mtcars)
mtcars2 <- mtcars %>% 
  mutate(carname = rownames(mtcars)) %>% 
  arrange(wt) %>% 
  select(carname,mpg,cyl,disp,hp,wt,am,gear)
  
rel_wd <- c(2.5, rep(1.0,4), 1.5, 1, 1)

# setup column headers
hdrs <- paste(colnames(mtcars2), collapse = "|")

# footnote
ft1 <- "This is a footnote about the mtcars data"

# source line
reptname <- "l-mtcars-reprex.rtf"
date_str <- paste0("DATE: ",toupper(format(Sys.Date(), format="%d%b%Y")))
npad <- 101 - sum(nchar("SOURCE: "),nchar(reptname),nchar(date_str))
sourcestr <- paste0("\n","SOURCE: ",reptname,str_pad(" ",npad),date_str)

cars_rtf <- mtcars2 %>%
  rtf_page(orientation = "landscape", 
           border_first = "single", border_last = "single",
           margin = c(1.0, 1.0, 1.25, 1.25, 0.5, 0.5), 
           nrow = 24) %>% 
  
  rtf_title(title = "Listing of mtcars data, sorted by weight",
            subtitle = "Page \\pagenumber of \\totalpage",
            text_font_size = 10, text_font = 9, text_convert = TRUE) %>%
  
  rtf_colheader(colheader = hdrs,
                border_left = "", border_right = "",
                text_font_size = 10, text_font = 9, # 9 = Courier New
                text_space_before = 120, text_space_after = 0,
                col_rel_width = rel_wd,
                cell_justification = "l",
                text_justification =  c("l",rep(c("d","r"),3),"r") ) %>%

  rtf_body(col_rel_width = rel_wd,
           as_colheader = FALSE,
           text_space = 1,
           text_space_before = 0,
           text_space_after = 120,
           cell_justification =  "l",
           text_justification =  c("l",rep(c("d","r"),3),"r"),
           text_font_size = 10, text_font = 9,
           border_left = "", border_right = "") %>% 
  
  rtf_footnote(footnote = ft1,
               text_font_size = 10, text_font = 9,
               cell_justification = "l",
               text_justification = "l",
               as_table = TRUE,
               border_top = "", border_bottom = "",
               border_left = "", border_right = "") %>%
  
  rtf_source(source = sourcestr,
             text_font_size = 10, text_font = 9,
             cell_justification = "l",
             text_justification = "l",
             as_table = TRUE,
             border_top = "", border_bottom = "",
             border_left = "", border_right = "")

cars_rtf %>% 
  rtf_encode(page_footnote = "all",
             page_source = "all") %>%
  write_rtf(reptname)

For some reason, it adds a \brdrs into the last source line.
I am assuming this is a bug, not a feature.
Thanks for looking at this!

\trowd\trgaph108\trleft0\trql
\clbrdrl\brdrw15\clbrdrt\brdrw15\clbrdrr\brdrw15\clbrdrb\brdrw15\cellx12240
\pard\intbl\sb15\sa15\ql{\f8\fs20 This is a footnote about the mtcars data}\cell
\intbl\row\pard
\trowd\trgaph108\trleft0\trql
\clbrdrl\brdrw15\clbrdrt\brdrw15\clbrdrr\brdrw15\clbrdrb\brdrs\brdrw15\cellx12240
\pard\intbl\sb15\sa15\ql{\f8\fs20 \line SOURCE: l-mtcars-reprex.rtf                                                           DATE: 29DEC2020}\cell
\intbl\row\pard
{\pard\par}

}

Adding Vector Graphs

Can you incorporate functionality to add vector graphs like EMF to the rtf file? There is a package called devEMF which will create the EMF files just need the functionality to add it to the RTF file.

`text_format` issue with all value equal to `""`

An error below shall be avoid.

library(r2rtf)

f <- rep("", ncol(iris))

iris %>% 
  rtf_body(text_format = f)

hot fix:

library(r2rtf)

f <- rep("", ncol(iris))

if(all(f == "")) f <- NULL

iris %>% 
  rtf_body(text_format = f)

rtf_subline text indent default doesn't match documentation

Hi folks,

I noticed the documentation for rtf_subline indicates the default for text_indent_left = 0,
but when I run a small listing using iris data (below), it shows the text_index_left = 360 twips.
and it doesn't change when I specify text_indent_left = 0, in the code

I can post-process it to get what I want, but can you look into this please?

> attr(attr(l_iris, "rtf_subline"),"text_indent_left")
[1] 360

attr(attr(l_iris, "rtf_subline"),"text_indent_left") <- 0

Thanks,
Robert

library(r2rtf)
library(dplyr)

data("iris")

l_iris <- iris %>% 
  slice_sample(prop = 0.15) %>% # take 15% sample

  rtf_page(orientation = "landscape", 
           nrow = 26,
           border_first = "single", border_last = "single",
           # The value set left, right, top, bottom, header and footer margin in order.
           margin = c(1.0, 1.0, 1.0, 1.0, 0.5, 0.5) ) %>% 

  rtf_title( title=paste("\\b\\fs24","Iris data by Species","\\b0"), 
             subtitle = paste("\\fs20","Page \\pagenumber of \\totalpage"),
             text_justification = "l",
             text_convert = TRUE
             ) %>%
  
  rtf_subline(text = "xxyyzz", text_indent_left = 0,
              text_font_size = 10, text_font = 9, text_format = "b") %>%
  
  rtf_colheader(colheader = c(" Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | "), 
                text_font_size = 9, text_format = "b", 
                col_rel_width = c(2.4,2.4,2.4,2.4,0.4),
                cell_justification = "l",
                text_justification = c(rep("d",4),"l"),
                border_left = "", border_right = ""
                ) %>%
  
  rtf_body(col_rel_width = c(2.4,2.4,2.4,2.4,0),
           cell_justification = "l",
           text_justification = c(rep("d",4),"l"),
           text_font_size = 9, text_font = 1,
           page_by = "Species",
           pageby_header = TRUE,
           new_page = TRUE,
           border_first = "single", border_last = "single",
           border_left = "", border_right = "") 

l_iris %>% 
  rtf_encode() %>%
  write_rtf("output/l-iris.rtf") 

image

'unicode_latex' not found

If I import the r2rtf package and try to use the functions in the package using full namespace syntax without calling the libname(r2rtf) first I receive the following error when it tries to call r2rtf::rtf_encode(page_footnote = "all") :

Error in as.hexmode(unicode_latex$unicode) :
object 'unicode_latex' not found

Different titles for multiple figures

I have 6 figures in all (each parameter for Vital Signs, say). The one combined rtf for all 6 figures was generated with usual rtf options indicated in /vignettes/example-figure.Rmd. But I want to put different titles to different figures on each page.
By which option can I do this?

rtf_footnote with text_justification = "l" and as_table = FALSE indents the line 1/4 inch

I have a reproducible example for you:

library(dplyr)
library(stringr)
library(r2rtf)

# create mtcars2 from mtcars
data(mtcars)
mtcars2 <- mtcars %>% 
  mutate(carname = rownames(mtcars)) %>% 
  arrange(wt) %>% 
  select(carname,mpg,cyl,disp,hp,wt,am,gear)
  
rel_wd <- c(2.5, rep(1.0,4), 1.5, 1, 1)

# setup column headers
hdrs <- paste(colnames(mtcars2), collapse = "|")

# source line
reptname <- "l-mtcars-reprex.rtf"
date_str <- paste0("DATE: ",toupper(format(Sys.Date(), format="%d%b%Y")))
npad <- 102 - sum(nchar("SOURCE: "),nchar(reptname),nchar(date_str))
splitstr <- paste0("\n","SOURCE: ",reptname,str_pad(" ",npad),date_str)

cmb_rtf <- mtcars2 %>%
  rtf_page(orientation = "landscape", 
           border_first = "single", border_last = "single",
           margin = c(1.0, 1.0, 1.25, 1.25, 0.5, 0.5), 
           nrow = 24) %>% 
  
  rtf_title(title = "Listing of mtcars data, sorted by weight",
            subtitle = "Page \\pagenumber of \\totalpage",
            text_font_size = 10, text_font = 9, text_convert = TRUE) %>%
  
  rtf_colheader(colheader = hdrs,
                border_left = "", border_right = "", 
                text_font_size = 10, text_font = 9, # 9 = Courier New
                text_space_before = 60, text_space_after = 60,
                col_rel_width = rel_wd,
                cell_justification = "l",
                text_justification =  c("l",rep(c("d","r"),3),"r") ) %>% 

  rtf_body(col_rel_width = rel_wd,
           text_space = 1,
           text_space_before = 0,
           text_space_after = 120,
           cell_justification =  "l",
           text_justification =  c("l",rep(c("d","r"),3),"r"),
           text_font_size = 10, text_font = 9,
           border_left = "", border_right = "") %>% 
  
  rtf_footnote(footnote = "[1] This is a footnote.",
               text_font_size = 10, text_font = 9,
               text_justification = "l",
               cell_justification = "l",
               border_left = "", border_right = "",
               as_table = FALSE) %>%
  
  rtf_source(source = splitstr,
             text_justification = "j",text_font_size = 10, text_font = 9, text_convert = TRUE)

cmb_rtf %>% 
  rtf_encode() %>%
  write_rtf(reptname)

Opening the output in Notepad++

{\pard\hyphpar \sb15\sa15\fi0\li360\ri0\ql {\f8\fs20 [1] This is a footnote.} \par}

the \li360 indents 360 twips, 1/4 inch. -- I think it should be \li0

Thanks

The Font size doesn't consistency between function default value and the Arguments description

Take an example: rtf_page_header

rtf_page_header(
  tbl,
  text = "Page \\pagenumber of \\pagefield",
  text_font = 1,
  text_format = NULL,
  text_font_size = 12,
  text_color = NULL,
  text_background_color = NULL,
  text_justification = "r",
  text_indent_first = 0,
  text_indent_left = 0,
  text_indent_right = 0,
  text_space = 1,
  text_space_before = 15,
  text_space_after = 15,
  text_convert = TRUE
)

The text font size in function default value is 12, while in Arguments description is 9. This in-consistence occurred in other functions, too.

text_font_size Text font size. Default is 9. To vary text font size by column, use numeric vector with length of vector equal to number of columns displayed e.g. c(9,20,40).

Enhancement idea for "Page X of Y" in RTF body

Hi Yilong,
and thanks for your email reply last Friday.

For companies where the RTF header and footer are reserved for regulatory publishing,
I came with this workaround, and wanted to share that with you and get your thoughts.
This is similar to papers on SAS programming using ODS RTF that I would see on pharmaSUG

Let’s say we create an RTF document using the r2rtf::rtf_encode() function.

rtf_doc <- rtf_encode(ae_tbl)

and it happened to have two title lines:
rtf_title("Listing of Subjects With Serious Adverse Events", "#PageXofY")

Here is an R function that uses Base R functions gregexpr() and gsub() instead of 'stringr::str_locate_all()andstringr::str_replace_all()` so as to minimize the package footprint.

gregexpr() returns a list of all the start positions of the control word “\page”
NumPages is simply the length of the list of the start positions, plus 1 (if there are no page breaks, then there is one page).
The replacement string, repl, uses the “\chpgn” control word and the NumPages coerced into a character.
the body of the RTF document is updated. Every instance of “string” (the sentinel "#PageXofY") is replaced with “repl” using gsub().

Encapsulating \\chpgn in the function means programmers don’t have to get their hands dirty with RTF command line coding.

pagexofy <- function(rtf_doc, string = "#PageXofY") {
  # function to replace "string" with "Page X of Y" in RTF document body
  pages <- gregexpr("\\\\page", rtf_doc$body)  # \page = Required page break
  NumPages <- length(pages[[1]]) + 1
  repl <- paste("Page \\\\chpgn"," of", as.character(NumPages))
  rtf_doc$body <- gsub(string, repl, rtf_doc$body)
  return(rtf_doc)
}

Next we update the document and write it out.
pagexofy(rtf_doc) %>% write_rtf("pageby-ae-listing.rtf")

-or-

rtf_encode(ae_tbl) %>% pagexofy() %>% write_rtf("paged-ae-table.rtf")

Add parameter for left and right cell margins

The RTF table output has cell margins left = 0.08” and right = 0.08”. Is there a way to change these values? We often have tables with many columns, and it would be nice to be able to adjust these margins. Would it be possible to add a parameter so we can change the right and left cell margins? I think it would need to be added to at least rtf_colheader() and rtf_body().

Below is a reproducible example and a screenshot from Word.

library(r2rtf)
library(dplyr)
library(tidyr)

tbl <- r2rtf_adae %>%
  count(TRTA, AEDECOD) %>%
  pivot_wider(names_from = TRTA, values_from = n, values_fill = 0)

head(tbl) %>%
  rtf_body() %>% 
  rtf_encode() %>% 
  write_rtf("rtf/intro-ae1.rtf") 

image

Create a way to allow user define rules to customize text in r2rtf

There are some use cases we will need to customize text in different places. For example,

  1. highlight part of text in a cell.
  2. In preparing mock, automatically use blue color for any text in for a title.

It would be good if we have a API to allow user define a pattern and assign a format to it.

rtf_paragraph not getting loaded

I have written the following code:

res <- rtf_paragraph(rtf_text(quest_text),justification = "l")
write_rtf_para(res, "questions.rtf")

I am getting error message as could not find function "write_rtf_para". I found that the rtf_paragraph and also rtf_text also are not getting loaded.

Release r2rtf v0.3.1

Documentation

  • Seek review from current users
  • Review all Vignettes

Prepare for release

  • Check current CRAN check results
  • Check licensing of included files
  • Review pkgdown reference index for, e.g., missing topics
  • Bump version
  • Update cran-comments.md (optional)
  • Update NEWS.md
  • Review pkgdown website
  • urlchecker::url_check()
  • Check with local machine
  • Check with GitHub Actions
  • Check with win-builder

Submit to CRAN

  • Draft GitHub release
  • Submit to CRAN via web form
  • Approve emails

Wait for CRAN

  • Accepted 🎉
  • Post on r-packages mailing list
  • Tweet

pageby_header as a #byval above the column headers

I have been asked to find out a way to use the pageby_header like a SAS #byval() function,
similar to this SAS statement: title2 "Cohort=#byval(cohort)";

So instead of showing the pageby_header underneath the header created by rtf_colheader()
image

My client wants to see the pageby_header as a title line above the header like this:
image

I know this comes out of the rtf_body() function, so adding an option to move it would not be easy to do.
But no matter, I came up with a workaround.

I don't need you to fix anything, @elong0527 , just wanted to share this with you and have you comment on it.

original code:

sampl_iris <- iris %>% 
  group_by(Species) %>% 
  slice_sample(n = 5) # take 5 rows of each Species

iris_rtf <- sampl_iris %>% 
  rtf_page(orientation = "landscape", 
           nrow = 26,
           border_first = "single", border_last = "single",
           # The value set left, right, top, bottom, header and footer margin in order.
           margin = c(1.0, 1.0, 1.0, 1.0, 0.5, 0.5) ) %>% 
  
  rtf_title( title=paste("\\b\\fs24","Iris data by Species","\\b0"), 
             subtitle = paste("\\fs20","Page \\pagenumber of \\totalpage"),
             text_justification = "l",
             text_convert = TRUE
  ) %>%
  
  rtf_colheader(colheader = c(" Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | "), 
                text_font_size = 9, text_format = "b", 
                col_rel_width = c(2.4,2.4,2.4,2.4,0.1),
                cell_justification = "l",
                text_justification = c(rep("d",4),"l"),
                border_left = "", border_right = ""
  ) %>%
  
  rtf_body(col_rel_width = c(2.4,2.4,2.4,2.4,0.1),
           cell_justification = "l",
           text_justification = c(rep("d",4),"l"),
           text_font_size = 9, text_font = 1,
           page_by = "Species",
           pageby_header = TRUE,
           new_page = TRUE,
           border_first = "single", border_last = "single",
           border_left = "", border_right = "") 

iris_rtf %>% 
  rtf_encode() %>% 
  write_rtf("output/l-iris-before.rtf") 

Modified code (using the same data above)

iris_rtf <- sampl_iris %>% 
  rtf_page(orientation = "landscape", 
           nrow = 26,
           border_first = "single", border_last = "single",
           # The value set left, right, top, bottom, header and footer margin in order.
           margin = c(1.0, 1.0, 1.0, 1.0, 0.5, 0.5) ) %>% 

  rtf_title( title=paste("\\b\\fs24","Iris data by Species","\\b0"), 
             subtitle = paste("\\fs20","Page \\pagenumber of \\totalpage"),
             text_justification = "l",
             text_convert = TRUE
             ) %>%
  
  rtf_subline(text = "subline text", 
              text_font_size = 10, text_font = 9, text_format = "b") %>%
  
  rtf_colheader(colheader = c(" Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | "), 
                text_font_size = 9, text_format = "b", 
                col_rel_width = c(2.4,2.4,2.4,2.4,0.4),
                cell_justification = "l",
                text_justification = c(rep("d",4),"l"),
                border_left = "", border_right = ""
                ) %>%
  
  rtf_body(col_rel_width = c(2.4,2.4,2.4,2.4,0), # 0 for Species suppresses the output
           cell_justification = "l",
           text_justification = c(rep("d",4),"l"),
           text_font_size = 9, text_font = 1,
           page_by = "Species",
           pageby_header = TRUE,
           new_page = TRUE,
           border_first = "single", border_last = "single",
           border_left = "", border_right = "") 

# fix subline indent
attr(attr(iris_rtf, "rtf_subline"),"text_indent_left") <- 0

# get page headers from attributes
by_var <- attr(l_iris, "rtf_pageby")$by_var
# [1] "Species"
eval_this <- paste0('attr(l_iris, "rtf_pageby_row")$',by_var,'$x')
# [1] "attr(l_iris, \"rtf_pageby_row\")$Species$x"
headers <- as.character(eval(parse(text=eval_this)))
# chr [1:3] "setosa" "versicolor" "virginica"

iris_encode <- iris_rtf %>% 
  rtf_encode()

# replace subline text with header line
# note that str_replace only makes one change at a time
for (i in 1:length(headers)) {
  iris_encode$body <- str_replace(iris_encode$body,"subline text",paste("\\\\b",headers[[i]])) 
}

# now write it out
iris_encode %>% 
  write_rtf("output/l-iris-after.rtf") 

I found that setting the col-rel_width for the page_by variable "Species" to 0 (zero) suppresses the output
But the information is still there.
I was able to extract the "header" information (one per page) like this:

# get page headers from attributes
by_var <- attr(l_iris, "rtf_pageby")$by_var
# [1] "Species"
eval_this <- paste0('attr(l_iris, "rtf_pageby_row")$',by_var,'$x')
# [1] "attr(l_iris, \"rtf_pageby_row\")$Species$x"
headers <- as.character(eval(parse(text=eval_this)))
# chr [1:3] "setosa" "versicolor" "virginica"

and then run rtf_encode()

iris_encode <- iris_rtf %>% 
  rtf_encode()

and then replace the "subline text" with the headers.
I'm taking advantage of the fact that str_replace() only replaces one string at a time.
Here I'm adding the RTF bold directive in front of the headers.

# replace subline text with header line
# note that str_replace only makes one change at a time
for (i in 1:length(headers)) {
  iris_encode$body <- str_replace(iris_encode$body,"subline text",paste("\\\\b",headers[[i]])) 
}

and then just write it out

# now write it out
iris_encode %>% 
  write_rtf("output/l-iris-after.rtf") 

Thoughts? Comments?

Add missing fields to the vignettes yaml header

Right now, no vignettes shows up on CRAN.

This is probably due to the missing settings in the vignette yaml header, such as %\VignetteEngine{} and %\VignetteIndexEntry{}. Consider adding them.

---
title: "What the Vignette Does"
...
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{What the Vignette Does}
---

Underscore rendering

An option is needed to allow r2rtf to print underscores (eg, in path names) rather than converting subsequent text to subscript.

rtf_page breaks before 42 rows

A multi-page, portrait-oriented table has page breaks after 15-24 rows rather than including 42 rows per page. Specifying nrow=42 results in a longer first page, but subsequent pages still end after ~15 rows. No page_by or group_by options are specified.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.