Why do I get an error when loading the package tidyverse? - r

When I run library(tidyverse) I see a list of all the attached packages and below that there is an error message:
"Error in -library(tidyverse) : invalid argument to unary operator."
Why?
I am using the latest version of tidyverse (1.2.1).

Because you are not actually doing: library(tidyverse) but -library(tidyverse), note the hyphen (or minus sign) just before the call to library.
The call to library actually runs fine but then R tries to pass the returned value of library (a character vector with the list of attached packages) to the unary operator -, hence the error.
Just fix that typo, i.e. remove the -. :)

Related

R error message for unused argument (writexl)

I have simple code
library(writexl)
write_xlsx(dataset, "C:/adhoc/my_data.xlsx")
The above works, however when I try
library(writexl)
write_xlsx(dataset, "C:/adhoc/my_data.xlsx", append=TRUE)
I get an unused argument error. How can I fix?
If you look at the documentation ?write_xlsx there is not an append argument defined for this function, thus the error. It has been raised as a possible feature for that package and deemed not possible. You could read the spreadsheet, do the appending in R, and then re-write the full version.
Alternatively, perhaps you would be served by write.xlsx() from the xlsx package which does have this argument and might be suitable for your purposes?

How do I create a package name with underscore?

I get this error when running devtools::check().
Error in processx::run(bin, args = real_cmdargs, stdout_line_callback = real_callback(stdout), :
System command error
The check runs and completes just fine if I remove the underscore from my package name. How do I create a package name with underscore?
The manual on writing R extensions describes that this is not permitted.
The mandatory ‘Package’ field gives the name of the package. This should contain only (ASCII) letters, numbers and dot, have at least two characters and start with a letter and not end in a dot. If it needs explaining, this should be done in the ‘Description’ field (and not the ‘Title’ field).
https://cran.r-project.org/doc/manuals/r-devel/R-exts.html#Creating-R-packages
See also this question: What's a good R package name?

R, How do I use here() within parse()

I know how much you love the here() package... I'm just starting to catching on.
I am trying to write an (relatively) automated code for cleaning data and I would like to use here() and parse(). But parse() doesn't seem to like having here() within it.
cleaning <- parse(file = here("folder", "source-code.R"))
and I get the error:
Error in here("folder", "source-code.R"):
unused argument ("source-code.R")
If I set my working directory as the folder containing "source-code.R" and remove the here() argument, the process works just fine.
also, I've tried removing the "folder" and just calling the "source-code.R", but then I get the error:
Error in parse(file = here("source-code.R")) :
'file' must be a character string or connection
thanks for your help!
Thanks to MrFlick who recognized the naming conflict. Using here::here() solved it!

R documentation and help page - An example with the pipe operator [duplicate]

This question already has answers here:
What does %>% function mean in R?
(6 answers)
Closed 7 years ago.
I was wondering about this %>% so I typed ?%>%into the console and got:
Error: unexpected SPECIAL in "?%>%". Fair enough. So I typed ?"%>%" and got the following:
%>% package:tidyr R Documentation
Pipe operator
Description:
See ‘%>%’ for more details.
Usage:
lhs %>% rhs
Okay then! Typing ?'%>%', however, lead me to the exact same help page. What's my mistake?
No need to explain the pipe operator, I googled it by now, but what would I have done, was I on a train with no internet?
Edit. I see this question was somewhat misleading, so let me rephrase. How do I find the appropriate documentation for the %>% pipe operator in R's help documentation? What exactly does it mean if a documentation tells me "See ... for more details"? What am I supposed to do then?
%>% is an binary infix operator, you need to use it with operands, or you'll recieve an error. Hence the specified usage:
lhs %>% rhs
In you case above, writing ?%>% will attempt to call %>% using ? as your lhs operator. Hence the error ... unexpected SPECIAL .... If you, on the other hand, wrap your operator in '...' or "...", it will use the ? prefix as you intend: showing the help section for that operator.
As an example, try the following in your console:
?< <-- Error: unexpected '<' in "?<"
?'<' <-- OK
?"<" <-- OK
(after first edit of question)
Now, regarding your updated question, where to find appropriate documentation for the pipe operator, I quote this site
Although not required, the tidyr and dplyr packages make use of the
pipe operator %>% developed by Stefan Milton Bache in the R package
magrittr. Although all the functions in tidyr and dplyr can be used
without the pipe operator, one of the great conveniences these
packages provide is the ability to string multiple functions together
by incorporating %>%.
Hence, an appropriate place to begin would be in the documentation for the magrittr package:
https://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html
... ?magrittr or ??magrittr from your console.
(after second edit of question)
Finally, if we take a look at how the associated tidyr manual page for the pipe operator is generated, we get our answer to your final edit:
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{\%>\%}
\alias{\%>\%}
\title{Pipe operator}
\usage{
lhs \%>\% rhs
}
\description{
See \code{\link[magrittr]{\%>\%}} for more details.
}
\keyword{internal}
Hence, your help page is supposed to contain a link to the magrittr help page for the pipe operator, but if you are running from e.g. terminal (are you?), then you will only be shown plain text, in so loosing (or at least not seeing) the link.
%>% originally comes from the matrittr package. You can find documentation about the pipe operator there (Github page).

Error when installing: cannot coerce type 'closure' to vector of type 'character'

Tried to install all rattle related packages by typing:
install.packages(rattle, dependencies = c("Depends","Suggests"))
and got this
Installing package into ‘C:/Users/Hooman/Documents/R/win-library/3.1’
(as ‘lib’ is unspecified)
Error in as.character(x) :
cannot coerce type 'closure' to vector of type 'character'
I already installed two individual packages in that folder and had no issue.
#BenBolker posted an answer as a comment, perhaps because he is hoping that you will stare at the difference between your code and his and get a Zen-moment. He also thinks, being the modest gentleman that he is, that it's too simple to warrant any upvotes. So I am feeling guilty that any upvotes are his rather than mine. I will now try to legitimately earn any upvotes.
The reason you got the error was the the install.packages function expects a character object, while you gave it the unquoted expression rattle. Ben uses a single element character vector "rattle", thus conforming to the requirement of the function. Unlike some functions install.packages is not equipped to provide "non-standard evaluation" of its first argument.
R is a funny language at times with some inconsistent evaluation conventions. There are several functions where you can provide an unquoted expression and have it automatically converted to character. The list includes library, and its cousin, require, as well as help, subset, and $. These are considered "non-standard" evaluation by knowledgeable users, and they can have their pitfalls in programming. The error message tells you that R tried to convert what it "thought" would be a language object, a closure (which loosely is an R and LiSP term for function), to a character and did not succeed. You can see the same error with this console interaction:
> as.character(mean)
Error in as.character(mean) :
cannot coerce type 'closure' to vector of type 'character'
If you look at the library function mentioned by Ben and scroll down past the internal function definitions you eventually get to the mechanism whereby library avoids that error:
if (!character.only)
package <- as.character(substitute(package))
This would also avoid the error, which I intentionally used as an example:
> as.character(substitute(mean))
[1] "mean"
The substitute function is doing processing on a language element taken to be a closure and the resulting object an R "name" has an as.character method.

Resources