How to debug or see function source codes/script

I am using a package called Cardinal. I am fairly new in Rstudio and R. I want to follow through with the R codes I am using in a script.
What I got so far is debug(function_name) should show the function script. But it doesn't work always or most cases.
For example,

data_peakref <- peakPick(data_spec) %>%

I want to go through the lines of peakPick function. How do I do that in Rstudio?

What happens when you send

peakPick

with no brackets or anything else to the console ?

> peakPick
standardGeneric for "peakPick" defined from package "Cardinal"

function (object, ...) 
standardGeneric("peakPick")
<bytecode: 0x558ce3b87700>
<environment: 0x558ce3b51bb0>
Methods may be defined for arguments: object
Use  showMethods(peakPick)  for currently available ones.

> showMethods(peakPick)
Function: peakPick (package Cardinal)
object="MSImageSet"
object="MSImagingExperiment"

This is what I am getting. No script or function code is shown.

Then try

Cardinal:::peakPick.MSImageSet
Cardinal:::peakPick.MSImagingExperiment
> Cardinal:::peakPick.MSImageSet
Error: object 'peakPick.MSImageSet' not found
> Cardinal:::peakPick,MSImageSet
Error: unexpected ',' in "Cardinal:::peakPick,"
> Cardinal::peakPick.MSImageSet
Error: 'peakPick.MSImageSet' is not an exported object from 'namespace:Cardinal'

After a bit of searching I found this:

> View(peakPick)

image

which is showing all codes and methods in the script window just as I wanted.

Now is it possible to run/step through the code and see what's happening something like debugging?

This is code I used to trace the internal function rtweet:::get_trends_ of the rtweet package.
Try to adapt it for your needs:

as.list(body(rtweet:::get_trends_))
trace(rtweet:::get_trends_,
			quote(browser()),
			at=8,
			where=asNamespace("rtweet"))
rtweet::get_trends("amsterdam") # this will hit the trace somewhere
untrace(rtweet:::get_trends_) # undo the tracing
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.