简体   繁体   English

R Shiny DT 渲染文本和输入没有换行符

[英]R Shiny DT render text and input without linebreaks

I am trying to render any kind of R Shiny input in a Shiny DT, however I would like to avoid the linebreaking.我正在尝试在 Shiny DT 中渲染任何类型的 R Shiny 输入,但是我想避免换行。 If I concatenate some text and the html tags with the shinyInput function both the text and the inputs are rendered, but a linebreak happens before and after the input.如果我将一些文本和 html 标签与 shinyInput 函数连接起来,则文本和输入都会被渲染,但在输入之前和之后会发生换行符。

I think that the root cause for this is the div tag, but googling it seems that adding the style="display:inline;"我认为根本原因是 div 标签,但谷歌搜索似乎添加了 style="display:inline;" css code should solve it, but it doesn't, it even breaks the width definition. css 代码应该解决它,但它没有,它甚至破坏了宽度定义。

do you have any idea how to get the text before, the div, and the text after on the same cell?你知道如何在同一个单元格上获取之前的文本、div 和之后的文本吗?

below some code to play with.下面是一些可以玩的代码。

library(DT)

ui <- basicPage(
  h2("The mtcars data"),
  DT::dataTableOutput("mytable")
)

server <- function(input, output) {
  
  shinyInput <- function(FUN, len, id, ...) {
    inputs <- character(len)
    for (i in seq_len(len)) {
      inputs[i] <- as.character(FUN(paste0(id, i), ...))
    }
    inputs
  }
  
  mtcarsx <- data.frame(mtcars, newvar=
                         paste0(
                           "tex before "
                           ,shinyInput(checkboxInput,nrow(mtcars),"mychbx",label="",value=FALSE,width=NULL),
                           " text after"))

  
  output$mytable = DT::renderDataTable({
    DT::datatable(mtcarsx, 
                  escape = FALSE, 
                  selection = 'none', 
                  rownames = FALSE, 
                  extensions = 'RowGroup', 
                  options = list(searching = FALSE, 
                                 ordering  = FALSE,
                                 rowGroup = list(dataSrc=c(1)),
                                 columnDefs = list(list(visible=FALSE, targets=c(1)))
                                 ))
  })
  
}

shinyApp(ui, server)

You are right, you need to change the divs that wrap the checkbox element to display:inline .没错,您需要将包装复选框元素的 div 更改为display:inline You say that this doesn't solve it as it breaks the width definition.你说这并不能解决它,因为它破坏了宽度定义。 Perhaps I'm missing something?也许我错过了什么? I do not see a change in the widths column.我没有看到宽度列的变化。

tags$style("
             #mytable tr td div.form-group {
               display: inline;
             }
             #mytable tr td div.checkbox {
               display: inline; 
             }
             #mytable tr td div.checkbox label {
               padding: 0;
             }
             #mytable tr td div.checkbox input {
               position: relative;
               margin: 0;
             }")

If the text that goes before and after are constant, you could use the css pseudo classes after and before to add that content.如果之前和之后的文本是不变的,您可以在afterbefore使用 css 伪类来添加该内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM