crsh / papaja

papaja (Preparing APA Journal Articles) is an R package that provides document formats to produce complete APA manuscripts from RMarkdown-files (PDF and Word documents) and helper functions that facilitate reporting statistics, tables, and plots.
https://frederikaust.com/papaja_man/
Other
657 stars 133 forks source link

Using papaja::apa_table in loops #581

Closed SuzanneSegerstrom closed 9 months ago

SuzanneSegerstrom commented 10 months ago

I have a lot of tables to create for a data dictionary, and I've figured out how to make them look like what I want using papaja. However, it would be great if I could use a loop to ,create the multiple tables.

I've tried using different versions of apply and putting apa_table inside and outside the loop, but nothing is rendering correctly. I have tried pdf, word, and html. Sorry if I'm missing something obvious! Example syntax below. I am using ```{r echo=FALSE, results = 'asis', include = TRUE} in the block.

A simple example:

data <- cars

################### write a function to do all tables ####################################

### get the variable names

cat_vars <- data %>% colnames() %>% as.vector()

### put my syntax inside a function

cat_table <- function(x) {

  freq <- table(data$x) %>% t() %>% round(., digits = 0) 
  perc <- prop.table(freq) %>% t() %>% round(., digits = 2) %>% as.vector()

  table_cat <- 
    rbind(freq,perc) %>% as.data.frame() %>% set_rownames(c("Frequency", "Proportion")) 

  papaja::apa_table(table_cat)

}

### get the tables for all of the cat_vars
print(lapply(cat_vars, cat_table))

Result in knit document:

[1]] [1] “Table: (\#tab:unnamed-chunk-2) ———–Frequency Proportion ———–” attr(,“class”) [1] “knit_asis” attr(,“knit_cacheable”) [1] NA
#### OR #####

### function without apa_table()

cat_table <- function(x) {

  freq <- table(data$x) %>% t() %>% round(., digits = 0) 
  perc <- prop.table(freq) %>% t() %>% round(., digits = 2) %>% as.vector()

  table_cat <- 
    rbind(freq,perc) %>% as.data.frame() %>% set_rownames(c("Frequency", "Proportion")) 

}

### get the "raw" data to make into tables
categorical <- lapply(cat_vars, cat_table)
for (i in length(cat_vars)) {print(
                            papaja::apa_table(categorical[[i]])
                            )
                            }

result in knit document:

[1] “Table: (\#tab:unnamed-chunk-2) ———–Frequency Proportion ———–” attr(,“class”) [1] “knit_asis” attr(,“knit_cacheable”) [1] NA
crsh commented 9 months ago

Hi Suzanne,

I think there are bugs in your R code unrelated to papaja. Also, you need to use cat() rather than print(). The following works for me:

```{r test, results = "asis"}
library("magrittr")
data <- cars

################### write a function to do all tables ####################################

### get the variable names

cat_vars <- data %>% colnames() %>% as.vector()

### put my syntax inside a function

cat_table <- function(x) {

  freq <- table(data[, x]) %>% t() %>% round(., digits = 0) 
  perc <- prop.table(freq) %>% t() %>% round(., digits = 2) %>% as.vector()

  table_cat <- 
    rbind(freq,perc) %>% as.data.frame() %>% set_rownames(c("Frequency", "Proportion")) 

}

### get the "raw" data to make into tables
categorical <- lapply(cat_vars, cat_table)
for (i in 1:length(cat_vars)) {
  cat(papaja::apa_table(categorical[[i]], caption = "test"))
}
```

Given that this seems to be unrelated to papaja, I'll close this issue. If you have further questions, StackOverflow would be a more suitable platform to get answers quickly. You can use the papaja tag over there for anything related to this package.

Cheers.