rubenarslan / codebook

Cook rmarkdown codebooks from metadata on R data frames
https://rubenarslan.github.io/codebook/
Other
142 stars 16 forks source link

anoymous multi-line function in "aggregate_and_document_scale" leads to an error #69

Closed nsunami closed 1 month ago

nsunami commented 3 years ago

codebook() was failing with the Error: Argument 1 must have names for bind_rows(). After some investigation, I found that the error is caused by my anonymous function in aggregate_and_document_scale()

I got a function like:

df$my_scale <- df %>% select(var1:var8) %>%
  aggregate_and_document_scale(fun = function(x){
  rowMeans(x, na.rm = TRUE)
})

But, it was producing labels like

attr(,"label")
[1] "8 my_scale items aggregated by function(x) {"               
[2] "8 my_scale items aggregated by     rowMeans(x, na.rm = TRUE)"
[3] "8 my_scale items aggregated by }"                           

I declared a named function instead, and it worked just fine, like below.

rowMeansNarm <- function(x){
  rowMeans(x, na.rm = TRUE)
}
df$my_scale <- df %>% select(var1:var8) %>%
  aggregate_and_document_scale(fun = rowMeansNarm)

It seems like each line of the anonymous function is currently treated as separate labels.

HeatherUrry commented 2 years ago

Hello Nami and Ruben!

I'm encountering the same error (Error: Argument 1 must have names for bind_rows()) but I can't figure out how to solve it. By poking through codebook's R scripts, I see that the metadata.R file calls the aggregate_and_document_scale() function. But I can't find metadata.R in the codebook library on my computer to toy around with the code. Wondering if someone can point me in the right direction.

H.

nsunami commented 2 years ago

Hi Heather! I'm happy to take a look at your example if you can share it somewhere. My solution was to create another function for the fun argument (to remove the argument na.rm=TRUE). But I'm not sure if this solution applies to your case.

metadata.R is available on this repo—here is the part that the aggregate_and_document_scale function is being declared.

HeatherUrry commented 2 years ago

Thanks, Nami! I realize now that I was fundamentally misunderstanding something. My code never calls aggregate_and_document_scale() so that can't be the source of the error for me. :\

When I backtrace the error, it appears to be an issue in codebook_table.R. Still sleuthing over here.

HeatherUrry commented 2 years ago

Well, I doubt anybody's keeping track but, for the record, I got smart and realized that something I was doing with the data after importing from SPSS but before generating the codebook was introducing the Error: Argument 1 must have names message. After deleting extraneous code, the codebook generates without errors. I don't exactly know what I was doing that was problematic but it works now and I'm gonna go with it.

rubenarslan commented 2 years ago

Hi @HeatherUrry and @nsunami. I think I know how to fix the first error, but it'd help me if you can create a small reproducible example of the second error for me Heather.

On Fri, Dec 24, 2021 at 7:19 PM Heather Urry @.***> wrote:

Well, I doubt anybody's keeping track but, for the record, I got smart and realized that something I was doing with the data after importing from SPSS but before generating the codebook was introducing the Error: Argument 1 must have names message. After deleting extraneous code, the codebook generates without errors. I don't exactly know what I was doing that was problematic but it works now and I'm gonna go with it.

— Reply to this email directly, view it on GitHub https://github.com/rubenarslan/codebook/issues/69#issuecomment-1000912181, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGK5BONM7G6NFCK664DWPLUSS2U5ANCNFSM5D6LDVWQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

HeatherUrry commented 2 years ago

Hi @rubenarslan, Thanks for getting back to me about this. I deleted the code that led to the error so I can't easily backtrace which specific code was problematic. Sorry! If I end up tracing it enough to put together a reproducible example, I'll let you know.

H.