I want to add variable labels to the frequency tables that I generated. But, I can't find that functionality in the summarytools documentation.
Here is my code:
the data
library(magrittr)
library(dplyr)
library(gtsummary)
library(summarytools)
require(pander)
library(knitr)
library(stringr)
data_in_na <- readr::read_table2('q1 q2
No somelongresponsethattakesupmorethanonline--somelongresponsethattakesupmorethanonline
No somelongresponsethattakesupmorethanonline--somelongresponsethattakesupmorethanonline
No somelongresponsethattakesupmorethanonline--somelongresponsethattakesupmorethanonline
No somelongresponsethattakesupmorethanonline--somelongresponsethattakesupmorethanonline
No Yes
No Always
Yes No
Yes No
Yes No
NA NA
NA NA
NA NA
NA NA
')
vct <- data_in_na %>% names(.)
function to create frequencies
create_freq <- function(v1) {
#names <- c("var name 1", "var name 2")
freq(v1,
cumul = FALSE,
totals = TRUE,
display.type = FALSE,
variable.label = v1,
missing ='missing',
report.nas = TRUE)
}
loop to run through all the variables
for (i in vct) {
tbl <- create_freq(data_in_na[i]) # build gtsummary table
print(tbl) # print table
cat('\n\n----------------------------------------------------------\n\n')
# d <- table(data_in[i])
# print(kable(d))
}
what I want
Any suggestions please??

This adds a "Variable" label about the field names and removes the double hashtags. I also adjusted the standard max-width of 940 px to 1000px -- however, if the actual long name is a lot longer than your example data, this won't be much help. I've provided comments (
//precedes inline comments in JS) so that you can see what each area of the Javascript is doing. You don't have to do anything special for the Javascript to work (it's built-in in R Markdown).Using your code as it already is, add the following in as a chunk after your code.
If you try to run this chunk inline, it won't produce output. However, you'll see the output when you render.
For the questions added to the question labels, I've edited this to provide two different ways to do this. One uses R. The other uses JS.
I would suggest using R, because it's native. But you may have your own reasons for not doing so. This violates good naming practices, but it could be used for just these tables (if you wanted).
The R version is OPTION 1. This code goes between your data collection and storing the variable names in
vct. (I addedvctto this code, actually.)Now for the JS - 2 lines equate to doing what
pasteis doing in the R chunk. Using one or the other, you won't see a difference with this data specifically. Although, if there are a lot of questions, you may find the R method preferable.If you add
eval=FALSEto the chunk, you can see what it changes when you don't use it.If you want a word other than
Variables, you have to adjust that value next toFreq. (Currently, that value is 12.) You'll also want to make sure that you maintain the same number of spaces between that word andFreq.Let me know if you have any questions.
Before edit:
After edit: