How to avoid printing a package's author message? [duplicate] - r

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Library/package development - message when loading
I want to set up a web interface using Rapache; however, the underlying R code uses packages that display a quick message from the author. E.g., for data.table,
Quick start guide : vignette("datatable-intro") Homepage : http://datatable.r-forge.r-project.org/
Is there a way to avoid this? I tried suppressMessages(), and the quietly option to library(), but to no avail.
Thanks

For data.table, this was done in commit 233 (2011.06.11 01:04:27) :
"onAttach now uses packageStartupMessage so the banner can be suppressed by those annoyed by banners, whilst still being helpful to new users"
This is in v1.6.1 available from R-Forge, and may be released to CRAN soon.
I'll add a note to NEWS ...

The brute force way of suppressing all output and messages for chatty packages is to use sink:
t <- tempfile()
tcon <- file(t,open="w+")
sink(file=tcon,type='output')
sink(file=tcon,type='message')
require(YOURLIBRARY)
sink(NULL,type='output')
sink(NULL,type='message')
unlink(t)
TAKE THAT YOU CHATTY PACKAGE!

Related

Check if package is installed without loading [duplicate]

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
}

rtweet error message "data is not a data frame"

I am trying to use the rtweet package but get the following message when trying to use the search_tweets() function like so:
x <- search_tweets(q="football", n=100)
Searching for tweets...
Collected tweets!
Error: data is not a data frame
I couldn't find a lot of instruction on how to use the basic functions of the package, so maybe am I missing an intermediate step in between setting up the app token and grabbing tweets?
I'd currently recommend installing the development version on Github (https://github.com/mkearney/rtweet). Without seeing your session info and script, I wouldn't be able to tell you exactly what the problem is. My guess is something went wrong with your API token/oauth (see the Github link for token instructions) or you're missing a dependency. If it's the latter, you could make sure you have "dplyr" installed, but you're better off with the Github version anyway, which only has a few dependencies.
If that doesn't fix the problem, I'd be curious to know what's going on. The transition from twitteR to rtweet has sped bings up a bit, but the next CRAN release should include a few more functions and a lot more documentation and useful error messages. Until then, feel free to post any issues to Github (I'll try to check on here, too, but it probably won't be as frequent).
I would suggest trying x <- rtweet::search_tweets(q="football", n=100), resulting in:
>dim(x)
[1] 98 35
Additionally:
>class(x)
[1] "data.frame"

On-demand installation of packages [duplicate]

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.)

how to load a library dynamically? [duplicate]

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.

Building a package so that it loads other packages automatically in R [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to properly use functions from other packages in a R package
load data set automatically
I've built an R package and am now testing it. It requires several packages like scatterplot3d, gdata, etc.
How can I get those packages to load automatically, when someone loads my package?
Thanks!
Edit Re Comments: I've already set imports in the Description and I've set the namespaces file. I know this works because the examples I put in work when I do R CMD Check. However, if I just load up R, type library(mypackage) it gives me an error.
Edit: Solved, I moved the packages I had in the imports section to the depends section. Thank you!

Resources