问题描述
我在一个闪亮的应用程序中有一个rhonsontable.我的目标是根据列的总和为列中的所有单元格着色.例如:如果该列中值的总和为1,则该列中的所有单元格都将显示为绿色.
I have an rhandsontable inside a shiny app.My goal is to color all cells in a column based on the sum of the column.Ex : if the sum of the values in the column is 1, then, all cells in this column are colored in green.
向用户显示的预期结果如下:
The expected outcome displayed to the user is like this:
使用这样的JS格式似乎可以做到这一点:
It seems possible to do so with a JS formatting like this :
rhandsontable(myrhandsontable) %>%
hot_cols(renderer ="some JS script")
我以前从未做过任何JS,而我很难获得"some JS script
"部分中的列总和.
I have never done any JS before and I struggle to get the sum of the column inside the "some JS script
" part.
以下是可与之一起使用的最小生殖实例:
Here is a minimal reproductible exemple to work with:
library(shiny)
library(rhandsontable)
library(tidyverse)
# basic user interface (not important here)
ui <- fluidPage(
rHandsontableOutput(outputId = "ex")
)
# server side calculations
server <- function(input, output) {
# create a dummy dataset
ex_data = data.frame(id = letters[1:3],
attr1 = c(0.5, 0.4, 0.3),
attr2 = c(0.6, 0.3, 0.1))
# create the rhandsontable object and define conditional formatting
output$ex = renderRHandsontable({
rhandsontable(ex_data) # %>% renderer ="JS script for conditional formatting")
})
我尝试使用这些帖子和教程未成功:
I unsuccessfully tried to use these posts and tutorials:
- 特定行的可合理更改的背景
- > Rhandsontable条件格式-如何突出显示基于特定属性值的行?
- https://jrowen.github.io/rhandsontable/#custom_renderer_using_r
- https://jrowen.github.io/rhandsontable/#conditional_formatting
- rhandsontable change background of specific row
- Rhandsontable conditional formatting- How to highlight rows based on specific attribute value?
- https://jrowen.github.io/rhandsontable/#custom_renderer_using_r
- https://jrowen.github.io/rhandsontable/#conditional_formatting
欢迎提出任何想法:)
推荐答案
我们可以在此处看到此解决方案 rhandsontable-Custom Renderer ,但是,当我们以闪亮的方式实现此解决方案时,就会遇到问题,幸运的是,该问题已得到解决此处
We can see this solution here rhandsontable - Custom Renderer, however, this solution has an issue when we implement in shiny, luckily, the issue has been resolved here
library(shiny)
library(tidyverse)
library(rhandsontable)
# basic user interface (not important here)
ui <- fluidPage(
rHandsontableOutput(outputId = "ex")
)
# server side calculations
server <- function(input, output, session) {
# create the rhandsontable object and define conditional formatting
output$ex = renderRHandsontable({
# create a dummy dataset
ex_data = data.frame(id = letters[1:3],
attr1 = c(0.5, 0.4, 0.3),
attr2 = c(0.6, 0.3, 0.1))
#create index with columns sum is equal to 1
col_highlight <- unname(which(colSums(ex_data[c(2,3)])==1))
rhandsontable(ex_data, col_highlight = col_highlight,
width = 550, height = 300) %>%
hot_cols(renderer = "
function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.NumericRenderer.apply(this, arguments);
if (instance.params) {
hcols = instance.params.col_highlight;
hcols = hcols instanceof Array ? hcols : [hcols];
}
if (instance.params && hcols.includes(col)) {
td.style.background = 'lightgreen';
}
}")
})
}
shinyApp(ui,server)
这篇关于闪亮且可朗读-基于列总和的条件单元格/列格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!