【问题标题】:Carriage returns in DT package R ShinyDT 包 R Shiny 中的回车
【发布时间】:2016-01-17 21:51:27
【问题描述】:

有没有办法在 R 闪亮的应用程序中使用 DT 包显示回车?

我已经尝试过这里的代码:

library(DT)

# create data frame with an example column
test_df <- data.frame(SAME =  "Same Line", NEW = "New\nLine")

# print data frame
datatable(test_df)

\n 表示法不起作用,似乎datatable 函数用空格替换了\n

我希望第二个单元格“新行”在不同的行上有“新”和“行”字样。

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    这解决了问题:

    library(DT)
    
    # create data frame with an example column
    test_df <- data.frame(SAME =  "Same Line", NEW = "New\nLine")
    
    # replace \n with <br/>
    test_df$NEW <- gsub(pattern = "\n", replacement = "<br/>", x = test_df$NEW)
    
    # print data frame 
    # with escape set to FALSE
    datatable(test_df, escape = FALSE)
    

    【讨论】:

    • 你能不能创建一个可重现的版本?
    • 你是对的,很抱歉这个含糊的评论......我可以并且会用一个例子回来
    • 我不确定 'escape = F' 的作用。在我自己的应用程序中,当我添加 escape = false 时,它​​什么都不返回。以下返回一个空数据表。 x 是带有行名的 df 单列。 datatable(x, colnames = NULL, selection = "single", escape = TRUE, options = list(pageLength = -1, info = FALSE, dom='t', autoWidth = TRUE, columnDefs = list(list(width = ' 300px', 目标 = list(1)))) )
    【解决方案2】:

    这不是解决方案,而是@easports611 评论的后续行动。以下是答案不起作用的应用程序:

    server <- function(input, output, session) {
      library(data.table)
      data("iris")
        output$buySegments <- DT::renderDataTable({
          colnames(iris)=c("a <br>b","c<br>d","e<br>f","g<br>h","i")
          sketch<-htmltools::withTags(table(
            tableHeader(iris
          )))
          #rangeRatio
          thing = DT::datatable(
            iris
            ,rownames = FALSE
            ,container = sketch
          )
          return(thing)
        }
        )  
    }
    
    
    ui=shinyUI(
      fluidPage(theme = "bootstrap.css",title = "Buyer Console",
                mainPanel(  
                  DT::dataTableOutput('buySegments') 
                )
      )
    )
    
    shinyApp(ui = ui, server = server)
    

    问题显然是我通过容器指定列名这一事实。原来解决办法是在tableHeader函数中设置escape=F选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 2016-05-22
      • 2020-10-28
      • 2021-12-13
      • 2018-02-06
      • 2020-11-08
      • 2021-11-20
      相关资源
      最近更新 更多