insightsengineering / tern

Table, Listings, and Graphs (TLG) library for common outputs used in clinical trials
https://insightsengineering.github.io/tern/
Other
77 stars 22 forks source link

`max_width` of `decorate_grob()` seems to be incorrectly handled #1240

Closed pawelru closed 5 months ago

pawelru commented 6 months ago

Please have a look at the below example

library(tern)
library(grid)

text <- "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

plot(1)
grid.newpage()
grid.draw(
    decorate_grob(
        NULL,
        titles = text,
        width_titles = unit(1, "npc"),
        footnotes = text,
        width_footnotes = unit(1, "npc")
    )
)

image

This produces a plot with title being outside of the plot range. It looks to me that the splitting mechanism is incorrectly handling the max width parameter. The value of unit(1, "npc") should indicate max viewport width but not beyond that.

Melkiades commented 6 months ago

@pawelru, there are margins that are correctly handled by the default grid::unit(1, "npc") - grid::unit(1.5, "cm"). I tried many options, and they seem to work fine. It is fighting a bit with rstudio when it comes to knowing what is the default width of viewport, but I think it is an rstudio issue. It works perfectly in base R and with files (@BFalquet). We can enhance the documentation, but I think for the general user, we can simply discourage the visual appreciation from rstudio and look at the file exports for these cases

In my case I get this for example in rstudio (same inputs)

Screenshot 2024-05-14 192331
pawelru commented 6 months ago

The fact that it's okey-ish for the default value does not change the fact that it cannot properly handle other values (e.g. the one I used in my example - unit(1, "npc")). There might be something with local viewport. I haven't checked create-export scenario (as opposed to create-draw one).

The longer story is that I somehow got into an email chain with the user complaining that this is not reproducible - one one machine it looked significantly different that on the other. That was for the default value you mentioned. But let's leave that user issue for a while and focus on the example i provided.

Melkiades commented 6 months ago

The issue should be fixed. For rstudio users the viewport width is not fixable as the conversion is off: https://github.com/rstudio/rstudio/issues/8559