codebook icon indicating copy to clipboard operation
codebook copied to clipboard

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

Open nsunami opened this issue 4 years ago • 6 comments

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.

nsunami avatar Sep 13 '21 17:09 nsunami

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.

HeatherUrry avatar Dec 22 '21 22:12 HeatherUrry

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.

nsunami avatar Dec 22 '21 23:12 nsunami

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 avatar Dec 24 '21 14:12 HeatherUrry

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.

HeatherUrry avatar Dec 24 '21 18:12 HeatherUrry

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: @.***>

rubenarslan avatar Dec 27 '21 07:12 rubenarslan

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.

HeatherUrry avatar Jan 03 '22 01:01 HeatherUrry