Есть ли шанс, что вы можете сделать что-то подобное с помощью Shiny? Я прошел через учебники, но это, по-видимому, не охвачено.
Ответ 3
Я получил окраску клеток в Shiny DataTables
, который, я считаю, jQuery под капотом, используя этот код ниже для options
части вызова renderDataTable
:
options = list(fnRowCallback = I(colouring_datatables(do_colouring=do_colouring,c("regular","strict","strict","regular","strict","regular","regular","regular"),c(8,9,10,11,12,13,14,15))), bSortClasses = TRUE, aaSorting=list(list(3, "desc")), aLengthMenu = list(c(10, 25, 50, 100, -1), c('10', '25', '50', '100', 'All')),
"sDom" = 'RMDT<"cvclear"C><"clear">lfrtip',
"oTableTools" = list(
"sSwfPath" = "copy_csv_xls.swf",
"aButtons" = list(
"copy",
"print",
list("sExtends" = "collection",
"sButtonText" = "Save",
"aButtons" = list("xls","csv")
)
)
)
)
Я определил список диапазонов цветов, таких как "обычный", "строгий" и т.д. и использовал их в этой функции colouring_datatables ниже:
colouring_datatables = function(do_colouring = TRUE, apply_ranges,apply_columns) {
string = ''
callback_init = ""
callback_ends = ""
function_init = 'function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {'
function_ends = '}'
# highviz
#regular$colour = c("#FF0000","#FF3800","#FF7100","#FFAA00","#FFE200","#E2FF00","#AAFF00","#71FF00","#38FF00","#00FF00")
# Semaphore: only three colours
semaphore = list()
semaphore$from = c(0.000 ,0.500 ,0.750 )
semaphore$to = c(0.500 ,0.750 ,1.100 )
semaphore$colour = c("#F7977A","#FFF79A","#82CA9D")
# Strict: ten colours with most granularity around 0.900 and 1.000
strict = list()
strict$from = c(0.000 ,0.500 ,0.800 ,0.900 ,0.960 ,0.970 ,0.975 ,0.980 ,0.985 ,0.990 )
strict$to = c(0.500 ,0.800 ,0.900 ,0.960 ,0.970 ,0.975 ,0.980 ,0.985 ,0.990 ,1.100 )
strict$colour = c("#F7977A","#F3AC7B","#F0C07C","#ECD27D","#E8E27E","#D8E47F","#C3E180","#B0DD80","#9FD981","#8FD581")
# Regular: ten colours with most granularity between 0.800 and 0.900
regular = list()
regular$from = c(0.000 ,0.500 ,0.700 ,0.800 ,0.860 ,0.870 ,0.875 ,0.880 ,0.885 ,0.890 )
regular$to = c(0.500 ,0.700 ,0.800 ,0.860 ,0.870 ,0.875 ,0.880 ,0.885 ,0.890 ,1.100 )
regular$colour = c("#F7977A","#F3AC7B","#F0C07C","#ECD27D","#E8E27E","#D8E47F","#C3E180","#B0DD80","#9FD981","#8FD581")
# Linear: twenty colours with linear scale from 0.000 to 1.000
linear = list()
linear$from = c(0.000 ,0.050 ,0.100 ,0.150 ,0.200 ,0.250 ,0.300 ,0.350 ,0.400 ,0.450 ,0.500 ,0.550 ,0.600 ,0.650 ,0.700 ,0.750 ,0.800 ,0.850 ,0.900 ,0.950 )
linear$to = c(0.050 ,0.100 ,0.150 ,0.200 ,0.250 ,0.300 ,0.350 ,0.400 ,0.450 ,0.500 ,0.550 ,0.600 ,0.650 ,0.700 ,0.750 ,0.800 ,0.850 ,0.900 ,0.950 ,1.100 )
linear$colour = c("#F7967A","#F4A47A","#F2B17B","#EFBE7C","#EDC97C","#EBD47D","#E8DF7D","#E4E67E","#D6E47F","#C9E17F","#BCDF7F","#B1DC80","#A5DA80","#9BD880","#91D581","#87D381","#81D184","#81CE8D","#81CC95","#82CA9D")
# Twenty: twenty colours with most granularity between 0.700 and 1.000
twenty = list()
twenty$from = c(0.000 ,0.200 ,0.300 ,0.400 ,0.500 ,0.700 ,0.720 ,0.740 ,0.760 ,0.780 ,0.800 ,0.820 ,0.840 ,0.860 ,0.880 ,0.900 ,0.920 ,0.940 ,0.960 ,0.980 )
twenty$to = c(0.200 ,0.300 ,0.400 ,0.500 ,0.700 ,0.720 ,0.740 ,0.760 ,0.780 ,0.800 ,0.820 ,0.840 ,0.860 ,0.880 ,0.900 ,0.920 ,0.940 ,0.960 ,0.980 ,1.100 )
twenty$colour = c("#F7967A","#F4A47A","#F2B17B","#EFBE7C","#EDC97C","#EBD47D","#E8DF7D","#E4E67E","#D6E47F","#C9E17F","#BCDF7F","#B1DC80","#A5DA80","#9BD880","#91D581","#87D381","#81D184","#81CE8D","#81CC95","#82CA9D")
ranges = list()
ranges[["semaphore"]] = semaphore
ranges[["strict"]] = strict
ranges[["regular"]] = regular
ranges[["linear"]] = linear
ranges[["twenty"]] = twenty
string = paste0(string, callback_init)
string = paste0(string, function_init)
if (do_colouring) {
for (i in 1:length(apply_columns)) {
for (idx in 1:length(ranges[[apply_ranges[i]]]$from)) {
this = list()
this$column = apply_columns[i]
this$from = ranges[[apply_ranges[i]]]$from[idx]
this$to = ranges[[apply_ranges[i]]]$to[idx]
this$colour = ranges[[apply_ranges[i]]]$colour[idx]
string = paste0(string,'if (parseFloat(aData[',this$column,']) >= ',this$from,' && parseFloat(aData[',this$column,']) < ',this$to,') { $("td:eq(',this$column,')", nRow).css("background-color", "',this$colour,'"); }')
}
}
}
string = paste0(string, function_ends)
string = paste0(string, callback_ends)
return(string)
}