Reasonable way to use RStudio's source with modules?
Currently it seems like if you source module a, which imports module b using box::use(b), then any changes to b will not ever be reflected when you source a subsequent times. I assume this is to do with the caching mechanism, but it basically means that you can never use source, and therefore can't use the debugger. This seems like an issue.
There are two aspects here:
-
box::usekeeps the module source when loading, so debugging in modules loaded viabox::useworks in RStudio. What unfortunately doesn’t work is setting break points in the editor pane (I don’t know why this doesn’t work, because I don’t know how break points are implemented in RStudio; it could work but I don’t think RStudio provides public APIs to allow packages to hook into this). -
Reloading a module during development can be achieved via
box::reload. This will reload a module and its dependencies.
Thank you for the answer. Yes to be clear I was specifically talking about breakpoints in the editor. Is there any other way to step through code that you know does work with box?
I wonder if the RStudio folk could help elucidate why breakpoints aren't working. My understanding is that when you source a file containing breakpoints, RStudio actually edits the source code in some way to add something equivalent to a browser() call where the breakpoint is. But since the modules themselves are cached and also box doesn't use source, I'm not sure that it will be possible to pick this up, unless as you say there's a specific API that can be hooked into. I have a suspicion they haven't ever needed to provide one before, however.
Is there any other way to step through code that you know does work with
box?
‘box’ itself doesn’t interfere with R’s built-in debugging mechanisms. Therefore, all the following ways work with ‘box’:
- Explicitly debugging a function by calling
debugordebugonce. - Setting a break point in a function (via code) by inserting a call to
browser. - Stop on error by setting
options(error = recover). - There’s also
trace. which allows much more extensive tracing, but I’m not very familiar with it.
boxdoesn't usesource
The sole reason why ‘box’ doesn’t use source, incidentally, is that source is still broken on Windows (ironically this was first described by Joe Cheng, who works at RStudio; and yet RStudio uses the broken source to load files …). But even if ‘box’ internally used source, RStudio unfortunately wouldn’t pick this up …:
My understanding is that when you source a file containing breakpoints, RStudio actually edits the source code in some way to add something equivalent to a
browser()call where the breakpoint is.
You seem to be right, sourcing a function with break points in RStudio creates a trace wrapper via .rs.setFunctionBreakpoints — but I have no idea how/when exactly RStudio does this, but it doesn’t hook into the actual source function, and when calling source indirectly (i.e. by wrapping it onto another function), RStudio doesn’t notice that a file was loaded. It only notices if source is called directly on the R command line. I’ll try to understand what exactly RStudio is doing here, and whether I can convince it to do the same for ‘box’ modules.