1

I'm trying to work through the source code for sjmisc::count_na. I'm not very experienced at digging through source code so I've been using this thread as a guide, but I am still getting stuck. I hope this is not a duplicate.

In RStudio I can press F2 on count_na to get its source, but it looks like some of the work is being done in a function called get_label. The source code for this function is simply

function (x, ..., def.value = NULL, case = NULL) 
{
  UseMethod("get_label")
}

The thread above suggests trying methods() or getAnywhere

library(sjmisc)

methods("get_label")
#> Error in .S3methods(generic.function, class, parent.frame()): no function 'get_label' is visible

getAnywhere("get_label")
#> A single object matching 'get_label' was found
#> It was found in the following places
#>   namespace:sjlabelled
#> with value
#> 
#> function (x, ..., def.value = NULL, case = NULL) 
#> {
#>     UseMethod("get_label")
#> }
#> <bytecode: 0x000000001a7522c0>
#> <environment: namespace:sjlabelled>

This makes clear that get_label() is a function in the sjlabelled namespace. I'm not sure how to proceed in order to find its source code. Based on the thread above, I understand that I first need to figure out which classes get_label is written for, and then I can look at the source code for the class-specific methods. For some functions I've been able to do this using a dropdown menu inside RStudio after using F2, but there is no such menu available here. I have also tried:

methods("sjlabelled::get_label")
#> Error in .S3methods(generic.function, class, parent.frame()): no function 'sjlabelled::get_label' is visible

methods("sjlabelled:::get_label")
#> Error in .S3methods(generic.function, class, parent.frame()): no function 'sjlabelled:::get_label' is visible

showMethods("sjlabelled::get_label")
#> 
#> Function "sjlabelled::get_label":
#>  <not an S4 generic function>

What's going on here? What do I need to do the find the get_label source code?

lost
  • 1,483
  • 1
  • 11
  • 19

1 Answers1

2

Try this: You can change the class argument to your preferred in getS3method("get_label","data.frame"). Available classes are found in the result of methods("get_label")

library(sjlabelled)
library(sjmisc)
methods("get_label")

getS3method("get_label","data.frame")

function (x, ..., def.value = NULL, case = NULL) 
{
    x <- get_dot_data(x, dplyr::quos(...))
    sapply(seq_along(x), function(i) {
        label <- attr(x[[i]], "label", exact = T)
        if (is.null(label)) {
            if (!is.null(def.value)) {
                if (i <= length(def.value)) 
                  label <- def.value[i]
                else label <- def.value
            }
            else {
                label <- ""
            }
        }
        names(label) <- colnames(x)[i]
        convert_case(label, case)
    })
}
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • 1
    It looks like the part I was missing was `library(sjlabelled)` before `methods()`. I take it `sjlabelled` is a kind of sub-package whose functions aren't meant to globally visible? – lost Feb 01 '19 at 06:10
  • `sjmisc` imports some functions from `sjlabelled`. I wouldn't say sjlabelled is a sub-package. Perhaps it's meant to be "invisible" as it doens't show when called using `sjlabelled:::get_label` – NelsonGon Feb 01 '19 at 06:18