2015-10-19 91 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選項。