首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用formaStyle中的datatable按索引按行执行/rowname?

如何使用formaStyle中的datatable按索引按行执行/rowname?
EN

Stack Overflow用户
提问于 2021-12-10 17:02:14
回答 2查看 409关注 0票数 1

我正在做一个闪亮的应用程序,我有麻烦,以采取一些风格和颜色。

我正在尝试在datatable上添加一些颜色:我想给一个特定的行着色。此行的行名为"Sum“。

我还想将列"Sum“涂上颜色,并成功地做到了这一点:因此,我可以为一个名为"Sum”的特定列着色,如下所示:

代码语言:javascript
复制
output$data_1<-renderDataTable(datatable(data(),options = list(dom = 't',pageLength=100))%>%formatStyle("Sum", backgroundColor = "orange")

但我不知道我怎么能用我的争吵做同样的事?

编辑:我的“求和”行并不总是数据中的最后一行。

谢谢你的帮助!)

编辑:

一个简单的例子:

代码语言:javascript
复制
library(shiny)
library(DT)

data_example<-data.frame("A"=c(40,10,20,10,5,85),"B"=c(10,20,10,20,5,65),"Sum"=c(50,30,30,30,10,150), row.names = c("1","2","3", "4", "5", "Sum"))

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Example"),
    dataTableOutput("table")

)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$table <- renderDataTable(datatable(data_example)%>%formatStyle("Sum", backgroundColor = "orange"))
}

# Run the application 
shinyApp(ui = ui, server = server)

编辑:

多亏了akrun,我终于找到了一种通用的表达式,可以用我所有的表来完成它:

代码语言:javascript
复制
output$table <- renderDataTable(datatable(data_example)%>%formatStyle("Sum", backgroundColor = "orange")
                                    %>%formatStyle(0, target="row",backgroundColor =  styleEqual("Sum", "orange"))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-10 18:24:35

我们可以将行的索引指定为0formatStyle中,并使用styleEqual来匹配和替换创建的“cols1”

代码语言:javascript
复制
server <- function(input, output) {
  v1 <- row.names(data_example)
  cols1 <- ifelse(v1 =='Sum','orange','')
  
  output$table <- renderDataTable(datatable(data_example)%>%
        formatStyle(0, target = "row",
                            backgroundColor = styleEqual(v1, cols1)))
}


shinyApp(ui = ui, server = server)

-output

票数 1
EN

Stack Overflow用户

发布于 2021-12-10 18:55:36

我的答案是@akrun答案!^^

代码语言:javascript
复制
  output$table <- renderDataTable(datatable(data_example)%>%formatStyle("Sum", backgroundColor = "orange")
                                        %>%formatStyle(0, target="row",backgroundColor =  styleEqual("Sum", "orange"))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70308017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档