This question already has an answer here:
How do I override a non-visible function in the package namespace?
(1 answer)
Closed 8 years ago.
I want to temporarily modify the function lattice:::print.trellis. I can use edit to open the function code in editor but changes will be discarded after exit the editor (not the R session). I also tried fix but met the following error:
> fix(lattice:::print.trellis)
Error in fix(lattice:::print.trellis) : 'fix' requires a name
>
Can anybody explain the error? Is there anyway to modify this invisible function conveniently and temporarily (only effective in the current session)?
BTW, the lattice library is already loaded.
There are functions assignInNamespace and fixInNamespace that allow doing what you say. There is also the edit argument to the trace function which will let you edit a function in place. Using trace has the advantage of making it easy to untrace and remove the changes that you made.
Related
This question already has answers here:
Elegant way to check for missing packages and install them?
(33 answers)
Closed 4 years ago.
The typical way a package developer is advised to check whether a user has installed a package is like this:
if (!requireNamespace("package")) {
stop("Please install package.")
}
requireNamespace loads the package (in the current scope?) and returns a TRUE/FALSE value. I need to check the install state of a package without loading the namespace.
The reason for this is because I am writing a knit_print S3 method (extending the knitr package) and the namespace I am checking for kableExtra has side effects outside of the context of my knit_print method that I want to avoid.
When loaded, kableExtra changes how subsequent calls to knitr::kable are formatted at the global level. It has good reasons for doing so, but I want to use kableExtra inside my S3 method and not have end users confused about why kable behaves differently after my knit_print method is called.
That's why I want to do the check for the namespace (and if kableExtra is not installed, just call knitr::normal_print) without loading the namespace.
Edit: To clarify why I don't think this is a duplicate of this question, those answers do not pay any special attention to whether the solution loads the package when it is installed. It turns out that some of the solutions do not load the package in question, but they are not clearly differentiated.
Use installed.packages.
if ("kableExtra" %in% rownames(installed.packages()) {
# do something
}
For a package I am using, I would like to fix part of the code. I have downloaded the "package source" from CRAN and have narrowed down where the problem is. I would like to edit the problem function in RStudio. I've tried using trace, but I am unable to call the desired function. The function I want to call is a helper function (not in the documentation) to the main function (which is in the documentation). Is there a way to edit the functions not in the documentation and used in the implementation?
If it helps, the package I am using is called RecordLinkage. I would like to change the function .toFF which is called by RLBigDataDedup and is in the file RLBigData-classes.r.
Now that I've established the problem by looking at the source code, I now want to edit it in RStudio so that I can run it when using the package... The best I can do is looking at RLBigDataDedup in RStudio, but I can't find the .toFF that is used within it.
Thats not working because the hidden function .toFF is not known in the global env because it is hidden. If you want to edit/debug hidden function you have to specify the where argument in trace() with the corresponding function. In your case it would be RLBigDataLinkage.
Regarding to the docs of trace, it is stated:
For “hidden” functions such as S3 methods in a namespace, where = *
typically needs to be specified as well
So for your answer this will work:
trace(".toFF", edit=T, where = RLBigDataLinkage)
try debug(.toFF) or debug(function_to_calls_.toFF) This will open the debug tool when the function is called.
This question already has answers here:
Load R package from character string
(2 answers)
Closed 6 years ago.
I would like to install/load packages from a different location that is default. I dont have admin privileges so I cant access my .rprofile from the control panel.
My thought was I could just make a different library function, so I dont have to type a lib.loc statement every time i want to install/load a function. This is what i think the "liBerty" function should look like.
liBerty <- function(a) {
require(a,lib.loc="C:\\Users\\bert\\Documents\\rpackages" )
}
liBerty(tm)
The error I am getting states "there is no package 'a'.". Is there a way i can write this function to accomplish my task?
The function needs to also be modified for installing packages
install.Bertages<-function(b){
install.packages(b,lib="C:\\Users\\bert\\Documents\\rpackages")
}
liBerty<-function(a){
require(a,lib.loc="C:\\Users\\bert\\Documents\\rpackages",
character.only=TRUE )
}
install.Bertages("lubridate")
liBerty("lubridate")
This question already has answers here:
Load R package from character string
(2 answers)
Closed 7 years ago.
Sorry for asking easy question. I am a R beginner. I tried to load a library run-time,
e.g.
x<-"snow"; library(eval(x))
Result:
Error in library(eval(x)) : 'package' must be of length 1.
I would appreciate it if anyone gave me some solutions.
Use character.only=TRUE. See the help page for library, with ?library.
> library(x, character.only=TRUE)
I'd recommend to use require instead of library.
require returns a logical indicating whether the package was successfully loaded, i.e. you can use it in constructs like
if (require (x, character.only = TRUE))
...
On contrast, library will by default stop with an error if the package is not available (you can change this behaviour by logical.return = TRUE, though).
In case the package is loaded already, and this part of code is executed often, speed may matter: require is almost 20x faster than library on my laptop if the package is loaded already. If not, it calls library.
This question already has an answer here:
ggplot's qplot does not execute on sourcing
(1 answer)
Closed 9 years ago.
Consider a source file of this form:
# initialize the function
f = function() {
# ...
}
# call the function
f()
In python, the import function would load and executes the file; however in R, the source command does initialize functions defined by the source file, but does not call the functions if they are called in the file.
Is there any R command (or option) to import and execute the file?
Thanks for your help.
?source states:
Description:
‘source’ causes R to accept its input from the named file or URL
or connection. Input is read and ‘parse’d from that file until
the end of the file is reached, then the parsed expressions are
evaluated sequentially in the chosen environment.
Therefore,
source is the function you are looking for,
I refute your claim - source works for me as per the documentation
If you are not seeing the documented behaviour then there must be a different problem that you are not telling us.
How are you deducing that source is not executing your f?
I can think of one scenario that you may not be aware of, which is documented in ?source, namely:
Details:
Note that running code via ‘source’ differs in a few respects from
entering it at the R command line. Since expressions are not
executed at the top level, auto-printing is not done. So you will
need to include explicit ‘print’ calls for things you want to be
printed (and remember that this includes plotting by ‘lattice’,
FAQ Q7.22).
Not seeing the output from objects called by name or grid graphics-based plots not showing are symptoms of auto-printing not being active as the evaluation is not occurring at the top-level. You need to explicitly print() objects, including grid-based graphics like Lattice and ggplot2 figures.
Also note the print.eval argument, which will default to
> getOption("verbose")
[1] FALSE
which may also be of use to you.