简体   繁体   English

根据分类值有条件地为htmlTable中的单元格着色。 该表根据用户输入动态显示(r Shiny0

[英]Conditionally colour cells in an htmlTable based on categorical values. Table displays dynamically on user input (r shiny0

I've created an app that allows user to select an electoral division and then a table is updated showing all the political parties which had been voted into power over the years. 我创建了一个应用程序,允许用户选择一个选举部门,然后更新一个表格,其中显示了多年来已被投票通过的所有政党。

I would like to color the cells containing the political party abbreviations with the corresponding party color. 我想用相应的政党颜色给包含政党缩写的单元格上色。 image of example table showing colored cells based on categorical value (party abbreviation) 示例表的图像,该图像显示了基于分类值的彩色单元格(缔约方的缩写)

 Example data:
     Elect_div<-c("Blair","Bonner","Bowman","Brisbane","Capricornia","Dawson","Dickson")
     Elected_party_2016<-c("ALP","LNP","LNP","LNP","LNP","LNP","LNP)
     Elected_party_2013<-c("ALP","LNP","LNP","LNP","LNP","LNP","LNP)

     df.party <- data.frame(Elect_div, Elected_party_2016, Elected_party_2013)
     # i need the table in long format as the real data goes back to 2004 and 
     the table displays below a map and some graphs
     df.melted<-melt(df.party, idvars="Elect_div", measure.vars=c("Elected_party_2016", "Elected_party_2013"))
     #removing varible column as I am manually setting the row names within the htmlTable below
     df.melted$variable <- NULL
     #bring the valu column to the first position
     df.melted<-df.melted[,c(ncol(df.melted),1:(ncol(df.melted)-1))]

     #shiny app

     ui<- fluidPage(
             selectInput("division", "",
          label="Select an electorate, graphs below will be updated.",
          choices = df.melted$Elect_div),
          htmlOutput("table"))

     server <- function(input, output, session) {  
           selectedData<-eventReactive(df.melted$Elect_div==input$division,  {

           HTML(
             htmlTable(subset(df.melted,df.melted$Elect_div==input$division), 
             align="l",
             header=c("",""),
             rnames= paste(c("Party elected 2016","Party elected 2013")), 
            caption="Historic elected party data from the Australian Electoral Commission (AEC)",
             tfoot="&dagger;Participation & Unemployment rate calculated using data from Australian Government Dept of Jobs & Small Business (2018)"

              ))

                    })
                 output$table <- renderUI({selectedData()})

                      }

                  shinyApp(ui, server)

Now my question is how do I set the background colour of the cells to match the party colors if: 现在我的问题是,如果出现以下情况,如何设置单元格的背景颜色以匹配聚会颜色:

                party_cols<-c("LNP"="blue","ALP" = "red","IND" = "grey", "KAP = "purple")

I've tried a bunch of different options based on what I have read here but none work (kable, col.rgroup, background = , cell_apec). 根据我在这里阅读的内容,我尝试了很多不同的选择,但没有任何效果(kable,col.rgroup,background =和cell_apec)。

Thanks in advance 提前致谢

Is it what you want ? 是你想要的吗?

在此处输入图片说明

Elect_div<-c("Blair","Bonner","Bowman","Brisbane","Capricornia","Dawson","Dickson")
Elected_party_2016<-c("ALP","LNP","LNP","LNP","LNP","LNP","LNP")
Elected_party_2013<-c("ALP","LNP","LNP","LNP","LNP","LNP","LNP")

df.party <- data.frame(Elect_div, Elected_party_2016, Elected_party_2013)
# i need the table in long format as the real data goes back to 2004 and the table displays below a map and some graphs
df.melted<- reshape2::melt(df.party, idvars="Elect_div", measure.vars=c("Elected_party_2016", "Elected_party_2013"))
#removing varible column as I am manually setting the row names within the htmlTable below
df.melted$variable <- NULL
#bring the valu column to the first position
df.melted<-df.melted[,c(ncol(df.melted),1:(ncol(df.melted)-1))]

party_cols<-c("LNP"="blue","ALP" = "red","IND" = "grey", "KAP" = "purple")

library(shiny)
library(htmlTable)

ui<- fluidPage(
  selectInput("division", "",
              label="Select an electorate, graphs below will be updated.",
              choices = df.melted$Elect_div),
  htmlOutput("table"))

server <- function(input, output, session) {  
  selectedData<-eventReactive(df.melted$Elect_div==input$division,  {
    dat <- subset(df.melted,df.melted$Elect_div==input$division)
    party <- dat$value[1]
    HTML(
      htmlTable(
        dat, 
        align="l",
        header=c("",""),
        rnames= paste(c("Party elected 2016","Party elected 2013")), 
        css.cell = rep(sprintf("background-color: %s;", party_cols[party]), 2),
        caption="Historic elected party data from the Australian Electoral Commission (AEC)",
        tfoot="&dagger;Participation & Unemployment rate calculated using data from Australian Government Dept of Jobs & Small Business (2018)"
      ))

  })
  output$table <- renderUI({selectedData()})

}

shinyApp(ui, server)

UPDATE UPDATE

Elect_div<-c("Blair","Bonner","Bowman","Brisbane","Capricornia","Dawson","Dickson")
Elected_party_2016<-c("ALP","LNP","LNP","LNP","LNP","LNP","LNP")
Elected_party_2013<-c("ALP","KAP","LNP","LNP","LNP","LNP","LNP")

df.party <- data.frame(Elect_div, Elected_party_2016, Elected_party_2013)
# i need the table in long format as the real data goes back to 2004 and the table displays below a map and some graphs
df.melted<- reshape2::melt(df.party, idvars="Elect_div", measure.vars=c("Elected_party_2016", "Elected_party_2013"))
#removing varible column as I am manually setting the row names within the htmlTable below
df.melted$variable <- NULL
#bring the valu column to the first position
df.melted<-df.melted[,c(ncol(df.melted),1:(ncol(df.melted)-1))]

party_cols<-c("LNP"="blue","ALP" = "red","IND" = "grey", "KAP" = "purple")

library(shiny)
library(htmlTable)

ui<- fluidPage(
  selectInput("division", "",
              label="Select an electorate, graphs below will be updated.",
              choices = df.melted$Elect_div),
  htmlOutput("table"))

server <- function(input, output, session) {  
  selectedData<-eventReactive(df.melted$Elect_div==input$division,  {
    dat <- subset(df.melted,df.melted$Elect_div==input$division)
    HTML(
      htmlTable(
        dat, 
        align="l",
        header=c("",""),
        rnames= paste(c("Party elected 2016","Party elected 2013")), 
        css.cell = t(vapply(party_cols[dat$value], function(x) rep(sprintf("background-color: %s;", x), 2), character(2))),
        caption="Historic elected party data from the Australian Electoral Commission (AEC)",
        tfoot="&dagger;Participation & Unemployment rate calculated using data from Australian Government Dept of Jobs & Small Business (2018)"
      ))

  })
  output$table <- renderUI({selectedData()})

}

shinyApp(ui, server)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R 闪亮的绘图颜色基于单独数据框上的值有条件地 - R shiny plot colour conditionally based on values on separate data frame 在闪亮的应用程序中基于用户输入(使用预定义的基于分类的调色板)的彩色传单多边形 - Colour leaflet polygons based on user input (using predefined categorical based palette) in shiny app 表格为 output 基于 R shiny 中的用户输入 - Table as output based on user input in R shiny 有条件地替换R中的类别值 - Conditionally replace categorical values in R 有条件地为htmlTable中的单元格着色 - Conditionally coloring a cell in htmlTable for R 根据用户输入动态子集数据帧,并将结果表显示为 Shiny 应用程序中的另一个用户输入 - Subsetting a dataframe dynamically based on user input and presenting the resulting table as another user input in a Shiny app 根据Shiny中的用户输入动态更改图 - Dynamically change plots based on user input in Shiny R 闪亮有条件地改变 numericInput 背景颜色 - R shiny conditionally change numericInput background colour r Shiny 必须将用户输入返回到有条件构建的小部件中 - r Shiny has to return user input into a conditionally built widget 在 Shiny R 中,根据用户选择有条件地渲染 html - In Shiny R render html conditionally based on user selection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM