各位,苦找这函数的R包却不得
希望得到各位解惑~
tb1.contineous.normal
tb1.categorial
tb1.contineous.not.normal
源代码如下:
chtable <- function (design, cv = NULL, cv.nn = NULL, gv = NULL, by = NULL,
c_meanSQse = FALSE, c_meanPMse = FALSE, c_ci = FALSE, g_N = FALSE,
g_percent = FALSE, g_perSQse = FALSE, g_NSQper = FALSE, g_nSQper = FALSE,
g_ci = FALSE, g_direction = "v", total = FALSE, round = 2,
view = T, xlsx = NULL)
{
ck.by <- list(cv = cv, gv = gv, cv.nn = cv.nn, by = by)
(ck.by <- ck.by[!sapply(ck.by, is.null)])
if (length(ck.by) > 1) {
for (i in 1:(length(ck.by) - 1)) {
for (j in (i + 1):length(ck.by)) {
if (names(ck.by)[i] == "cv" & names(ck.by)[j] ==
"cv.nn")
(next)(j)
ck.common <- set::and(ck.by[[i]], ck.by[[j]])
if (length(ck.common) > 0)
stop(names(ck.by)[i], " and ", names(ck.by)[j],
" have the same variable: ", paste0(ck.common,
collapse = ", "))
}
}
}
v <- c(cv, cv.nn, gv)
lv <- set::not(v, colnames(design$variables))
if (length(lv) > 0) {
if (do::cnOS())
stop(tmcn::toUTF8("变量 "), paste0(lv, collapse = ", "),
tmcn::toUTF8(" 输入错误"))
if (!do::cnOS())
stop("variable ", paste0(lv, collapse = ", "), " not exist")
}
r1 <- NULL
if (!is.null(cv)) {
r1 <- tb1.contineous.normal(design, cv, by, meanSQse = c_meanSQse,
meanPMse = c_meanPMse, ci = c_ci, total, round)
}
r2 <- NULL
if (!is.null(cv.nn)) {
r2 <- tbl.contineous.not.normal(design, cv.nn, by, total,
round)
}
r3 <- NULL
if (!is.null(gv)) {
r3 <- tb1.categorial(design, x = gv, by, value = g_N,
per = g_percent, perSQse = g_perSQse, NSQper = g_NSQper,
nSQper = g_nSQper, ci = g_ci, direction = g_direction,
total = total, round)
}
r <- plyr::rbind.fill(r1, r2)
r <- plyr::rbind.fill(r, r3)
row.names(r) <- NULL
if (view)
nhs_view.svytableone(r)
r$variable <- do::Replace(r$variable, "~~~~", " ")
if (!is.null(xlsx)) {
header_bold <- openxlsx::createStyle(textDecoration = "Bold")
wb <- openxlsx::createWorkbook()
openxlsx::addWorksheet(wb, "Sheet1")
for (i in 1:ncol(r)) {
openxlsx::writeData(wb, sheet = 1, x = colnames(r)[i],
startCol = i, startRow = 1, headerStyle = header_bold)
if (i == 1) {
for (j in 1:nrow(r)) {
if (do::left(r[j, i], 4) == " ") {
openxlsx::writeData(wb, sheet = 1, x = r[j,
i], startCol = i, startRow = j + 1)
}
else {
openxlsx::writeData(wb, sheet = 1, x = r[j,
i], startCol = i, startRow = j + 1, headerStyle = header_bold)
}
}
}
else if (grepl("p", colnames(r)[i], T)) {
for (j in 1:nrow(r)) {
if (nchar(r[j, i]) > 0) {
ii <- tryCatch(as.numeric(r[j, i]), warning = function(w) "w")
if ((ii == "w" | ii <= 0.05) & r[j, i] !=
"ref") {
openxlsx::writeData(wb, sheet = 1, x = ii,
startCol = i, startRow = j + 1, headerStyle = header_bold)
}
}
else {
openxlsx::writeData(wb, sheet = 1, x = r[j,
i], startCol = i, startRow = j + 1)
}
}
}
else {
openxlsx::writeData(wb, sheet = 1, x = r[, i],
startCol = i, startRow = 2)
}
}
openxlsx::saveWorkbook(wb, xlsx, overwrite = TRUE)
}
invisible(r)
}