I have a file, test.R :
somefunc<-function(){
print("Hello")
print("World")
}
somefunc()
print("The End")
I want to set a tracepoint on the first line of somefunc. So I try,
> trace(what='somefunc', tracer=browser, at=1)
Error in getFunction(what, where = whereF) : no function ‘somefunc’ found
No traceback available
Ok, so it is not in the namespace. Let's load the file (so the function is in our current namespace) and then set the tracepoint...
> source("test.R")
[1] "Hello"
[1] "World"
[1] "The End"
> trace(what='somefunc', tracer=browser, at=1)
[1] "somefunc"
Now run the file again.
> source("test.R")
[1] "Hello"
[1] "World"
[1] "The End"
Alas, the breakpoint isn't hit. Presumably, the act of loading the file again clobbered the previous function (and tracepoint) in namespace.
I can only hit the tracepoint, after I've loaded the file and called the function directly through the interpreter. E.g.
> source("test.R")
[1] "Hello"
[1] "World"
[1] "The End"
> trace(what='somefunc', tracer=browser, at=1)
[1] "somefunc"
> somefunc()
Tracing somefunc() step 1
Called from: eval(expr, p)
Browse[1]>
QUESTIONS :
How do I set a breakpoint on a function that is to be loaded / sourced in R?
How do I previously set tracepoints when reloading the sourced file?
NOTE :
I'm not looking for the 'I like RStudio, why don't you try using that?' answer.
Related
I would like to import a python file and then use a function within the python file; but it does not work (it only work for source_python). Is it suppose to be this way?
In python file called the_py_module.py includes this code:
def f1():
return "f one"
def f2():
return "f two"
R script
# Trying to import the python file, which appear to work:
reticulate::import("the_py_module")
Gives this output:
Module(the_py_module)
# But when calling the function:
f1()
I get error saying:
Error in f1() : could not find function "f1"
This works using source python script though.
reticulate::source_python("the_py_module.py")
f1()
Try the following approach:
> library(reticulate)
> my_module <- import(the_py_module)
> my_module$f1()
[1] "f one"
or, using your approach
> my_module_2 <- reticulate::import("the_py_module")
> my_module_2$f1()
[1] "f one"
I am using the R marmap package, and when I load the sid file, I keep getting this error:
sid<-readGEBCO.bathy(file="C:/Users/Desktop/GEBCO_2014_SID_2D_-72.2876_-29.6177_-71.2257_-28.392.nc", resolution=1)
I get the following error:
[1] "vobjtovarid4: error #F: I could not find the requsted var (or dimvar) in the file!" [1] "var (or dimvar) name: elevation"
[1] "file name: C:/Users/Susannah Buchan/Desktop/GEBCO_2014_SID_2D_-72.2876_-29.6177_-71.2257_-28.392.nc"
Error in vobjtovarid4(nc, varid, verbose = verbose, allowdimvar = TRUE) : Variable not found
I've posted an updated version of readGEBCO.bathy() on GitHub. Setting the new sid argument to TRUE should now do the trick:
library(devtools)
install_github("ericpante/marmap")
library(marmap)
sid<-readGEBCO.bathy(file="C:/Users/Desktop/GEBCO_2014_SID_2D_-72.2876_-29.6177_-71.2257_-28.392.nc", resolution=1, sid=TRUE)
This bugfix along with other minor improvements should be published on CRAN servers early July. Stay tuned!
The problem is simple and annoying. I cannot print russian text neither in console nor in file.
Input:
print("hello world")
print("привет мир")
Output:
> print("hello world")
[1] "hello world"
> print("привет мир")
[1] "ïðèâåò ìèð"
I would not use russian at all, but sometimes I get errors in russian, and since they are not readable, I have no way to treat those errors:
> load(swirl)
Error in load(swirl) : íåïðàâèëüíûé àðãóìåíò 'file'
I m trying to use rPython package to pass some arguments into python code and get results back. But for some reason I m getting weird encoding from my python code. Maybe someone has some hints to point me out.
Here is my simple code to test:
require(rPython)
#pass the test word 'audiention' (in ukrainian)
word<-"аудієнція"
python.assign("input", word)
python.exec("input = input.encode('utf-8')")
python.exec("print input") #the output in console is correct at this step: аудієнція
x<-python.get("input")
cat(x) # the output is: 0C4VT=FVO
Does anybody have some suggestions why the output of python.get is encoded weird?
My Sys.getlocale() output is:
Sys.getlocale()
[1] "LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=uk_UA.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=uk_UA.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=uk_UA.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=uk_UA.UTF-8;LC_IDENTIFICATION=C"
Thank you in advance for any hints!
I have recently built a new package based on the original rPython code called SnakeCharmR that addresses this and other problems rPython had.
A quick comparison:
> library(SnakeCharmR)
> py.assign("a", "'")
> py.get("a")
[1] "'"
> py.assign("a", "áéíóú")
> py.get("a")
[1] "áéíóú"
> library(rPython)
> python.assign("a", "'")
File "<string>", line 2
a =' [ "'" ] '
^
SyntaxError: EOL while scanning string literal
> python.assign("a", "áéíóú")
> python.get("a")
[1] "\xe1\xe9\xed\xf3\xfa"
You can install SnakeCharmR like this:
> library(devtools)
> install_github("asieira/SnakeCharmR")
Hope this helps.
I have sourced my code and am doing debug(myfun1) on the code below. As seen in the image, I have a breakpoint set at line 13.
My code is as follows
myfun1 = function()
{
print("This is myfun1, before calling the myfun2()")
myfun2()
print("This is myfun1, after calling the myfun2()")
print("Does the debugger come back to this point")
}
myfun2 = function()
{
print("This is myfun2, before calling the myfun3()")
myfun3()
print("This is myfun2, after calling the myfun3()")
print("This is the last but one line of myfun2()")
print("This is the last line of myfun2()")
}
myfun3 = function()
{
print("This is myfun3")
}
These are the sequence of steps I use for my debugging
I start with myfun1 and then do 's' on line4 to step inside myfun2()
The debugger directly takes me to line 13 since I have a breakpoint
set
Now I do 'n' within myfun2() a couple of times which takes me to the
subsequent lines, ie line14 and line15 resp.
But, if I do 'n' on line 15, I expect it to go back to myfun1(). However, it just exits(I was able to get it to go back to myfun1()
by setting a breakpoint in myfun1 after myfun2() is called(say in line5 or line6), but shouldn't it
return even otherwise?)
See my debug log below
> debugSource('~/Desktop/foo1.R')
> debug(myfun1)
> myfun1()
debugging in: myfun1()
debug at ~/Desktop/foo1.R#2: {
print("This is myfun1, before calling the myfun2()")
myfun2()
print("This is myfun1, after calling the myfun2()")
print("Does the debugger come back to this point")
}
Browse[2]> c
[1] "This is myfun1, before calling the myfun2()"
[1] "This is myfun2, before calling the myfun3()"
[1] "This is myfun3"
Called from: eval(expr, envir, enclos)
Browse[1]> n
debug at ~/Desktop/foo1.R#13: print("This is myfun2, after calling the myfun3()")
Browse[2]> n
[1] "This is myfun2, after calling the myfun3()"
debug at ~/Desktop/foo1.R#14: print("This is the last but one line of myfun2()")
Browse[2]> n
[1] "This is the last but one line of myfun2()"
debug at ~/Desktop/foo1.R#15: print("This is the last line of myfun2()")
Browse[2]> n
[1] "This is the last line of myfun2()"
[1] "This is myfun1, after calling the myfun2()"
[1] "Does the debugger come back to this point"
exiting from: myfun1()
I'm assuming the GUI for RStudio is just a wrapper to the basic debugging functions in R. Setting the breakpoint by clicking on the margin would be equivalent to calling setBreakpoint().
These functions untimely call trace() which operate on a function level. That is, the tracer replaces the current function with a new function that enables the tracing; and that replacement function include an on.exit call to stop the tracing when the function is complete. So after myfun2 exits, the tacser is disabled so you can't debug inside the functions that call myfun2 (is myfun2).
If you need to debug at a higher level, set your breakpoint at a higher level. I'm not sure if you can change your debugger in the RStudio GUI, but if you call trace() yourself you can call recover() rather than browser() which will allow you to jump to different parts of the call stack.