【问题标题】:R shiny DT strange rendering issueR闪亮的DT奇怪的渲染问题
【发布时间】:2015-10-19 10:18:16
【问题描述】:

我的一个使用 DT 的应用程序遇到了一个奇怪的问题。我正在尝试使用 renderDataTable 呈现一个表,其中所有列名都将鼠标悬停在文本上。代码如下:

服务器.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 Studio 会话中,代码不会在第一次尝试中呈现表格,但会在第二次尝试中呈现它,而无需对代码进行任何修改。从发布的位置来看,尽管多次尝试,表格根本不会呈现。我不知道为什么 - 有什么帮助吗?

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

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

    我认为闪亮的版本已被弃用...

    【讨论】:

    • 我尝试使用DT::renderDataTable,但没有任何区别,因为ui.R 中的DT 库未加载。请参阅下面的答案:
    【解决方案2】:

    我认为 ui.R 中缺少 library(DT),在我在 library(shiny) 之后添加它之后,它工作正常。它在第二次尝试中在Rstudio 中工作的原因是因为那时DT 是从server.R 加载的,但由于ui.R 中缺少此语句而没有在第一次尝试中,我猜。

    【讨论】:

      猜你喜欢
      • 2021-06-17
      • 2015-01-11
      • 2014-02-15
      • 2014-02-20
      • 2021-06-19
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多