5

Data...

DF <- data.frame(a = c("hi", rep(NA, 4)),
                 b = letters[1:5],
                 c = LETTERS[1:5],
                 stringsAsFactors = FALSE)

When I fix the column widths and the row heights, how can i force the text to wrap within cells (either for all or a subset of columns?)

rhandsontable(DF, stretchH = "all", height = 300 ) %>%
  hot_cols(colWidths = c(100, 50, 50),
           manualColumnMove = FALSE,
           manualColumnResize = TRUE
           ##, wordWrap = "yes please"
           ) %>%
  hot_rows(rowHeights = 75 ) %>%
  hot_context_menu(allowRowEdit = TRUE, allowColEdit = FALSE)

Even better, can I leave the rowHeight "auto"/default and let it expand as needed with the text wrapping?

rhandsontable(DF, stretchH = "all", height = 300 ) %>%
  hot_cols(colWidths = c(100, 50, 50),
           manualColumnMove = FALSE,
           manualColumnResize = TRUE
           ##, wordWrap = "yes please"
  ) %>%
  hot_rows(rowHeights = NULL ) %>% #default
  hot_context_menu(allowRowEdit = TRUE, allowColEdit = FALSE)

Please help and thanks

bikeactuary
  • 447
  • 4
  • 18
  • 2
    The relevant parameter `wordWrap` seems to be true by default: https://handsontable.com/docs/7.4.2/Options.html#wordWrap, but not used in R. Therefore, i would file an issue at Github: https://github.com/jrowen/rhandsontable/issues?q=wordwrap. – Tonio Liebrand May 18 '20 at 16:15

1 Answers1

3

Since you tagged the question with shiny I'm assuming you're embedding the table in a shiny app. That being the case, you can nest the table in a div and use css to style the table to use word wrap.

Here is an example:

DF <- data.frame(
  a = c("hi", rep(NA, 4)),
  b = sapply(1:5, function(x) paste(sample(letters, size=x*5), collapse='')),
  c = LETTERS[1:5],
  stringsAsFactors = FALSE
)

library(shiny)  
ui <- fluidPage(
  tags$style('#myid * { word-wrap: break-word; color: blue }'),  # apply styling to children of myid
  div(id='myid', rHandsontableOutput('tbl'))
)
server <- function(input, output, session) {
  output$tbl <- renderRHandsontable({
    rhandsontable(DF, stretchH = "all", height = 300 ) %>%
      hot_cols(colWidths = c(100, 50, 50),
               manualColumnMove = FALSE,
               manualColumnResize = TRUE
               ##, wordWrap = "yes please"
      ) %>%
      hot_rows(rowHeights = NULL ) %>% #default
      hot_context_menu(allowRowEdit = TRUE, allowColEdit = FALSE)
  })
}
shinyApp(ui, server)
bobbel
  • 1,983
  • 6
  • 21