R语言 如何在shiny中使用datatable通过索引/行名格式化一行?

vhmi4jdf  于 2024-01-03  发布在  其他
关注(0)|答案(3)|浏览(200)

我正在做一个闪亮的应用程序,我有麻烦,把一些风格和颜色。
我正在尝试给我的数据表添加一些颜色:我想给一个特定的行添加颜色。这一行的行名是“Sum”。
我还想给列“Sum”着色,我成功地做到了:所以我可以像这样给一个名为“Sum”的特定列着色:

  1. output$data_1<-renderDataTable(datatable(data(),options = list(dom = 't',pageLength=100))%>%formatStyle("Sum", backgroundColor = "orange")

字符串
但是我不知道我怎么能对我的行做同样的事情?
edit:我的“Sum”行并不总是数据的最后一行。
谢谢你的帮助!:)
编辑:
举个简单的例子:

  1. library(shiny)
  2. library(DT)
  3. 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"))
  4. # Define UI for application that draws a histogram
  5. ui <- fluidPage(
  6. # Application title
  7. titlePanel("Example"),
  8. dataTableOutput("table")
  9. )
  10. # Define server logic required to draw a histogram
  11. server <- function(input, output) {
  12. output$table <- renderDataTable(datatable(data_example)%>%formatStyle("Sum", backgroundColor = "orange"))
  13. }
  14. # Run the application
  15. shinyApp(ui = ui, server = server)


x1c 0d1x的数据
编辑:
多亏了akrun,我终于找到了一种方法,一个通用的表达式来处理我所有的表:

  1. output$table <- renderDataTable(datatable(data_example)%>%formatStyle("Sum", backgroundColor = "orange")
  2. %>%formatStyle(0, target="row",backgroundColor = styleEqual("Sum", "orange"))

pbpqsu0x

pbpqsu0x1#

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

  1. server <- function(input, output) {
  2. v1 <- row.names(data_example)
  3. cols1 <- ifelse(v1 =='Sum','orange','')
  4. output$table <- renderDataTable(datatable(data_example)%>%
  5. formatStyle(0, target = "row",
  6. backgroundColor = styleEqual(v1, cols1)))
  7. }
  8. shinyApp(ui = ui, server = server)

字符串

  • 输出
    x1c 0d1x的数据
展开查看全部
zxlwwiss

zxlwwiss2#

我的解决方案根据@akrun回答!^^

  1. output$table <- renderDataTable(datatable(data_example)%>%formatStyle("Sum", backgroundColor = "orange")
  2. %>%formatStyle(0, target="row",backgroundColor = styleEqual("Sum", "orange"))

字符串

xwbd5t1u

xwbd5t1u3#

这里有一个解决方案,如何突出显示粗体字的数据框中的某些行(mtcars作为一个例子)

  1. library(DT)
  2. # SERVER side
  3. df_mtcars <- reactive({
  4. mtcars$show <- (mtcars$mpg > 20)
  5. })
  6. # UI side
  7. output$dt_mtcars <- renderDataTable(
  8. df_mtcars() %>%
  9. datatable(
  10. rownames = FALSE,
  11. options = list(
  12. columnDefs = list(list(visible = FALSE, targets = c('show'))))) %>%
  13. formatStyle(
  14. 'show', target = 'row',
  15. fontWeight = styleEqual(c(1), c('bold'))))

字符串

展开查看全部

相关问题