DT 包中的回车 R Shiny

问题描述 投票:0回答:2

有没有办法在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
2个回答
12
投票

这解决了问题:

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)

1
投票

这不是解决方案,而是@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)

问题显然是我通过容器指定列名称。事实证明,解决方案是在

escape = FALSE
函数中设置
tableHeader()
选项。

© www.soinside.com 2019 - 2024. All rights reserved.