我正在尝试使用Shiny创建一个合理的自定义数据表。我要合并的一些功能包括:过滤器标题,颜色/范围格式的列,X滚动条和Y滚动条,冻结窗格(第一列和所有标题)。我在DataTable上有很多问题。
其中一些问题是单击表的最右端的列的标题过滤器(单击时不正确过滤),并且向右移位(Y滚动条问题)。
在此页面的底部,我将添加数据以尝试一下。谢谢。
#first I create a function in which I will pass the data.frame
library(RColorBrewer)
library(DT)
library(tidyverse)
colorTable = function(table){
#choose our columns we want to be formatted
c.names = c("impErnMv", "stkVolu", "contango", "SIR", "AvgImpMv", "ImpMv_RealMv_Prem", "ImpMv_AvgImpMv", "IV_HV10", "ImpMv_AvgRealMv", "ImpMv_LastMv", "PC_ratio")
#choose the colors to use
colors = brewer.pal(9,"Pastel1")
colors = rep(colors,4)
nums = which(names(table)%in%c.names)
DT = datatable(
table, filter = 'top',extensions = "FixedColumns", options = list(
pageLength = 10, autoWidth = TRUE,
scrollX = TRUE, scrollY = T, scrollCollapse = T,
paging= F,
#freeze the panes
fixedColumns = list(leftColumns = 1)),
class = 'cell-border stripe', rownames = F
)
for (i in 1:ncol(table)){
if(i %in% nums){
DT = DT %>%
formatStyle(i,
background = styleColorBar(range(table[,i]), colors[i]),
backgroundSize = '98% 88%',
backgroundRepeat = 'no-repeat',
backgroundPosition = 'center'
)
}
}
return(DT)
}
#you can try it out in the console just with this. The scroll works great, but the filter headers to the right are still having some problems.
here is a basic shiny app with the DataTable.
library(shiny)
ui <- fluidPage(
titlePanel("DataTable Help"),
mainPanel(
DTOutput("table")
)
)
server <- function(input, output) {
output$table = renderDT({
colorTable(frameEA)
})
}
# Run the application
shinyApp(ui = ui, server = server)
#Here is frameEA which is the data used.
dput(frameEA)
structure(list(ticker = c("AAPL", "AMZN", "BA", "BABA", "BAC",
"CSCO", "CVX", "FB", "GOOG", "GOOGL", "HD", "INTC", "JNJ", "JPM",
"MA", "MRK", "MSFT", "ORCL", "PFE", "PG", "UNH", "V", "VZ", "WFC",
"WMT", "XOM"), Price = c(172.77, 1625.47, 421.63, 177.28, 28.53,
51.34, 122.1, 168.63, 1140.95, 1148.7, 182.03, 52.61, 138.14,
102.84, 221.76, 80.48, 110.33, 52.58, 41.39, 98.34, 236.8, 146.71,
56.11, 49.75, 97.34, 80.12), mktCap = c(849188, 798436, 238215,
454041, 292252, 247331, 233306, 489870, 793172, 798560, 205608,
246126, 370616, 352943, 227505, 216989, 849518, 217660, 246389,
247916, 227219, 294502, 231620, 242668, 288358, 339505), slope = c(3.168,
2.568, 3.054, 4.188, 3.222, 3.469, 4.498, 2.398, 2.74, 2.728,
3.43, 3.028, 5.039, 4.566, 4.858, 4.756, 3.76, 4.204, 4.631,
3.683, 3.273, 4.82, 3.742, 3.714, 2.865, 4.489), stkVolu = c(23977595,
3976467, 4057844, 12202657, 49433761, 22197795, 6028839, 15592495,
1450110, 1608300, 4797986, 20313728, 6138707, 11851414, 2901657,
9309362, 23134360, 13640114, 22052748, 8426695, 4639062, 7690331,
12526719, 16983757, 8282376, 12936396), avgOptVolu20d = c(351183.7,
154268.65, 72820.15, 133706.1, 220083.2, 78336.75, 22077.9, 210138.3,
20308.5, 26416.8, 30335.1, 79839.6, 23670.7, 50674, 14893.75,
26875.35, 122090.8, 18106.9, 26872.05, 21572.1, 14585.95, 31100.45,
28425.2, 41948.65, 48615.55, 48088.8), bestEtf = c("XLK", "XRT",
"XLI", "XRT", "XLF", "XLK", "XOP", "XLY", "XLY", "XLY", "XLY",
"XLK", "XLV", "XLF", "XLK", "XLV", "XLK", "XLK", "XLV", "XBI",
"XLF", "XLK", "XLK", "XLF", "XRT", "XOP"), Hv5 = c(15.26, 24.91,
26.34, 26.36, 18.8, 17.51, 19.38, 27.91, 21.01, 21.07, 16.49,
16.68, 11.44, 15.89, 15.66, 15.19, 15.28, 16.04, 20.81, 9.87,
39.39, 14.17, 19.19, 16.18, 15.59, 16.59), Hv10 = c(14.22, 19.94,
22.37, 27.77, 18.32, 17.12, 15.96, 24, 18.3, 18.25, 20.55, 16.57,
11.77, 14.74, 15.25, 14.73, 14.81, 13.37, 19.18, 10.28, 40.21,
13.83, 16.44, 16.31, 14.22, 15.24), Hv20 = c(14.39, 20.4, 19.92,
25.04, 18.7, 18.95, 15.6, 22.73, 18.06, 18.32, 18.56, 17.59,
12.27, 16.91, 14.29, 15.73, 15.79, 15.12, 18.35, 11.94, 32.73,
13.11, 15.88, 20.03, 18.2, 15.57), Iv10 = c(19.51, 27.51, 25.63,
26.68, 22.05, 17.72, 17.64, 25.79, 20.94, 21.18, 18.88, 21.86,
14.39, 18.37, 20.23, 17.45, 19.76, 39.78, 19.76, 14.53, 24.95,
17.12, 16.29, 19.08, 18.34, 18.65), Iv20 = c(21.19, 27.37, 25.8,
29.88, 22.39, 18.47, 18.08, 26.48, 20.71, 20.6, 18.35, 21.7,
13.29, 18.91, 20.21, 16.13, 19.48, 30.46, 18.67, 14.22, 25.51,
16.98, 16.58, 19.31, 17.7, 17.39), SIR = c(1.31, 0.9, 1, 8.23,
1.5, 1.69, 2.52, 1.15, 1.39, 1.23, 1.97, 1.9, 1.49, 1.25, 1.82,
3.23, 1.32, 3.91, 5.66, 2.18, 1.72, 3.31, 1.7, 1.11, 2.31, 2.33
), ernDate1 = c("1/29/2019", "1/31/2019", "1/30/2019", "1/30/2019",
"1/16/2019", "2/13/2019", "2/1/2019", "1/30/2019", "2/4/2019",
"2/4/2019", "2/26/2019", "1/24/2019", "1/22/2019", "1/15/2019",
"1/31/2019", "2/1/2019", "1/30/2019", "12/17/2018", "1/29/2019",
"1/23/2019", "1/15/2019", "1/30/2019", "1/29/2019", "1/15/2019",
"2/19/2019", "2/1/2019"), Ea_Date = c("2019-05-07", "2019-04-25",
"2019-04-24", "2019-05-03", "2019-04-15", "2019-05-15", "2019-04-26",
"2019-04-24", "2019-04-22", "2019-04-22", "2019-05-21", "2019-04-25",
"2019-04-16", "2019-04-12", "2019-05-01", "2019-05-07", "2019-04-25",
"2019-03-14", "2019-05-07", "2019-04-18", "2019-04-16", "2019-04-24",
"2019-04-23", "2019-04-12", "2019-05-16", "2019-04-26"), BdtEa = c(42,
34, 33, 40, 26, 48, 35, 33, 31, 31, 52, 34, 27, 25, 38, 42, 34,
4, 42, 29, 27, 33, 32, 25, 49, 35), AvgMv4 = c(5.944, 4.329,
3.104, 3.861, 3.45, 3.532, 2.502, 10.662, 2.836, 2.843, 0.82,
4.443, 1.966, 1.248, 2.87, 1.465, 2.781, 4.364, 2.675, 4.445,
3.616, 2.872, 2.72, 1.87, 3.852, 2.935), AvgImpMv = c(6.285,
8.124, 6.678, 7.817, 3.412, 4.814, 4.748, 8.783, 6.667, 6.719,
3.987, 7.6, 3.122, 3.412, 5.471, 4.001, 5.834, 6.021, 4.286,
4.033, 3.521, 5.259, 4.354, 4.062, 4.845, 4.131), ImpMv_RealMv_Prem = c(0.056,
0.63, 0.766, 0.705, -0.011, 0.31, 0.641, -0.194, 0.855, 0.86,
1.581, 0.537, 0.462, 1.006, 0.645, 1.005, 0.741, 0.322, 0.471,
-0.097, -0.027, 0.605, 0.471, 0.775, 0.229, 0.342), ImpMv_AvgRealMv = c(0.269,
0.572, 0.261, 0.766, 0.892, 0.362, 0.249, 0.178, 0.585, 0.538,
1.48, 0.413, -0.403, 0.654, 0.156, 0, 0.427, 0.111, 0.852, 0.125,
0.191, 0.589, 0.048, 0.684, 0.149, 0.177), ImpMv_AvgImpMv = c(-0.014,
-0.024, -0.216, -0.086, 0.314, 0.274, -0.395, -0.151, -0.185,
-0.215, 0.343, -0.141, -0.687, -0.272, -0.562, 0, -0.179, -0.087,
0.064, -0.119, -0.026, -0.249, -0.403, -0.208, 0.161, -0.526),
ImpMv_LastMv = c(-0.097, 0.388, -0.15, 0.124, -0.427, 1.206,
-0.014, -0.36, 1.56, 1.778, 1.849, 0.188, 0.082, 1.266, -0.111,
0, 0.979, 3.046, 0.376, -0.307, -0.035, 0.778, -0.111, 0.756,
0.946, -0.39), isConfirmed = c(FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE), PC_ratio = c(-0.4, -0.315, -0.061,
-0.187, -1.186, -0.114, -0.167, -0.489, 0.14, -0.319, -0.625,
-0.026, 0.773, 0.034, -0.106, -0.108, -0.308, 0.157, -0.334,
0.681, -0.235, -0.771, -0.438, -0.333, -0.412, 0.585), IvXern10 = c(19.51,
27.51, 25.63, 26.68, 22.05, 17.72, 17.64, 25.79, 20.94, 21.18,
18.88, 21.86, 14.39, 18.37, 20.23, 17.45, 19.76, 28.05, 19.76,
14.53, 24.95, 17.12, 16.29, 19.08, 18.34, 18.65), contango = c(-0.225,
-0.3, -0.32, 0.222, -0.013, 0.214, 0.113, 0.144, -0.033,
-0.051, -0.249, 0.062, -0.075, -0.008, -0.038, -0.11, 0.069,
-1.602, -0.155, 0.082, -0.349, 0.148, 0.095, -0.062, -0.192,
-0.325)), row.names = c(NA, -26L), class = "data.frame")