I'm in the process of creating my first package. In a lot of situations, I get ideas from someone else's function, and attempt to utilize how they went about doing something from their code.
Is there a suggested way to systematically review someone else's code in order to understand the arguments, and the steps that get to the result of the function?
I currently fork the repo, and manually execute the code line by line. It gets a little more complex with functions embedded within functions.
Thanks in advance, and look forward to suggestions!
My idea is to create an RMarkdown file and break out the code line by line. I would then print the objects to see what each line of code does. Does anyone else do that? Is there a way to print named objects by default in RMarkdown? Right now my code is taking each named object and then using
This doesn't answer your specific question, and you've nearly certainly seen this already, but a great way to get started with best practices for R package development is Hadley's
@realhiphop How exactly are you manually executing the code line by line? Are you using the interactive debugger? When I want to deeply understand a function in another package, I use debug() and browser() to enter into the debugger. browser() is especially useful for investigating nested functions.
Based on your response, I'm guessing that I've been creating a lot of manual work for myself. Here's an example of my process. Let's imagine that the arguments are variables required to build the URL.
I think you get the point. I would generally take the function arguments and write them in code, then run each line of code individually. I would then go into each object and see what changed, etc. Using glimpse, or view.
# Function Inputs
movie_genre = "Action"
stars = "4"
@realhiphop This approach works well for investigating a single function. But as you mentioned in your original post, if you want to investigate more complex situations, I'd recommend taking the time to learn how to use the interactive debugger. Fortunately RStudio enhances the experience. It will show you which line you are currently on, and the Environment pane automatically updates to show whichever objects are defined in the environment you are currently debugging. It also has shortcuts that allow you to conveniently use the R debugging tools, but I'd recommend using them manually first so that you better understand what the RStudio debugging tools are doing.
I'd recommend learning how to use the functions browser(), debug(), and traceback(), as well as setting the option options(error = recover).
My biggest tip is that when you're in the debugger, you can always type help to see the list of available commands:
> help
n next
s step into
f finish
c or cont continue
Q quit
where show stack
help show help
<expr> evaluate expression