Ark: `View()` should support function objects
So you can look at their sources in a temporary editor pane, ideally recognized as an R file with syntax highlighting
In Positron I think it makes sense for this functionality to be fulfilled by F12, i.e. jump-to-definition. And if we do implement View() for functions, it would be nice if it worked exactly the same way as F12? I.e. follow source references if any, create virtual doc otherwise.
Cf "Generalised source availability" section of https://github.com/posit-dev/positron/issues/408
I'd love to see View(function-name) work as @DavisVaughan suggested. I really like this feature in RStudio and it's one thing I miss in Positron so far. Would the F12/jump-to-definition proposal work for base R functions? At the moment, if I press F12 on the name of a base R function in Positron, I see No reference found for 'function-name'.
Would the F12/jump-to-definition proposal work for base R functions?
yep, currently needs more infrastructure to implement it but you can already get a feel of how it'd work by running this in the console:
debugonce(data.frame)
data.frame()
This will drop you in the debugger (jump-to-def wouldn't). You can type Q in the console to quit it.
Thanks. That would be neat. I would still find it really handy to have a way of pulling that up from the R console as well, either with View() or with F12.
Possibly related to #2749 and #1015? In general F12/editor.action.revealDefinition doesn't work for non-locally defined functions.
In https://github.com/posit-dev/positron/discussions/7470 we have a request for View() for functions, specifically to use from the Variables pane via a click.
Verified Fixed
Positron Version(s) : 2025.08.0 (Universal) build 11
OS Version : Sequoia 15.5
Test scenario(s)
This is a series of a bit confusing and extensive tests. I think it could be relevant to automate this. I verified the behavior by running each of the scripts from the Setup Code into the R console, and then observing what happens. Please note that behavior may be dependent on the virtual or global env. Please find cases I tested below.
| Description | Setup Code | View() Call | Expected Behavior |
|---|---|---|---|
| View function from installed package | None | View(lm) |
Opens virtual namespace doc with lm at correct location |
View after debug() |
debug(lm) |
View(lm) |
Debug and View work without interfering |
View after undebug() |
undebug(lm) |
View(lm) |
Debug cleared, View works |
| View global env function (no srcref) | foo <- function() 1 |
View(foo) |
Opens deparsed foo in foo.R, URI includes "global" |
| View local anonymous function | local({ foo <- function() 1; View(foo) }) |
inside local block | Opens foo.R with env address in URI |
| View expression | foo <- function() 1 |
View(identity(foo)) |
Opens unknown.R, env address in URI |
| View from list | xs <- list(foo = foo) |
View(xs$foo) |
Same as above (unknown.R) |
| View from unnamed env | localenv <- new.env(); assign("foo", function() 1, envir = localenv) |
with(localenv, View(foo)) |
Opens foo.R with env address |
| Invalid Ark URI | N/A | .rs.api.navigateToFile("ark:non-existing-file") |
Opens virtual doc with error message |
| View from Variables pane | foo <- function() 1; xs <- list(foo = foo) |
Click "View" in pane | Shows source in tab (file or virtual doc) |
| View script-defined function (no srcref) | foo <- function(x) x^2 |
View(foo) |
Opens foo.R with deparsed source |
| View from stale LSP session | Open + close session, then run View() | View(foo) |
Shows error in tab if session is invalid |
@testlabauto @jonvanausdeln This involves a series of tests, and I have the vague impression that things could go wrong depending on how user has environments and R versions setup, so I think we should automate this (added a tag). To make it easier, I put some examples based on the notes from the Ark PR. Please note that this table is not exhaustive, and that things may behave differently depending on R versions/environments.