首页 > 解决方案 > rhandsontable,将文本换行在列的单元格中(和自动行高)

问题描述

数据...

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

当我修复列宽和行高时,如何强制文本在单元格内换行(对于所有列还是部分列?)

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)

更好的是,我可以保留 rowHeight "auto"/default 并让它根据需要随文本换行扩展吗?

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)

请帮助并感谢

标签: rshinyrhandsontable

解决方案


既然你用我标记了这个问题,shiny我假设你将表格嵌入到一个闪亮的应用程序中。在这种情况下,您可以将表格嵌套在 a 中div并用于css设置表格样式以使用自动换行。

这是一个例子:

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)

推荐阅读