-->

[R闪亮DT奇怪的渲染问题(R shiny DT strange rendering issue)

2019-10-24 06:54发布

我在与使用DT我的应用程序一个一个奇怪的问题。 我试图渲染renderDataTable表的所有列名称鼠标移到文本。 下面是代码:

server.R

.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')
 )
})
})

ui.R

.libPaths("/usr/lib64/R/library")
 library(shiny)
 shinyUI(fluidPage(
  tags$head( tags$style("#table1 {color: blue; }")),
  tags$head( tags$style("#table1 th {background-color: yellow; }")),
  tags$head( tags$style("#table1 td,th {border: thin solid gray; }")),
  tags$head( tags$style("#table1 tr {background-color: Gainsboro;   }")),
  tags$head( tags$style("#table1 tr:nth-child(odd) {background-color: Lavender; }")),
  tags$head( tags$style("#table1 th:hover  {color: red;  }")),
  title = "Test Data",
  h3("Test Data ",align="center",style="color:red"),
  mainPanel( dataTableOutput("table1") )
 ))

在一个新的R演播室会议上,代码不呈现表中的第一次尝试,但会使其在没有任何修改代码的第二次尝试。 从发布位置,表中没有的一切,尽管多次尝试渲染。 我想不通为什么 - 任何帮助吗?

Answer 1:

我想, library(DT)中缺少ui.R后,我补充说,之后library(shiny)它工作得很好。 它在工作的原因Rstudio在第二个尝试是因为那时DT从加载server.R但不是在第一次尝试的,因为缺乏这句话的ui.R ,我猜。



Answer 2:

尝试使用DT::renderDataTable()DT::dataTableOutput()版本。

我认为,有光泽的版本已被弃用...



文章来源: R shiny DT strange rendering issue
标签: r shiny dt