dbosak01 / reporter

An R package to write statistical reports.
Creative Commons Zero v1.0 Universal
16 stars 3 forks source link

The width parameter of a function definition is not working for certain variables. #295

Closed bimalthomas closed 1 year ago

bimalthomas commented 1 year ago

I made a table with 9 columns, but when I exported it in RTF format, it split into two pages. To fix this, I tried adjusting the width of each variable in the define function multiple times, but the width of some variables remained too wide and couldn't be reduced, only increased.

dbosak01 commented 1 year ago

The function won't break words or numbers. It will only on break spaces. That might be the issue. Let me know if that is it or not. You can force a wrap with "\n".

On Tue, Mar 28, 2023, 8:34 AM Bimal Thomas @.***> wrote:

I have created a listing table with 9 columns, but the rtf table output broke into two pages, so I adjusted the width of each variable in the define function multiple times, but the width of certain variables never changed.

— Reply to this email directly, view it on GitHub https://github.com/dbosak01/reporter/issues/295, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJ6SCZTLMANRCD74PMOCYZDW6LLF7ANCNFSM6AAAAAAWKQEOC4 . You are receiving this because you are subscribed to this thread.Message ID: @.***>

bimalthomas commented 1 year ago

That's correct, it will only break at spaces.

dbosak01 commented 1 year ago

Can you send me a reproducible example? Even a screen shot of your report would help.

bimalthomas commented 1 year ago

TEST_1

The screenshot indicates that the width of the columns named "Reported Term" and "Preferred Term" cannot be decreased beyond a specific limit. It is assumed that this limitation is due to the presence of the term "ESOPHAGOGASTRODUODENOSCOPY" in one of the columns, resulting in the table being split across two pages. Is there a possible solution to split or shorten this term in order to avoid this problem?

dbosak01 commented 1 year ago

Bimal:

I've had someone else ask for this. One problem is there are no functions in base R or any package that will do it. The next problem is that it is not clear exactly what the user will want. Some people might want it broken at a certain length. Other people will want it broken on a syllable boundary. So I've avoided doing anything and left it up to the user to manipulate the data the way they want. Here is a function that will do it for you at a specified length:

# Sample data
dat <- read.table(header = TRUE, text = '
SUBJ     AGEGEN   REPTRM           PREFTERM             SOC          SDT         EDT
ABC-A01  "46/M"   ADENOIDECTOMY    Adenoidectomy        ""           UNUNK1996   UNUNK1996
ABC-A01  "46/M"   "CODEINE ALLERGY" "Drug hypersensitivity"       "" UNUNK1974   Ongoing
ABC-A01  "46/M"   "ESOPHGOGASTRODUODENOSCOPY" "Oesophagogastroduodenoscopy" "" UNUNK2010   UNUNK2010
ABC-A01  "46/M"   "Gestroesophageal Reflux Disease" "Gastroesophageal reflux disease" "" UNUNK1974   Ongoing')

library(stringi)

spltdata <- function(vect, sz) {

  # Split into list of vectors
  clls <- strsplit(vect, " ", fixed = TRUE)

  # Cycle through cells
  for (i in seq_along(clls)) {

    tmp <- clls[[i]]

    # Replace long words
    for (j in seq_along(tmp)) {

      # Insert space if word is over sz
      if (nchar(tmp[[j]]) > sz) {

        stri_sub(tmp[[j]], sz, sz - 1) <- " "

      } 
    }

    # Collapse each cell back into single string
    clls[[i]] <- paste(tmp, collapse = " ")
  }

  # Collapse list into vector of strings
  ret <- paste(clls)

  return(ret)
}

# Copy dataset
dat2 <- dat

# Adjust desired columns
dat2$REPTRM <-  spltdata(dat2$REPTRM, 20)
dat2$PREFTERM <-  spltdata(dat2$PREFTERM, 20)

# View Original
dat

# View Modified
dat2

Try and see if this will work for you. You would need to call this for each column you want adjusted.

dbosak01 commented 1 year ago

Related to #124

bimalthomas commented 1 year ago

Great Thanks