merck / r2rtf Goto Github PK
View Code? Open in Web Editor NEWEasily Create Production-Ready Rich Text Format (RTF) Table and Figure
Home Page: https://merck.github.io/r2rtf
License: GNU General Public License v3.0
Easily Create Production-Ready Rich Text Format (RTF) Table and Figure
Home Page: https://merck.github.io/r2rtf
License: GNU General Public License v3.0
The converted html file has a larger cell height than expected.
We need to find a way to adjust cell height. (maybe using CSS?)
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
The function can not properly handle vectorized input. For example
r2rtf:::rtf_text(c("a", "b"), color = "blue')
An option to include pagination below each table (eg, "Page 1 of 2") is needed.
Thank you for an excellent package! This is a request for support of other output formats, such as html for integration with rmarkdown.
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:
Hi Robert:
That would be great if you could review the r2rtf design pattern and provide suggestions if any.
This is a document to help developer understand the logic behind r2rtf
. We would like to improve this document for future collaborators.
Documentation
Prepare for release
cran-comments.md
(optional)urlchecker::url_check()
Submit to CRAN
Wait for CRAN
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.
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?
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 ..).
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.
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?
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"))
After inserting row breaks between groups,
I noticed that the row breaks themselves are using the RTF default of Times New Roman 12-pt.
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")
Documentation
Prepare for release
cran-comments.md
(optional)urlchecker::url_check()
Submit to CRAN
Wait for CRAN
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")
### 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")
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).
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.
We would like the page header to be hidden for all pages, like this:
Is there a way we can do that? Thank you!
we shall clarify the unit is twip for indent in rtf_xxx
function.
e.g.
A value of text left indent.
A value of text right indent.
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"
Refer to the reprex in issue #8
Any chance to get the footnotes to repeat across pages, like the titles and colheaders do?
Thanks,
Robert
We can potentially improve rtf_convert_format()
to:
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.
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()
.
Optimize the call parameters.
Maybe look into this and this.
This update will need testing on different infrastructures but I guess it's doable.
The download tells me "No File" when trying to retrieve the PDF.
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}
}
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.
The test shall be based on the code in #61
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)
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")
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
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?
pkgdown CSS template needs to be updated after using Bootstrap 5 with pkgdown 2.0.0
https://merck.github.io/r2rtf/index.html
pkgdown release notes: https://www.tidyverse.org/blog/2021/12/pkgdown-2-0-0/
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
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). |
---|
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()and
stringr::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")
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")
There are some use cases we will need to customize text in different places. For example,
It would be good if we have a API to allow user define a pattern and assign a format to it.
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.
Documentation
Prepare for release
cran-comments.md
(optional)urlchecker::url_check()
Submit to CRAN
Wait for CRAN
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()
My client wants to see the pageby_header
as a title line above the header like this:
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?
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}
---
An option is needed to allow r2rtf to print underscores (eg, in path names) rather than converting subsequent text to subscript.
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.
The current function did not recognize vectorized formatting argument.
tbl <- head(iris) %>%
rtf_page_header(c("text1", "text2"), text_justification = c("r", "l")) %>%
rtf_body() %>% rtf_encode() %>% write_rtf("tmp.rtf")
Hi,
I want to generate a table where there are too many columns (>20). To fit all the columns in one page (landscape format), I can reduce the font size, but then it becomes unreadable.
Is there a way to split the table after certain number of columns and get part 1 of page X, part 2 of page X
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.