更改Shiny table中的字体颜色

时间:2018-09-11 04:00:10

标签: r shiny

使用tableOutput更改单行数据框的字体颜色的最简单方法是什么。具体来说,如何将“右”下的“ 7”更改为绿色。

library(shiny)

shinyApp(
  ui = fluidPage(

    sidebarLayout(
      sidebarPanel(
      fluidRow(  
         tableOutput("view")
        )
      ),

      mainPanel(
      ))),

  server = function(input, output, session){

    correct <- reactiveValues(num = 7)
    wrong <- reactiveValues(num = 4)   
    skipped <- reactiveValues(num = 9)

    togo = 80

    output$view <- renderTable(tibble(
      Right = correct$num,
      Wrong = wrong$num,
      Skipped = skipped$num,
      ToGo = togo
    ), spacing = "xs")
  }
)

1 个答案:

答案 0 :(得分:1)

在这种情况下,最好使用DT随附的renderDataTable以获得更好的格式。

library(shiny)
library(tidyverse)
library(DT)

shinyApp(
  ui = fluidPage(

    sidebarLayout(
      sidebarPanel(
        fluidRow(  
          column(8,dataTableOutput("view"))
        )
      ),

      mainPanel(
      ))),

  server = function(input, output, session){

    correct <- reactiveValues(num = 7)
    wrong <- reactiveValues(num = 4)   
    skipped <- reactiveValues(num = 9)

    togo = 80

    output$view <- renderDataTable(datatable(tibble(
      Right = correct$num,
      Wrong = wrong$num,
      Skipped = skipped$num,
      ToGo = togo
    )) %>% formatStyle("Right",color=styleEqual(7, "red")) ) 
  }
)

仅显示表格:

library(shiny)
library(tidyverse)
library(DT)

shinyApp(
  ui = fluidPage(

    sidebarLayout(
      sidebarPanel(
        fluidRow(  
          column(8,dataTableOutput("view"))
        )
      ),

      mainPanel(
      ))),

  server = function(input, output, session){

    correct <- reactiveValues(num = 7)
    wrong <- reactiveValues(num = 4)   
    skipped <- reactiveValues(num = 9)

    togo = 80

    output$view <- renderDataTable(datatable(tibble(
      Right = correct$num,
      Wrong = wrong$num,
      Skipped = skipped$num,
      ToGo = togo
    ), options = list(dom = 't')) %>% formatStyle("Right",color=styleEqual(7, "red")) ) 
  }
)