0
votes

I'm trying to make a shiny app that displays a datatable (using DT::renderDataTable) that displays comments and their sentiments. When the sentiment column (sent_score) says "Positive", I want that cell of sent_score highlighted green. When it says "Negative", I want that cell highlighted red.

If possible, I'd also be interested if there's a way to make the entire row green or red depending on if the sent_score is Positive or Negative as well.

Below is a simplified version of the dashboard's code. The problem, I think, is in the output$comments portion of the code. Thank you for your help!!

 #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
It would be easier to help you if you provide the data (use dput).Stéphane Laurent

1 Answers

0
votes

You have to create a variable signscore which will be -1 or 1 depend of sent_score <0 or >0, and after :

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