本文介绍了在R Shiny中编写一个formatStyle循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们.

我遇到了一个问题:

我正在开发R Shiny应用程序,我想突出显示数据表中的一些值.

I am developing a R shiny app and I want to highlight some values in the datatable.

我有一个数据框(实体)和一个矢量(vec1),当每列中的值等于vec1中的值时,我想突出显示每列中的特定值.

I have a dataframe(entities) and a vector(vec1), and I want to highlight a specific value in each column when the value in each column is equal to the value in a vec1.

现在,我通过重复formatStyle代码25次来实现它,但是我相信可以通过编写循环来实现.有人可以帮我吗?

Now I achieved it by repeating the formatStyle code 25 times, but I believe it can be done by writing a loop. Could anyone help me?

vec1 = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)

       datatable(data = datasetInput3(), options = list(pageLength = 25)) %>% formatStyle(
      colnames(entities)[1],
      backgroundColor = styleEqual(vec1[1], c('yellow'))) %>% formatStyle(
       colnames(entities)[2],
       backgroundColor = styleEqual(vec1[2], c('yellow')))
...
%>% formatStyle(
       colnames(entities)[25],
       backgroundColor = styleEqual(vec1[25], c('yellow')))
    })

推荐答案

您可以这样做:

DT <- datatable(data = datasetInput3(), options = list(pageLength = 25))
for(i in 1:25){
    DT <- DT %>%
            formatStyle(colnames(entities)[i],
                        backgroundColor = styleEqual(vec1[i], c('yellow')))
}
DT

这篇关于在R Shiny中编写一个formatStyle循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:58