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")
Related
This question already has answers here:
Error: could not find function ... in R
(10 answers)
Closed 10 days ago.
I'm having real trouble running the function write.xlsx - the error "could not find function "write.xlsx"
I've already tried to:
Install the packages xlsx, readxl, writexl, XLConnect but no one of these is working.
Install Java JRE, but it's not working as well
Have you guys ever had a similar problem before?
I'm really needing to start running those flows which are properly working in other machines.
PS: I'm a beginner in the R coding
After installing the package xlsx you should also load the library in order to use the function, like this:
library(xlsx)
If you're just going to use the function one time you can call it without loading the library first like this:
xlsx::write.xlsx(data, file = "file.xlsx")
Hope this helps
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
}
This question already has answers here:
How to find all functions in an R package?
(6 answers)
Closed 7 years ago.
I would like to know if there is a command, using which one can view all the functions that are built into an R package.
For example, let's say I loaded a package into environment:
require(dplyr)
Now, I would like to get a list of all the functions present in the dplyr package.
Is there any way to get such a list?
You can use lsf.str.
For instance:
lsf.str("package:dplyr")
To list all objects in the package use ls
ls("package:dplyr")
Note that the package must be attached.
To see the list of currently loaded packages use
search()
Alternatively calling the help would also do, even if the package is not attached:
help(package = dplyr)
Finally, you can use RStudio which provides an autocomplete function. So, for instance, typing dplyr:: in the console or while editing a file will result in a popup list of all dplyr functions/objects.
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.
This question already has answers here:
Elegant way to check for missing packages and install them?
(33 answers)
Closed 9 years ago.
Is there a "standard" way to load a package, and install it if it isn't installed yet? Something like
if (!is.installed(package))
install(package)
library(package)
(pseudocode!), encapsulated in a neat function?
I'm usually having a hard time after wiping my private site library, which I do every now and then. If my scripts all used this "install-on-demand" facility, this would just happen automatically.
Dason K. and I have a package in the works on GitHub that needs some testing and a bit of cleaning and eventually will be pushed to CRAN. The function p_load in the package does this.
library(devtools)
install_github("trinker/pacman")
I see that other answers have been given but my preference would be:
if ( !require('pkg') ) { install.packages('pkg', dependencies=TRUE);
require('pkg') }
If you want to suppress the warning, then add quietly=TRUE to the first require call. I suppose you could bundle this into a function, called, what? insist?
insist <- function(pkg){
if ( !require(pkg, character.only=TRUE) ) {
install.packages(as.character(pkg), dependencies=TRUE)
require(pkg, character.only=TRUE) }
}
(My major stumbling block: The first argument to require didn't seem to get evaluated unless character.only=TRUE. Took me several reads of the ?require page to get this idea. Just slow, I guess.)