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

vhmi4jdf  于 5个月前  发布在  其他
关注(0)|答案(3)|浏览(59)

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

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

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

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)


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

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

pbpqsu0x

pbpqsu0x1#

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

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)

字符串

  • 输出
    x1c 0d1x的数据
zxlwwiss

zxlwwiss2#

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

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

字符串

xwbd5t1u

xwbd5t1u3#

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

library(DT)

# SERVER side
df_mtcars <- reactive({
  mtcars$show <- (mtcars$mpg > 20)
})

# UI side
output$dt_mtcars <- renderDataTable(
  df_mtcars() %>% 
    datatable(
      rownames = FALSE,
      options = list(
        columnDefs = list(list(visible = FALSE, targets = c('show'))))) %>% 
    formatStyle(
      'show', target = 'row',
      fontWeight = styleEqual(c(1), c('bold'))))

字符串

相关问题