Search code examples
rfunctionedittrace

Editing a function in R using trace?


I noticed there is a bug in a function from a package I want to use. An issue has been made on GitHub, but the creator hasn't adressed this yet, and I need the function as soon as possible.

Therefore I want to edit the code. Apparently this is possible by editing the source, repacking and installing the entire package, I can rewrite the function and reassign the namespace, but also possibly by just editing the function in the current session using trace().

I already found out I can do:

as.list(body(package:::function_inside_function))

The line I want to edit is located in the second step of the function.

Specifically, it is this line in the code I need to edit. I have to change ignore.case to ignore.case=TRUE. An example in case the link dies:

functionx(){if{...} else if(grepl("miRNA", data.type, ignore.case)) {...}}

I haven't really found a practical example on how to proceed from here, so can anyone show me an example of how to do this, or lead me to a practical example of using trace? Or perhaps reassigning the function to the namespace?


Solution

  • For your specific case, you could probably indeed work around it using trace.

    From the link you provide I don't know why you speak of a function inside a function, but this should work:

    # example
    trace("grepl", tracer = quote(ignore.case <- TRUE))
    
    grepl("hi", "Hi")
    ## Tracing grepl("hi", "Hi") on entry 
    ## [1] TRUE
    
    # your case (I assume)
    trace("readTranscriptomeProfiling", tracer = quote(ignore.case <- TRUE))
    

    Note that this would be more complicated if the argument ignore.case that you want to fix wasn't already at the right position in the call.