闪亮的DT :: RenderDataTable中的颜色代码单元

时间:2018-12-06 20:42:04

标签: r colors shiny dashboard dt

我正在尝试制作一个闪亮的应用程序,该应用程序显示一个数据表(使用DT :: renderDataTable),该数据表显示注释及其情感。当情感列(sent_score)说“正”时,我希望send_score的单元格突出显示为绿色。当它说“负”时,我希望该单元格突出显示为红色。

如果可能的话,我也很想知道是否有一种方法可以使整个行变成绿色或红色,这取决于send_score是正数还是负数。

下面是仪表板代码的简化版本。我认为问题出在代码的output $ comments部分。谢谢您的帮助!

 #Read in packages
 library(readxl)
 library(tools)
 library(dplyr)
 library(shiny)
 library(DT)


 #Read in Data

 fakeshinydata


#####Build the UI for the app####

ui <- fluidPage(
  sidebarLayout(
sidebarPanel(
  h4("Variables"),
  selectInput(inputId = "x",
              label = "Predictor:",
              choices = c("ID", "ave_sentiment", "location", "title", "job_sat", "motivation", "commitment", "review"),
              selected = "ID"),
  selectInput(inputId = "y",
              label = "Outcome:",
              choices = c("sale", "ave_sentiment", "location", "title", "job_sat", "motivation", "commitment", "review"),
              selected = "sale"),
mainPanel(
          DT::dataTableOutput(outputId = "comments")
)
)
 ))

#####

#####Connect to the server for the app####

 server <- function(input, output) {

 fake_subset_1 <- reactive({
fakeshinydata
  })

  output$comments <- DT::renderDataTable({
   fake_subset_1() %>%
      select(input$x, input$y, comment, sent_score, com_date)
    })
   }

 shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

您必须创建一个变量signscore,该变量应为-1或1,取决于send_score <0或> 0,以及之后:

DT::renderTable(
  DT::datatable( mydatatable)
  %>% formatStyle( 
             columns = c("signscore"), 
             valueColumns = c("signscore"), 
             target='row', 
             backgroundColor = 
               styleEqual(c(-1,1), 
               c('green','red')) 
      ) 
) 

https://rstudio.github.io/DT/010-style.html