R 闪亮颜色数据框 [英] R shiny color dataframe

查看:20
本文介绍了R 闪亮颜色数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框:

   runApp(
      list(ui = bootstrapPage(pageWithSidebar(
        headerPanel("Data frame with colors"),
        sidebarPanel(),
        mainPanel(
           tableOutput("my_dataframe")
        ) 
      )
     )
   ,
    server = function(input, output) {
       output$my_dataframe <- renderTable({ 
               data.frame("Brand ID"=1:4,"Client1"=c("red", "green", "green", "green"),
                                         "Client2"=c("green", "red", "green", "red")) 
       }) 
    }
)
)

是否可以为数据框着色,例如:

Is it possible to color data frame like:

例如,当我有 contidion1 时,我需要将数据框单元格着色为红色,条件 2 - 为绿色.

For example, when I have contidion1 I need to color data frame cell with red, on condition2 - with green.

任何帮助将不胜感激.

推荐答案

这里有一个解决方案.要使用它,您必须在向量中定义 css:

Here is a solution. To use it, you have to define css in a vector:

css <- c("#bgred {background-color: #FF0000;}",
          "#bgblue {background-color: #0000FF;}")

并在单元格内写入 #... :

and write #... inside the cell :

> data.frame(x=c("A","B"), y=c("red cell #bgred", "blue cell #bgblue"))
  x                 y
1 A   red cell #bgred
2 B blue cell #bgblue

然后使用我的 colortable() 函数,主要灵感来自 highlightHTML 包和我个人的闪亮经验.下面是一个例子:

Then use my colortable() function mainly inspired from the highlightHTML package and from my personal shiny experience. Here is an example:

library(pander)
library(markdown)
library(stringr)
library(shiny)

# function derived from the highlightHTMLcells() function of the highlightHTML package
colortable <- function(htmltab, css, style="table-condensed table-bordered"){
  tmp <- str_split(htmltab, "
")[[1]] 
  CSSid <- gsub("\{.+", "", css)
  CSSid <- gsub("^[\s+]|\s+$", "", CSSid)
  CSSidPaste <- gsub("#", "", CSSid)
  CSSid2 <- paste(" ", CSSid, sep = "")
  ids <- paste0("<td id='", CSSidPaste, "'")
  for (i in 1:length(CSSid)) {
    locations <- grep(CSSid[i], tmp)
    tmp[locations] <- gsub("<td", ids[i], tmp[locations])
    tmp[locations] <- gsub(CSSid2[i], "", tmp[locations], 
                           fixed = TRUE)
  }
  htmltab <- paste(tmp, collapse="
")
  Encoding(htmltab) <- "UTF-8"
  list(
    tags$style(type="text/css", paste(css, collapse="
")),
    tags$script(sprintf( 
                  '$( "table" ).addClass( "table %s" );', style
                )),
    HTML(htmltab)
  )
}

##
runApp(
  list(
    ui=pageWithSidebar(
      headerPanel(""),
      sidebarPanel(
      ),
      mainPanel(
        uiOutput("htmltable")
      )
    ),
    server=function(input,output,session){
      output$htmltable <- renderUI({
        # define CSS tags
        css <- c("#bgred {background-color: #FF0000;}",
                 "#bgblue {background-color: #0000FF;}")
        # example data frame 
        # add the tag inside the cells
        tab <- data.frame(x=c("A","B"), y=c("red cell #bgred", "blue cell #bgblue"))
        # generate html table with pander package and markdown package
        htmltab <- markdownToHTML(
          text=pandoc.table.return(
            tab, 
            style="rmarkdown", split.tables=Inf
          ), 
          fragment.only=TRUE
        ) 
        colortable(htmltab, css)
      })
    })
)

这篇关于R 闪亮颜色数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆