我正在尝试制作一个漂亮的应用程序,它可以显示一个数据表(使用DT::renderDataTable),该数据表可以显示评论和他们的情绪。当sentiment列( sent_score )显示“积极”时,我希望sent_score的单元格突出显示为绿色。当它显示“否定”时,我希望该单元格突出显示为红色。
如果可能的话,我也想知道是否有一种方法可以根据sent_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)
发布于 2018-12-07 07:59:56
您必须创建一个取决于sent_score <0或>0的变量signscore
,它将是-1或1,并且在此之后:
DT::renderTable(
DT::datatable( mydatatable)
%>% formatStyle(
columns = c("signscore"),
valueColumns = c("signscore"),
target='row',
backgroundColor =
styleEqual(c(-1,1),
c('green','red'))
)
)
https://stackoverflow.com/questions/53659354
复制相似问题