1
DT가 표시되는 data.table이 있습니다. 두 개의 열에서 백분율을 표시하고 배경 막대를 표시하려고합니다. 그러나 열 수는 선택한 테이블에 따라 변경 될 수 있습니다. 하나 또는 두 개의 백분율 열이 있습니다.DT 데이터 테이블 표시 오류
여기에 몇 가지 더미 데이터가 있고 grep을 사용하지 않은 접근 방법이 있습니다.
*Error in FUN(X[[i]], ...) : only defined on a data frame with all numeric variables*
그래서 어떻게 동적으로 막대를 표시 할 열을 선택할 수 있습니다
a <- c(45, 143, 123, 120, 118, 109, 94, 81)
b <- c(54, 132, 119, 113, 108, 104, 99, 91)
a2 <- round(a/sum(a)*100,2)
b2 <- round(b/sum(b)*100,2)
dt <- data.table("Age" = c("-20", "20-30", "30-40", "40-50",
"50-60", "60-70", "70-80", "80+"),
"Group A" = a,
"Group A %" = a2,
"Group B" = b,
"Group B %" = b2)
if(sample(c(0,1), 1)==1) x <- dt else x <- dt[ ,c(1:3)]
DT::datatable(x,
rownames = FALSE,
extensions = c('FixedColumns'),
class = 'cell-border stripe',
options = list(dom = 't',
pageLength = nrow(x),
columnDefs = list(list(className = 'dt-center', targets = 0:(ncol(x)-1)))
)
) %>%
formatStyle(
grep("%", colnames(x), value=TRUE),
background = styleColorBar(x[, .SD, .SDcols=grep("%", colnames(x), value=TRUE)], 'steelblue'),
backgroundSize = '50% 50%',
backgroundRepeat = 'no-repeat',
backgroundPosition = 'right')
불행하게도이 오류를 생성?
도움을 주시면 감사하겠습니다.
완벽한 및 큰 재 작성없이 돌아갑니다. 고맙습니다. –