我的一个使用DT的应用程序遇到了一个奇怪的问题。我正在尝试使用renderDataTable呈现一个表,所有列名都有鼠标悬停在文本上。这是代码:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" id="app">
<my-directive-name my-attribute="foo"></my-directive-name>
</div>
.libPaths("/usr/lib64/R/library")
library(shiny) # 0.12.1
library(data.table) # 1.9.4
library(DT) # 0.1
options(DT.options = list(pageLength = 5,lengthMenu = c(5,10, 25, 100),orderClasses=TRUE))
rb <- fread("r1.csv")
## r1.csv has the following data(subset) :
# "c1","c2","c3","c4"
# "10011","7","999999","3"
# "10597","6","114182","1"
# "20101","7","999999","3"
# "20102","7","999999","3"
non_factor_columns<-names(rb)[!names(rb) %in% c("c1","c3")]
rb[,(non_factor_columns):=lapply(.SD, as.factor),.SDcols=non_factor_columns]
cols<-c("c1","c2","c3","c4")
labels<-c("This is column 1","This is column 2","This is column 3","This is column 4")
rbcollabels<-data.table(cols,labels)
setkey(rbcollabels,cols)
prefcols<-c("c1","c3")
setcolorder(rb,union(prefcols,colnames(rb)[!colnames(rb) %in% prefcols]))
r<-as.data.table(colnames(rb))
gotlabels<-rbcollabels[r]
collabelstr<- paste0("thead(tr(",paste0("th('",gotlabels$cols,"'",",title=","'",gotlabels$labels,"')",collapse=","),"))")
shinyServer(function(input, output) {
sketch = htmltools::withTags(table(
class = 'display',eval(parse(text=collabelstr))
)
)
output$table1 <- renderDataTable({
datatable(rb, options=list(dom='C<"clear">Rlrtip',colVis = list(activate = 'mouseover', restore = 'Restore', showAll= 'Show all', showNone= "Show none" )),
rownames=F,container=sketch,filter='top',extensions = c('ColVis','ColReorder')
)
})
})
在新的R studio会话中,代码不会在第一次尝试时呈现表,但会在第二次尝试时呈现它,而不对代码进行任何修改。从发布的位置开始,尽管多次尝试,该表仍然不会呈现。我无法弄清楚为什么 - 任何帮助?
答案 0 :(得分:2)
我认为ui.R中缺少library(DT)
,并且在我library(shiny)
之后添加它之后它运行正常。它在第二次尝试Rstudio
中工作的原因是因为那时DT
已从server.R
加载但由于ui.R
中缺少此语句而未在第一次尝试中}, 我猜。
答案 1 :(得分:1)
尝试使用DT::renderDataTable()
和DT::dataTableOutput()
版本。
我认为闪亮的版本已被弃用......