【问题标题】:color code cells in DT::RenderDataTable in ShinyShiny 中 DT::RenderDataTable 中的颜色代码单元格
【发布时间】:2019-05-08 14:51:53
【问题描述】:

我正在尝试制作一个显示 cmets 及其情绪的数据表(使用 DT::renderDataTable)的闪亮应用程序。当情绪列 (sent_score) 显示“积极”时,我希望 sent_score 的那个单元格突出显示为绿色。当它显示“否定”时,我希望该单元格以红色突出显示。

如果可能的话,如果有办法根据 sent_score 是 Positive 还是 Negative 使整行变为绿色或红色,我也会感兴趣。

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

 #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)

【问题讨论】:

  • 如果您提供数据会更容易帮助您(使用dput)。

标签: r colors shiny dashboard dt


【解决方案1】:

您必须创建一个变量signscore,它将是 -1 或 1,取决于 sent_score 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

【讨论】:

    猜你喜欢
    • 2020-06-24
    • 2018-08-16
    • 1970-01-01
    • 2017-01-21
    • 2017-02-22
    • 2018-08-07
    • 2015-09-13
    • 2020-08-13
    相关资源
    最近更新 更多