My question is some packages share the same function name. How can I tell R which package that I want to use this function from?
I tried to load the package that I wanted to use again in the code but it still did not work. My case is the select in MASS and dplyr. I want to use dplyr but the error is always unused argument...
You can use the :: operator:
iris %>%
head(n = 3) %>%
dplyr::select(Sepal.Length)
See here for details.
Or detach MASS ala this post.
Related
Everytime I load the dplyr package the console shows a warning message.
warning message Some objetcs are masked from other packages. I think this is because the objects have the same name. For example:
Filter has this usage in dplyr packagefilter(.data, ..., .preserve = FALSE)
Filter has this usage in stats package filter(x, filter, method = c("convolution", "recursive"), sides = 2, circular = FALSE, init)
How can I unmask the filter object from stats package if I need to use it?
Regards
You are correct that they are simply packages that share the same name. The comments above basically answer the question already. Theoretically if you have a conflict of functions you want to avoid, you can also select which you prefer, like so:
library(conflicted)
conflict_prefer("slice", # the function
"dplyr") # the package
And R will tell you which it will use as your primary:
[conflicted] Will prefer dplyr::slice over any other package
However that is an extra step and I prefer usually to name it explicitly like dplyr::slice as mentioned in the comments instead.
I'm running an example in R, going through the steps and everything is working so far except for this code produces an error:
words <- dtm %>%
as.matrix %>%
colnames %>%
(function(x) x[nchar(x) < 20])
Error: could not find function "%>%"
I don't understand what the benefit of using this special operator
%>% is, and any feedback would be great.
You need to load a package (like magrittr or dplyr) that defines the function first, then it should work.
install.packages("magrittr") # package installations are only needed the first time you use it
install.packages("dplyr") # alternative installation of the %>%
library(magrittr) # needs to be run every time you start R and want to use %>%
library(dplyr) # alternatively, this also loads %>%
The pipe operator %>% was introduced to "decrease development time and to improve readability and maintainability of code."
But everybody has to decide for himself if it really fits his workflow and makes things easier.
For more information on magrittr, click here.
Not using the pipe %>%, this code would return the same as your code:
words <- colnames(as.matrix(dtm))
words <- words[nchar(words) < 20]
words
EDIT:
(I am extending my answer due to a very useful comment that was made by #Molx)
Despite being from magrittr, the pipe operator is more commonly used
with the package dplyr (which requires and loads magrittr), so
whenever you see someone using %>% make sure you shouldn't load dplyr
instead.
On Windows: if you use %>% inside a %dopar% loop, you have to add a reference to load package dplyr (or magrittr, which dplyr loads).
Example:
plots <- foreach(myInput=iterators::iter(plotCount), .packages=c("RODBC", "dplyr")) %dopar%
{
return(getPlot(myInput))
}
If you omit the .packages command, and use %do% instead to make it all run in a single process, then works fine. The reason is that it all runs in one process, so it doesn't need to specifically load new packages.
One needs to install magrittr as follows
install.packages("magrittr")
Then, in one's script, don't forget to add on top
library(magrittr)
For the meaning of the operator %>% you might want to consider this question: What does %>% function mean in R?
Note that the same operator would also work with the library dplyr, as it imports from magrittr.
dplyr used to have a similar operator (%.%), which is now deprecated. Here we can read about the differences between %.% (deprecated operator from the library dplyr) and %>% (operator from magrittr, that is also available in dplyr)
The pipe operator is not available in base R. You need to load one of the following packages to use it: dplyr, tidyverse or magrittr
Anyone else stumbling upon this for calculating powers of matrices please install this library (dplyr alone not correct)
library(expm)
This question already has answers here:
R: use magrittr pipe operator in self written package
(4 answers)
Closed 5 years ago.
I use dplyr a lot in various functions which I am putting together into a package.
I am not supposed to use library(dplyr) ever so I am trying to double colon everything. However I cannot seem to get the dplyr version right. When I do this for example:
SurveillanceLastToNow <- function(x, A_thing, Date) {
x %>% dplyr::arrange_(A_thing, Date) %>%
dplyr::group_by_(A_thing) %>% dplyr::mutate(diffDate = difftime(Sys.Date(),
last(Date), units = "days"))
}
I get the error:
could not find function "%>%"
So my questions are
Do I need to magrittr::%>% all the way through?....surely not
Given how much I use dplyr, including most of its functions, how do I just load the whole thing on installing the package rather than :: everywhere
Basically I'm looking for the laziest way to use all the dplyr functions in my package
You can simply put dplyr into the "depends" field of description file of your package, or if you don't want attach the package into the search path, you can put it into "imports" of description, but add a line import(dplyr) in the namespace file.
I recently downloaded googlesheets via
devtools::install_github("jennybc/googlesheets")
and experience some difficulties. When running the script as mentioned in
https://github.com/jennybc/googlesheets I get always:
Error: could not find function "%>%"
How can I solve that problem?
Reproducible example:
Download:
devtools::install_github("jennybc/googlesheets")
require(googlesheets)
Data:
gap_key <- "1HT5B8SgkKqHdqHJmn5xiuaC04Ngb7dG9Tv94004vezA"
copy_ss(key = gap_key, to = "Gapminder")
gap <- register_ss("Gapminder")
Error occurs:
oceania_csv <- gap %>% get_via_csv(ws = "Oceania")
Load the dplyr package first, which provides the %>% operator. This is noted here in the README you link to (suppressMessages is optional):
googlesheets is designed for use with the %>% pipe operator and, to a lesser extent, the data-wrangling mentality of dplyr. The examples here use both, but we'll soon develop a vignette that shows usage with plain vanilla R. googlesheets uses dplyr internally but does not require the user to do so.
library("googlesheets")
suppressMessages(library("dplyr"))
You can install dplyr with
install.packages("dplyr")
See here for more about the pipe operator (%>%).
I'm running an example in R, going through the steps and everything is working so far except for this code produces an error:
words <- dtm %>%
as.matrix %>%
colnames %>%
(function(x) x[nchar(x) < 20])
Error: could not find function "%>%"
I don't understand what the benefit of using this special operator
%>% is, and any feedback would be great.
You need to load a package (like magrittr or dplyr) that defines the function first, then it should work.
install.packages("magrittr") # package installations are only needed the first time you use it
install.packages("dplyr") # alternative installation of the %>%
library(magrittr) # needs to be run every time you start R and want to use %>%
library(dplyr) # alternatively, this also loads %>%
The pipe operator %>% was introduced to "decrease development time and to improve readability and maintainability of code."
But everybody has to decide for himself if it really fits his workflow and makes things easier.
For more information on magrittr, click here.
Not using the pipe %>%, this code would return the same as your code:
words <- colnames(as.matrix(dtm))
words <- words[nchar(words) < 20]
words
EDIT:
(I am extending my answer due to a very useful comment that was made by #Molx)
Despite being from magrittr, the pipe operator is more commonly used
with the package dplyr (which requires and loads magrittr), so
whenever you see someone using %>% make sure you shouldn't load dplyr
instead.
On Windows: if you use %>% inside a %dopar% loop, you have to add a reference to load package dplyr (or magrittr, which dplyr loads).
Example:
plots <- foreach(myInput=iterators::iter(plotCount), .packages=c("RODBC", "dplyr")) %dopar%
{
return(getPlot(myInput))
}
If you omit the .packages command, and use %do% instead to make it all run in a single process, then works fine. The reason is that it all runs in one process, so it doesn't need to specifically load new packages.
One needs to install magrittr as follows
install.packages("magrittr")
Then, in one's script, don't forget to add on top
library(magrittr)
For the meaning of the operator %>% you might want to consider this question: What does %>% function mean in R?
Note that the same operator would also work with the library dplyr, as it imports from magrittr.
dplyr used to have a similar operator (%.%), which is now deprecated. Here we can read about the differences between %.% (deprecated operator from the library dplyr) and %>% (operator from magrittr, that is also available in dplyr)
The pipe operator is not available in base R. You need to load one of the following packages to use it: dplyr, tidyverse or magrittr
Anyone else stumbling upon this for calculating powers of matrices please install this library (dplyr alone not correct)
library(expm)