#' Make descriptives of a list of numeric variables, and write it in excel
#' all descriptives in a single table
#'
#' @param x a tbl() that contains the variables of interest, keyed by ID variable
#' @param ... list of variables names that would appear in output
#' @param folder_name where would the output file will be saved (project's home by default).
#'
#' @return descriptives tbl
#'
#' @examples
#' data=tribble(~ID,~car_size,~height,
#' "1",120,71,
#' "2",110,81,
#' "3",90,91,
#' "4",80,120,
#' "5",70,300,
#' "6",60,1,
#' "7",NA,80)
#'
#' data %>%
#' write_descriptives(car_size,height,folder_name = "R")
#'
#' @importFrom magrittr %>%
#' @importFrom dplyr enquos
#' @importFrom dplyr mutate
#' @importFrom dplyr rename
#' @importFrom dplyr count
#' @importFrom dplyr select
#' @importFrom dplyr everything
#' @importFrom dplyr group_by
#' @importFrom dplyr summarise
#' @importFrom openxlsx createWorkbook
#' @importFrom openxlsx addWorksheet
#' @importFrom openxlsx writeData
#' @importFrom openxlsx createStyle
#' @importFrom openxlsx saveWorkbook
#'
#' @export
#'
write_descriptives <- function(x,...,folder_name=getwd(),
name="descriptives_of_variables.xlsx"){
#for all variable in variable list make a big table with n's
vlist <- enquos(...)
ntotal <- nrow(x)
wb <- createWorkbook()
hs1 <- createStyle(
fgFill = "#DCE6F1", halign = "CENTER", textDecoration = "bold",
border = "bottom"
)
addWorksheet(wb, "variables_descriptives")
#name <- paste0("n_of_variables.csv")
#cat(paste("printing-",name))
reslist <- list()
for (vname in vlist){
reslist[[as.character(vname)[2]]] <- x %>%
mutate(var_name=as.character(vname)[2]) %>%
group_by(var_name) %>%
summarise(n=sum(!is.na(!!vname)),
missing=sum(is.na(!!vname)),
avg=round(mean(!!vname,na.rm=T),2),
median=round(median(!!vname,na.rm=T),2),
max=round(max(!!vname,na.rm=T),2),
min=round(min(!!vname,na.rm=T),2),
std=round(sd(!!vname,na.rm=T),2)) %>%
# summarise(n=n()) %>%
# count(var,!!vname) %>%
# rename(value=!!vname) %>%
mutate(percent=paste0(round((n/ntotal)*100),"%")) %>%
mutate(total=ntotal)
}
x <- do.call(rbind, reslist)
writeData(wb, "variables_descriptives", x,
colNames = F,
rowNames = F,
startCol = "A", startRow = 2,
borders = "surrounding", borderColour = "black"
)
writeData(wb, "variables_descriptives", x,
colNames = T,
rowNames = F,
startCol = "A", startRow = 1,
borders = "surrounding", borderColour = "black",headerStyle = hs1
)
saveWorkbook(wb,file = paste0(folder_name,"/",name), overwrite = TRUE)
return(x)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.