首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Shiny中DT::RenderDataTable中的颜色代码单元格

Shiny中DT::RenderDataTable中的颜色代码单元格
EN

Stack Overflow用户
提问于 2018-12-07 04:42:04
回答 1查看 950关注 0票数 0

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

如果可能的话,我也想知道是否有一种方法可以根据sent_score是正数还是负数来使整行变绿或变红。

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

代码语言:javascript
复制
 #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)
EN

回答 1

Stack Overflow用户

发布于 2018-12-07 07:59:56

您必须创建一个取决于sent_score <0或>0的变量signscore,它将是-1或1,并且在此之后:

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53659354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档