2015-10-19 94 views
1

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

我试图代码在这里:

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

我希望第二个单元格“新行”在单独的行上包含单词“新”和“行”。

回答

2

这解决了问题:

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) 
+0

这不适合我。仍然没有换行符。 – Paul

+0

你可以创建一个可重复使用的版本吗? – easports611

+0

你是对的,对于隐晦的评论感到抱歉...我可以并将回来一个例子 – Paul

0

这不是一个解决方案,而是一个后续@ 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选项。