Find the source file of a function in Firefox DevTools - firefox-developer-tools

In Chrome Devtools console, if I output a Function object, it shows the top few lines of the function. I can click on the function name in the output and it takes me to the file and line where the function were loaded from (or from the original source if theres a source map):
In the new Firefox Devtools (Quantum) is it possible to find the sourcefile and linenumber for the function? Clicking on it only reveals the Function object itself:
I found i can use Function.toSource() to view the source code in Firefox Devtools console . But it gives no hint about which file or line it's from:

The DevTools show a button to jump to the function's definition for quite some time now.
I cannot pinpoint in what version this feature got added but it must have been around Firefox 61 when the new Console UI got enabled.

Related

Why do some functions open in the read-only mode in RStudio?

I am debugging my scripts in RStudio and came across a strange behaviour. For all my working functions, when I Command + click (ctrl + click), I get to the script of that functions.
However, for my one function when I do that it opens only read only mode. Does that mean the function is buggy? How can I fix this behaviour?
It depends on how the function was loaded: when you source the whole file, RStudio associates the function with its source file, and can jump to its definition. If, by contrast, you’ve loaded the function by e.g. only executing a single code fragment, RStudio doesn’t know which source file the function is associated with.
If you then subsequently want to jump to its definition, RStudio creates a temporary file which contains a deparsed representation of the function, not the original source. And since that file isn’t the original source but rather a temporary, RStudio marks it as read-only.

stop() in package function doesn't end debug mode

I've created very simple package, using RStudio, to demonstrate my problems, the steps follow.
Create package with only one function
write <- function(text) {
print(text)
if (is.numeric(text))
stop("Text cannot be numeric.")
}
Build -> Install and Restart
In a new R Script, write following code
pckgname::write(1)
Result in Console:
tempPackage::write(1)
[1] 1
Error in tempPackage::write(1) : Text cannot be numeric.
Called from: tempPackage::write(1)
Browse[1]>
Problems
5a) I don't want to see "Called from: tempPackage::write(1)".
5b) More importantly, I don't want to end with "Browse[1]> " meaning debug mode is still open (also buttons like "Continue" and "Stop" are visible) so now I need to click on button "Continue" to finish debug mode to end on line with ">" but I want to end this debug mode without clicking on button "Continue" meaning when I run that one line code "tempPackage::write(1)" I want to see result in Console with finished debug mode.
Additional information:
When I use this stop in function directly in the same R Script (not called from a package), it works like I want.
Solution using following method also doesn't help with previous problems.
opt <- base::options(show.error.messages = F)
on.exit(base::options(opt))
When I found some solution e.g. on GitHub e.g. https://github.com/jakesherman/easypackages/blob/master/R/package.R and installed them and intentionally made an error then again I get what I wanted (debug mode ended and no message Called from shown).
Thank you for any help in advance.
I just did exactly as you instructed and I see the expected result:
pckgname::write(1)
[1] 1
Error in pckgname::write(1) : Text cannot be numeric.
I'm not sure what's wrong when you try it, but would recommend starting a fresh package in case something is not working correctly. You could even try reinstalling R and RStudio, as it worked first time for me and I can't think of why it wouldn't for anyone else.
For reference, here's exactly what I did:
I started the package from RStudio -> File -> New Project -> New Directory -> New Package
The only file I changed was hello.R - I added the code in your question exactly as-is.
I built the package with RStudio -> Build -> Build from source
And then installed it with install.packages("../pckgname_0.1.0.tar.gz", repos = NULL, type="source")
And loaded with library(pckgname)

How to open the viewer pane in browser?

Just a simple question that I couldn't find an answer. I just updated my R Studio and R, and one function that I used a lot was opening the results in Viewer pane in my browser.
For example, with the resulting tables from packages like sjPlot or expss, I used the "Show in new window" to visualize the tables in Chrome or other default browser. Don't know if this was set by a old package, but I can't get this result right now. Actually, when I click this option, nothing is happening.
When I change to my old R version 3.6.2, I can use the "show in new window", but can't make to work in the version 4.0.1.
Anyone would know how can I get this function again?
Thanks
options(viewr=NULL) should do the trick.
In my case, i use browseURL instead of rstudioapi::viewer open file in browser directly.
browseURL(sprintf("%s/test.html", tempdir()))
if your file not in tempdir(), you should copy it to tempdir() first:
system(sprintf("cp %s %s", test_file, tempdir()))

R Hide internal objects in package from autocomplete

I am developing a package in Rstudio and I am trying to save object as internal to the package so that the user cannot see it. I make a default package project in Rstudio called "testpackage" and then execute:
library(devtools)
test.hidden.object <- 1:5
use_data(test.hidden.object,internal = T,overwrite = T)
Then I build the package, which saves it to my library. Then I restart Rstudio, and execute:
library(testpackage)
test.hidden.object
It prints out: [1] 1 2 3 4 5
The environment is empty, executing:
ls()
prints out "character(0)"
From what I understand, it is not possible to hide an object in a package from the user if the user knows the name of object, and I don't want to do that. But what worries me is that the autocomplete functionally is able to find these objects.
In both Rstudio and the R console, if I load the package and then type "test.hid" and then press TAB I can see the object "test.hidden.object" as an option. Should autocomplete be able to reveal internal objects? Am I building the package incorrectly?
To fix this problem I have so far updated R, Rstudio, devtools, and I have manually created the sysdata.rda file myself instead of using "use_data", but each time I am able to see the internal objects with autocomplete.
I think you are mistaken in your description. Autocompletion in RStudio and other R front ends will only show symbols that are visible in the current context. Your users can't make use of symbols that aren't exported, so autocompletion won't display them.
You may see hidden symbols while editing files in your own package, because your package code can see hidden symbols. But your users won't.
Edited to add: I've just followed your instructions more closely, and managed to duplicate what you saw. The problem is that by default the NAMESPACE file declares everything to be public, regardless of the setting for internal. This looks like a devtools misunderstanding or bug.
To fix it, manually edit your NAMESPACE file to make sure only public symbols are exported.
2nd edit: The docs for devtools::use_data have been updated on Github. They now say "If TRUE, stores all objects in a single R/sysdata.rda file. Objects in this file follow the usual export rules. Note that this means they will be exported if you are using the common exportPattern()
rule which exports all objects except for those that start with .."

What is this error in RStudio: "breakpoints will be activated when the file or function is finished executing"?

I am trying to set a break point inside a function from a package I wrote. I am unsuccessful when clicking next to the line number in Rstudio, the error message looks like:
I am not executing anything. Reloading the package did not help either. What is this error and what can I do about it?
Check out this documentation from RStudio that explains what to do.
Basically, make sure you save your R file, then click the Source button on the toolbar:
Your breakpoint should then turn from a hollow red circle to a full one.
Usually running rm(list=ls()) will do the job.

Resources