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

查看:82
本文介绍了R闪亮DT奇怪的渲染问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个使用DT的应用程序遇到了一个奇怪的问题。我正在尝试使用renderDataTable渲染表,所有列名称的鼠标悬停在文本上。下面是代码:

I'm having a strange issue with one of my applications using DT. I'm trying to render a table with renderDataTable with all column names have mouse over text. Here is the code:

.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") )
 ))

在新在Studio会话中,代码不会在第一次尝试中呈现表格,而是会在第二次尝试中呈现表格,而无需对代码进行任何修改。从发布位置开始,尽管进行了多次尝试,但表格完全没有呈现。我不知道为什么-有帮助吗?

In a new R studio session, the code doesn't render the table in the first attempt, but will render it in the 2nd attempt without any modifications to the code. From the published location, the table doesn't render at all despite multiple attempts. I cannot figure out why - any help?

推荐答案

我发现 library(DT)在ui.R中丢失,在我添加了 library(shiny)之后,它运行良好。它在第二次尝试中在 Rstudio 中工作的原因是因为那时 DT 是从 server.R ,但由于不是 ui.R 中缺少此语句,因此没有进行第一次尝试。

I figured that library(DT) is missing in ui.R and after I added that right after library(shiny) it worked fine. The reason that it worked in Rstudio in the 2nd try was because by then DT was loaded from server.R but was not in the first try because of lack of this statement in ui.R, I guess.

这篇关于R闪亮DT奇怪的渲染问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆