This question already has answers here:
Load multiple packages at once
(10 answers)
Closed 3 years ago.
Instead of loading 30 packages each with the library function is it possible to do this in a loop?
pckgs = c("readr", "dplyr")
sapply(pckgs, library)
Background:
Before loading, i test if the packages are installed. For doing so i already have the package names in the form of c("readr", ..., "dplyr") and was wondering if i can also load the package in a loop instead of writing 30 times library().
What i tried:
I simplified to one package:
sapply("readr", library)
sapply("readr", function(lib) library(lib))
sapply("readr", function(lib) library(get(lib)))
Spoiler:
I wanted to post this question and then decided to check for a "force character" in the parameter and got lucky. (It is a bit weird asking a question and answering it yourself, but when i read this i felt motivated enough :) https://stackoverflow.com/help/self-answer
The parameter character.only can be used for that.
Example:
pckgs = c("readr", "dplyr")
sapply(pckgs, library, character.only = TRUE)
Related
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you you determine the namespace of a function?
I don't know how to do this... How do you know the package name for a certain function in R? I would like to have a function that given the name of a function, returns the name of the package that owns it. Any suggestion?
There may be better solutions, but find("functionname") seems to work reasonably well? However, it only works for loaded packages.
> find("strwidth")
[1] "package:graphics"
> find("qplot")
character(0)
> library(ggplot2)
> find("qplot")
[1] "package:ggplot2"
>
(If you need the raw name of the package you can use gsub("^package:","",results))
(The answers to the previous question linked by Andrie include this answer; they don't give the bit about gsub, and they all seem to share the issue of not finding non-loaded packages.)
Here's a quick hack to find functions even in non-loaded packages:
findAllFun <- function(f) {
h <- help.search(paste0("^",f,"$"),agrep=FALSE)
h$matches[,"Package"]
}
findAllFun("qplot")
## "ggplot2"
findAllFun("lambertW")
## "emdbook" "VGAM"
> findAllFun("xYplot")
## "Hmisc" "lattice"
If you need to find functions in non-installed packages (i.e. searching CRAN), then findFn from the sos package will be your friend.
I'm using Rstudio 2022.22.1 on MacOS Monterey 12.3.1.
I load libraries at the begininning by doing:
knitr::opts_chunk$set(echo = TRUE)
library("tidyverse", "here", "magrittr")
library("pastecs", "psych")
## dlf<-read.delim("data/DownloadFestival(No Outlier).dat", header=TRUE)
dlf<-here::here("data/DownloadFestival(No Outlier).dat") %>% readr::read_delim(col_names = TRUE)
I also check the thick for the library "psych" in the Packages section of RStudio.
The issue is that, from a certain point (after Knitting) I wasn't unable to use the command describe, this is the error:
could not find function "describe"
I could bypass this, by typing each time I use the function:
psych::describe
instead of describe alone
How can I use describe without specifying the psych:: prefix each time ?
Your problem is that library("pastecs", "psych") isn't doing what you think. Weirdly enough, there isn't an obvious idiom for "load a bunch of packages at once": I wish there were an easier way to do this, but try
invisible(lapply(c("psych", "pastecs"), library, character.only = TRUE))
The answers to this question provide a bunch of different ways to load many packages at once (the accepted answer is the same as the one given here).
This question already has answers here:
Error: could not find function ... in R
(10 answers)
Closed 1 year ago.
I am not able to use is.square.matrix() function for my matrix in R.
library(tidyverse)
install.packages(c("readr", "dplyr", "haven"))
install.packages(c("psych"))
install.packages("matlib")
matr <- cov(na.omit(airquality))
is.square.matrix(matr)
I am getting error for this function could not find is.square.matrix. Can someone help mw in figuring out what I am missing here?
is.square.matrix seems to be from matrixcalc package
# // if not installed, then install the package and load it
#install.packages('matrixcalc')
library(matrixcalc)
is.square.matrix(matr)
[1] TRUE
If we don't know from which package it is loaded, then use ?? to do the check on R console and then it opens a webpage with the help pages of similar/exact functions
??is.square.matrix
gives
New to R and have the following question. I got the error below when I was trying to create wordcloud in R. Could anyone tell me what the error means and is there a workaround?
Error in .overlap(x1, y1, sw1, sh1, boxes) :
function 'dataptr' not provided by package 'Rcpp'
That is an error we are getting with the newest Rcpp (which uses a different initialization scheme and no user-facing library). Make sure you have
the current version of Rcpp
and a current / rebuilt version of wordcloud.
On my system, with a fresh install of wordcloud, it all works fine:
R> library(wordcloud)
Loading required package: Rcpp
Loading required package: RColorBrewer
R> example(wordcloud)
wrdcldR> wordcloud(c(letters, LETTERS, 0:9), seq(1, 1000, len = 62))
wrdcldR> if(require(tm)){
wrdcld+
wrdcld+ ##### from character #####
wrdcld+ wordcloud(
wrdcld+ "Many years ago the great British explorer George Mallory, who
wrdcld+ was to die on Mount Everest, was asked why did he want to climb
wrdcld+ it. He said, \"Because it is there.\"
[.... more omitted ...]
After a while, I got it.
1) As stated, reinstall the latest version of Rcpp is the solution.
2) On top of that, if you use a library other than wordcloud that do not load automatically RCPP, do not forget to include
library(Rcpp)
or
require(Rcpp)
on your code before
dyn.load("your_shared_lib.so")
Source:
building_shared_libs_with_Rcpp
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you you determine the namespace of a function?
I don't know how to do this... How do you know the package name for a certain function in R? I would like to have a function that given the name of a function, returns the name of the package that owns it. Any suggestion?
There may be better solutions, but find("functionname") seems to work reasonably well? However, it only works for loaded packages.
> find("strwidth")
[1] "package:graphics"
> find("qplot")
character(0)
> library(ggplot2)
> find("qplot")
[1] "package:ggplot2"
>
(If you need the raw name of the package you can use gsub("^package:","",results))
(The answers to the previous question linked by Andrie include this answer; they don't give the bit about gsub, and they all seem to share the issue of not finding non-loaded packages.)
Here's a quick hack to find functions even in non-loaded packages:
findAllFun <- function(f) {
h <- help.search(paste0("^",f,"$"),agrep=FALSE)
h$matches[,"Package"]
}
findAllFun("qplot")
## "ggplot2"
findAllFun("lambertW")
## "emdbook" "VGAM"
> findAllFun("xYplot")
## "Hmisc" "lattice"
If you need to find functions in non-installed packages (i.e. searching CRAN), then findFn from the sos package will be your friend.