Merck / r2rtf

Easily Create Production-Ready Rich Text Format (RTF) Table and Figure
https://merck.github.io/r2rtf
GNU General Public License v3.0
78 stars 20 forks source link

Error when trying to output rtf file with missing fields #146

Closed riawoo closed 1 year ago

riawoo commented 1 year ago

Hi there!

image

I have defined a table format for the attached dataframe and now trying to output into rtf file, however I am getting the below error message. I am thinking that possibly the issue is because I have missing values in the "pedesc" col. What can I do to prevent this from happening?

Error in if (nc[currentIndex] == 0L) upperBlockIndex <- c(upperBlockIndex, : missing value where TRUE/FALSE needed

elong0527 commented 1 year ago

please provide reproducible examples with a dummy dataset following https://reprex.tidyverse.org/index.html

riawoo commented 1 year ago

r2rtf package version is 1.01

structure(list(subjid = c("R001", "R001", "R001", "R001"), dosegrp.factor = structure(c(1L, 
1L, 1L, 1L), levels = "Active Control Group", class = "factor"), 
    visit = structure(c(1L, 1L, 1L, 1L), levels = "Screening", class = "factor"), 
    pecat = c("Complete", "Complete", "Complete", "Complete"), 
    pedat = structure(c(19278, 19278, 19278, 19278), class = "Date"), 
    petim = c("12:19", "12:19", "12:19", "12:19"), date_diff_ca = c(-14, 
    -14, -14, -14), petest_updated = c("Abdomen", "Chest", "Head / Neck / Ears / Eyes / Nose / Mouth / Throat", 
    "Heart / Circulation"), peorres = structure(c(1L, 1L, 1L, 
    1L), levels = "Normal", class = "factor"), pedesc = c(NA_character_, 
    NA_character_, NA_character_, NA_character_)), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame"))
#>   subjid       dosegrp.factor     visit    pecat      pedat petim date_diff_ca
#> 1   R001 Active Control Group Screening Complete 2022-10-13 12:19          -14
#> 2   R001 Active Control Group Screening Complete 2022-10-13 12:19          -14
#> 3   R001 Active Control Group Screening Complete 2022-10-13 12:19          -14
#> 4   R001 Active Control Group Screening Complete 2022-10-13 12:19          -14
#>                                      petest_updated peorres pedesc
#> 1                                           Abdomen  Normal   <NA>
#> 2                                             Chest  Normal   <NA>
#> 3 Head / Neck / Ears / Eyes / Nose / Mouth / Throat  Normal   <NA>
#> 4                               Heart / Circulation  Normal   <NA>

Created on 2023-03-15 with reprex v2.0.2

listing31_rtf <- PE_Data %>%
  rtf_page(
    orientation = "landscape"
  ) %>%
  rtf_page_header(
    text = c(paste0(PageHeader1), paste0(PageHeader2), paste0(PageHeader3)),
    text_justification = c("r", "l", "l"),
    text_font = 9,   # Courier New
    text_font_size = 8
  ) %>%
  rtf_page_footer(
    text = c("Notes:    Day relative to Challenge agent is the number of days relative to day of administration of the challenge agent, where day of administration = 0.",
             "",
             paste0("Printed: ", CurrentDate, " (", TFLVersion, ")")),
    text_font = 9,   # Courier New
    text_justification = "l",
    text_font_size = 8
  ) %>%
  rtf_title("Listing 31", "Physical Examination",
    text_font = 9,   # Courier New
    text_font_size = 8
  ) %>%
  rtf_colheader("Subject Number | Treatment Group | Visit | Type of Examination | Date of Exam | Time of Exam | Day Relative to Challenge Agent | Anatomical Site | Result | Description of Abnormality",
    col_rel_width = c(1,1,1,1,1,1,1,1.5,1,1.5),
    text_font = 9,   # Courier New
    text_justification = "c",
    text_font_size = 8,
    text_format = "b"
  ) %>%
  rtf_body(
    group_by = c("subjid", "dosegrp.factor", "visit", "pecat", "pedat", "petim", "date_diff_ca"), 
    col_rel_width = c(1,1,1,1,1,1,1,1.5,1,1.5),
    text_font = 9,   # Courier New
    text_justification = "c",
    text_font_size = 8,
    border_top = "single"
  )
#> Error in PE_Data %>% rtf_page(orientation = "landscape") %>% rtf_page_header(text = c(paste0(PageHeader1), : could not find function "%>%"

# Output .rtf file
listing31_rtf %>%
  rtf_encode() %>%                                                   
  write_rtf(file = "31_Listings_Physical_Examination.rtf")
#> Error in listing31_rtf %>% rtf_encode() %>% write_rtf(file = "31_Listings_Physical_Examination.rtf"): could not find function "%>%"

Created on 2023-03-15 with reprex v2.0.2

I hope this is all you need but let me know if you need anything else! I am pretty new to R and self-learning, so hope you can bear with me!

elong0527 commented 1 year ago

Thanks!

The error messages from the code is not the same as your original post. Could you load all proper packages and rerun the code? So we are able to see the exact errors message.

riawoo commented 1 year ago

I am not sure why "Could not find function "%>%" error messages get created when I run the reprex function. I think this has something to do with the reprex function and you can probably ignore these.

The error messages I get when running the rtf_encode() and write_rtf() is this: Error in if (nc[currentIndex] == 0L) upperBlockIndex <- c(upperBlockIndex, : missing value where TRUE/FALSE needed

elong0527 commented 1 year ago

I can reproduce the error now. The error message basically means you need to load the R package magrittr before using the %>% pipe function.

To reproduce the error, we need to add additional code before execute your code.

library(dplyr)
library(r2rtf)

PageHeader1 <- "a"
PageHeader2 <- "b"
PageHeader3 <- "c"
CurrentDate <- "d"
TFLVersion <- "e"
elong0527 commented 1 year ago

Here is the ad-hoc way to solve the bug.

With r2rtf v1.0.1, replace all missing values to an empty string using code below before you execute all other code.

PE_Data$pedesc <- ifelse(is.na(PE_Data$pedesc), "", PE_Data$pedesc)

I also fixed the bug in the PR #147 , but I need to submit a newer package version to CRAN before the bug disappear from your end. Otherwise, you need to use development version of the package.

riawoo commented 1 year ago

Yes, this worked! Thanks for your help.