如何在R Shiny中有条件格式的数据帧? [英] How to have conditional formatting of data frames in R Shiny?

查看:157
本文介绍了如何在R Shiny中有条件格式的数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Excel,您可以轻松地在单元格上应用条件格式:



有没有可能用Shiny做这样的事情?我已经阅读了教程,但是显然没有涉及。



例如,我想有条件地着色 runExample(02_text)中的 perm code $:b
$ b


解决方案

您可以使用jQuery有条件地格式化您的表。



例如:

  library(shiny)
library(datasets)

script< - $('tbody tr td:nth-​​child(5)')。each(function(){

var cellValue = $(this).text();

$ if(cellValue> 50){
$(this).css('background-color','#0c0');
}
else if(cellValue< = 50 ){
$(this).css('background-color','#f00');
} $
runApp(list(
ui = basicPage(
tags $ head(tags $ script(HTML('Shiny.addCustomMessageHandler(jsCode ,function(message){eval(message.value); });'))),
tableOutput(view)
),
server = function(input,output,session){
$ b $ session $ onFlushed (function(){
session $ sendCustomMessage(type ='jsCode',list(value = script))
})

output $ view< - renderTable({ b $ b头(摇滚,n = 20)
})
}
))



tbody tr td:nth-​​child(5) I精确 nth-child(5)来循环第5列的每个 td (perms)。 c> session $ onFlushed(function(){
session $ sendCustomMessage(type ='jsCode',list(value = script))
})
因为如果你把脚本如果你想要更多的格式化,我建议你创建css类,并使用
code> addClass


  ###在UI中:
标签$头(标签$ STYL e(
.greenCell {
background-color:#0c0;
}

.redCell {
background-color:#f00;
}))

###在脚本
###中使用.addClass而不是.css(...)

$ (this).addClass('greenCell')


With Excel, you can easily apply conditional formatting over cells:

Is there any chance you can do something like this with Shiny? I've gone through the tutorials, but this apparently is not covered.

For instance, I'd like to conditionally colour the perm row in runExample("02_text"):

解决方案

You can conditionnal formatting your table using jQuery.

For example :

library(shiny)
library(datasets)

script <- "$('tbody tr td:nth-child(5)').each(function() {

              var cellValue = $(this).text();

              if (cellValue > 50) {
                $(this).css('background-color', '#0c0');
              }
              else if (cellValue <= 50) {
                $(this).css('background-color', '#f00');
              }
            })"

runApp(list(
  ui = basicPage(
    tags$head(tags$script(HTML('Shiny.addCustomMessageHandler("jsCode", function(message) { eval(message.value); });'))),
    tableOutput("view")
  ),
  server = function(input, output, session) {

    session$onFlushed(function() {
      session$sendCustomMessage(type='jsCode', list(value = script))
    })

    output$view <- renderTable({
      head(rock, n = 20)
    })
  }
))

In tbody tr td:nth-child(5) I precise nth-child(5) To loop on each td of the 5th column only (perms).

We need session$onFlushed(function() { session$sendCustomMessage(type='jsCode', list(value = script)) }) because if you put the script in the head, it will be executed before the table output rendered and then nothing will be formatting.

If you want more formatting I suggest you to create css classes and use addClass :

### In the UI :
tags$head(tags$style(
            ".greenCell {
                background-color: #0c0;
            }

            .redCell {
                background-color: #f00;
            }"))

### In th script
### use .addClass instead of .css(...)

$(this).addClass('greenCell')

这篇关于如何在R Shiny中有条件格式的数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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