R闪亮renderTable更改单元格颜色 [英] R shiny renderTable change cell colors

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

问题描述

我有一张桌子,想根据X(= 6)不同蓝色阴影中的值(0-100)为每个单元格着色.该表显示在TabPanel中.

I have a table and want to color every cell depending on the value (0-100) in X(=6) different shades of blue. The table is shown in a TabPanel.

当前,我正在使用Shinyjs调用一个javascript函数,该函数选择我的表并将CSS样式添加到<td>标记中,具体取决于值范围.

Currently I am using shinyjs to call a javascript function which selects my table and add CSS styling to the <td> tags, depending on the value range.

问题是,在第一次加载表格时(单击TabPanel),只有重新加载后才显示颜色.

The Problem is, that on the first loading of the table (click on TabPanel), no color is shown, only after reloading again.

因此,我正在寻找R中的解决方案(不需要额外的Javascript),或者是一种自动重新加载Table/TabPanel的方法.

So I am either looking for a solution in R (without the need for extra Javascript), or a method to automatically reload a Table/TabPanel.

library(shiny)

ui <- shinyUI(fluidPage(
    tableOutput("dataTable")
  ))

server <- shinyServer(function(input, output) {

  output$dataTable <- renderTable({
    data <- getDataFromSomeWhere();
    //Some operations on data
    data
    //I want to color every table cell, depening on value (f.e. 0-5 = white, 10-20 = light blue ...)
  }, rownames = TRUE, colnames = TRUE)

shinyApp(ui = ui, server = server) 

更新 最后,我坚持使用JavaScript解决方案,但使用了闪亮的特定js事件来获得所需的效果:

UPDATE In the end I stayed with the JavaScript solution, but used the shiny specific js events to get the desired effect:

$(document).on("shiny:value", function(e) {
  if (e.name == "myComponent") {
    e.preventDefault();
    $('#myComponent').html(e.value);
    //color code etc.
}

推荐答案

您可以使用tableHTML创建表并有条件地设置其样式.

You can use tableHTML to create a table and style it conditionally.

library(shiny)
library(tableHTML)

更改ui以使用tableHTML中的输出功能:

Change the ui to use the output function from tableHTML:

ui <- shinyUI(fluidPage(
  tableHTML_output("dataTable")
))

然后使用render_tableHTML()呈现其中生成的表.

Then use render_tableHTML() to render the table that is produced within.

您可以使用函数tableHTML()创建纯HTML表.然后,您可以使用add_css_conditional_column()创建条件(在本例中为between)来更改背景色(注意:您也可以使用其他css.在示例中,我使用了#f6f6f6而不是white,因为您不会在输出中看到差异)

You can create a plain HTML table using the function tableHTML(). You can then use add_css_conditional_column() to create conditionals (in this case between) to change the background colour (Note: you could use other css as well. I have used #f6f6f6 instead of white in the example, since you would not see a difference in the output)

server <- shinyServer(function(input, output) {

  getDataFromSomeWhere <- reactive({
    mtcars
  })

  output$dataTable <- render_tableHTML({
    data <- getDataFromSomeWhere();
    # //Some operations on data
    data %>% 
      tableHTML(rownames = TRUE) %>% 
      add_css_conditional_column(conditional = 'between',
                                 between = c(0, 5),
                                 css = list(c('background-color'),
                                            c('#f6f6f6')),
                                 columns = 1:ncol(data)) %>% 
      add_css_conditional_column(conditional = 'between',
                                 between = c(10, 20),
                                 css = list(c('background-color'),
                                            c('lightblue')),
                                 columns = 1:ncol(data))

  })

})

最终结果是:

shinyApp(ui = ui, server = server) 

您可以在中找到有关如何使用tableHTML的更多详细信息.渐晕.

You can find more details on how to use tableHTML in the vignettes.

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

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