Catch Error/Warning from Install.Packages() - R [duplicate] - r

Consider the following usage:
tryCatch(log("a"), error = function(e) NULL)
#NULL
Now I'm trying to do essentially the same, but in a more complicated fashion. I have two network repositories, and I'd like to install packages from the second if the first is not available for some reason. Here's how I do it:
pkg_location <- c("file://main_repo", "file://extra_repo")
lapply(pkg_location, function(repo)
{
tryCatch(install.packages("my-cool-package",
contriburl = repo, dependencies = TRUE),
error = function(e) NULL)
})
And I'm expecting a list of NULLs. However, the error is not suppressed:
Installing package into ‘...’
(as ‘lib’ is unspecified)
Warning in install.packages :
cannot open compressed file '//extra_repo/PACKAGES',
probable reason 'No such file or directory'
Error in install.packages : cannot open the connection
[[1]]
NULL
[[2]]
NULL
It seems like install.packages somehow ignores the mechanism. How is that possible, why is that happening and how can I approach the problem?
Here's sessionInfo, probably worth noting I'm running RStudio 0.98.977.
> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.1.2

RStudio does not exectute the normal install.packages but instead does its own thing:
look at the code in RStudio:
> install.packages
function (...)
.rs.callAs(name, hook, original, ...)
<environment: 0x3e4b478>
> .rs.callAs
function (name, f, ...)
{
withCallingHandlers(tryCatch(f(...), error = function(e) {
cat("Error in ", name, " : ", e$message, "\n", sep = "")
}), warning = function(w) {
cat("Warning in ", name, " :\n ", w$message, "\n", sep = "")
invokeRestart("muffleWarning")
})
}
<environment: 0x3bafa38>
weird code, it recalls itself ...
i was expecting a .Primitive() somewhere
> sum
function (..., na.rm = FALSE) .Primitive("sum")
but it is an ugly RStudio hack. if you look at install.packages in normal R you get:
head(install.packages) # it is really long :P
1 function (pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos,
2 type), method, available = NULL, destdir = NULL, dependencies = NA,
3 type = getOption("pkgType"), configure.args = getOption("configure.args"),
4 configure.vars = getOption("configure.vars"), clean = FALSE,
5 Ncpus = getOption("Ncpus", 1L), verbose = getOption("verbose"),
6 libs_only = FALSE, INSTALL_opts, quiet = FALSE, keep_outputs = FALSE,
....

I'm going to suggest closing as off-topic because this is an RStudio problem. Basically, tryCatch is catching the error, but RStudio's error handler prints the error anyway. Thus the reason you're getting a return value:
[[1]]
NULL
[[2]]
NULL
This means tryCatch works. RStudio just prints caught errors weirdly.

Use the namespaced invocation:
utils::install.packages()

Related

Error in rep(" ", len) : invalid 'times' argument

library(OneR)
library(RWeka)
loan_train <- read.csv("loan_train.csv")
loan_test <- read.csv("loan_test.csv")
loan_train <- optbin(loan_train, method = "logreg", na.omit = TRUE)
loan_test <- optbin(loan_test, method = "logreg", na.omit = TRUE)
#Task 1
loan_1R <- OneR(bad_loans ~ ., data = loan_train)
loan_1R
loan_JRip <- JRip(bad_loans ~ ., data = loan_train)
loan_JRip
Need some help with my code. I am able to run everything but for some reason, every time I print loan_1R, it gives me an error. Tried using traceback() but have no idea what it means. My csv file can be in the link below.
https://drive.google.com/file/d/1139FUSXUc_fdzgtKAleo5bGAtjcVGoRC/view?usp=sharing
Error in rep(" ", len) : invalid 'times' argument
In addition: Warning message:
In max(nchar(names(model$rules))) :
no non-missing arguments to max; returning -Inf
> traceback()
3: cat("If ", model$feature, " = ", names(model$rules[iter]), rep(" ",
len), " then ", model$target, " = ", model$rules[[iter]],
"\n", sep = "")
2: print.OneR(x)
1: function (x, ...)
UseMethod("print")(x)
> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_Singapore.1252 LC_CTYPE=English_Singapore.1252
[3] LC_MONETARY=English_Singapore.1252 LC_NUMERIC=C
[5] LC_TIME=English_Singapore.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RWeka_0.4-37 OneR_2.2
loaded via a namespace (and not attached):
[1] compiler_3.4.1 tools_3.4.1 grid_3.4.1 rJava_0.9-9 RWekajars_3.9.2-1
After hours of testing i found out the problem but I have no idea why it is so. Think that it has something to do with the library(RWeka) package.... Placing library(RWeka) after the OneR code seemed to make it run. But this means i encounter the error only once i run the library(RWeka). Any workaround this?
library(OneR)
loan_train <- read.csv("loan_train.csv")
loan_test <- read.csv("loan_test.csv")
loan_train <- optbin(loan_train, method = "logreg", na.omit = TRUE)
loan_test <- optbin(loan_test, method = "logreg", na.omit = TRUE)
#Task 1
loan_1R <- OneR(bad_loans ~ ., data = loan_train)
loan_1R
library(RWeka)
loan_JRip <- JRip(bad_loans ~ ., data = loan_train)
loan_JRip

error saving: Error in gzfile(file, "wb") : cannot open the connection

I am trying to run a LDA topic analysis on Rstudio 3.3.0. I am at the following step but keep getting the error:
Error in gzfile(file, "wb") : cannot open the connection
In addition: Warning message:
In gzfile(file, "wb") :
cannot open compressed file 'results/Gibbs_5_1.rda', probable reason 'No such file or directory'
There is a problem while saving.
D <- nrow(data)
folding <- sample(rep(seq_len(10), ceiling (D))[seq_len(D)])
for (k in topics)
{
for (chain in seq_len(10))
{
FILE <- paste("Gibbs_", k, "_", chain, ".rda", sep = "")
training <- LDA(data[folding != chain,], k = k,
control = list(seed = SEED,
burnin = BURNIN, thin = THIN, iter = ITER, best= BEST),
method = "Gibbs")
best_training <- training#fitted[[which.max(logLik(training))]]
testing <- LDA(data[folding == chain,], model = best_training,
control = list(estimate.beta = FALSE, seed = SEED,
burnin = BURNIN,
thin = THIN, iter = ITER, best = BEST))
save(training, testing, file = file.path("results", FILE))
}
}
There is enough workspace on my computer, and I tried to restart r several times and yes I looked at the other questions but none of the solutions seem to work.
> sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)
locale:
[1] C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] topicmodels_0.2-4 wordcloud_2.5 RColorBrewer_1.1-2 slam_0.1-35 SnowballC_0.5.1
[6] tm_0.6-2 NLP_0.1-9
loaded via a namespace (and not attached):
[1] modeltools_0.2-21 parallel_3.3.0 tools_3.3.0 Rcpp_0.12.5 stats4_3.3.0
I am a beginner in R and I follow a book to conduct the analysis for my master thesis.
Thanks!
The error message says it can't save the file. What is it trying to save? Looking at the code it looks like its trying to save in a folder called "results". Does this folder exist? Because if it doesn't, I get that error when I try and save something to a non-existent folder:
> save(iris, file=file.path("results","foo.rda"))
Error in gzfile(file, "wb") : cannot open the connection
In addition: Warning message:
In gzfile(file, "wb") :
cannot open compressed file 'results/foo.rda', probable reason 'No such file or directory'
If I create the folder then it works:
> dir.create("results")
> save(iris, file=file.path("results","foo.rda"))

install_github or install_bitbucket crashing R with segfault error

I'm using R on the university HPC cluster and now they have upgraded to R/3.3.0 I'm looking to install my code and supporting packages to match my local setup. However I'm having problems with the commands install_github and install_bitbucket to install my code (and another package I use).
These are the commands and errors from the R session:
> library(devtools)
> install_github("config-i1/smooth")
Downloading GitHub repo config-i1/smooth#master
from URL https://api.github.com/repos/config-i1/smooth/zipball/master
Installing smooth
*** caught segfault ***
address 0x4000000079, cause 'memory not mapped'
Traceback:
1: .Call(digest_impl, object, as.integer(algoint), as.integer(length), as.integer(skip), as.integer(raw), as.integer(seed))
2: `_digest`(c(list(repos, type), lapply(`_additional`, function(x) eval(x[[2L]], environment(x)))), algo = "sha512")
3: available_packages(repos, type)
4: package_deps(deps, repos = repos, type = type)
5: dev_package_deps(pkg, repos = repos, dependencies = dependencies, type = type, force_deps = force_deps, quiet = quiet)
6: install_deps(pkg, dependencies = dependencies, upgrade = upgrade_dependencies, threads = threads, force_deps = force_deps, quiet = quiet, ...)
7: install(source, ..., quiet = quiet, metadata = metadata)
8: FUN(X[[i]], ...)
9: vapply(remotes, install_remote, ..., FUN.VALUE = logical(1))
10: install_remotes(remotes, quiet = quiet, ...)
11: install_github("config-i1/smooth")
Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
I receive a similar error with: install_bitbucket("wellermatt/forecastR"
> sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Scientific Linux release 6.7 (Carbon)
locale:
[1] LC_CTYPE=C LC_NUMERIC=C
[3] LC_TIME=en_US.iso88591 LC_COLLATE=C
[5] LC_MONETARY=en_US.iso88591 LC_MESSAGES=en_US.iso88591
[7] LC_PAPER=en_US.iso88591 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.iso88591 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] devtools_1.11.1
loaded via a namespace (and not attached):
[1] withr_1.0.1 memoise_1.0.0 digest_0.6.9
I have tried to upgrade RCurl and devtools, but neither has helped. WOuld anybody be able to suggest a potential solution - I'm not able to find one on the web at the moment. I guess I can try to download the whole repository from github and then install from source that way but this doesn't help my workflow which currently pulls the latest version automatically.
---- EDIT -----
The cluster manager at Lancaster has tried the install also and has looked into the traceback. He reports as follows:
Let me know how that goes. I can replicate the crash myself (which may in itself be odd, as I expected your codebase to require authentication), but I’m none the wiser. Here’s the last few lines of strace (which logs systems calls only) leading up to the segfault:
read(3, "Package: forecastR\nType: Package"..., 16384) = 492
read(3, "", 12288) = 0
lseek(3, 0, SEEK_CUR) = 492
read(3, "", 4096) = 0
close(3) = 0
munmap(0x7f276cdfe000, 4096) = 0
stat("/tmp/RtmpjNlvQb/devtools2d0a1128ed80/wellermatt-forecastr-d5b58631b3a1/src", 0x7fff1a81bdb0) = -1 ENOENT (No such file or directory)
write(2, "Installing forecastR\n", 21) = 21
--- SIGSEGV (Segmentation fault) # 0 (0) ---
The failed stat() call may or may not be an issue – were you expecting your package to have src dir? If not, this may simply be an expected test.

devtools::install_github Error in function (type, msg, asError = TRUE) : <not set>

I'm trying to install a package via devtools::install_github for the first time
l#np350v5c:~$ R --vanilla
> library(devtools)
Attaching package: ‘devtools’
The following objects are masked from ‘package:utils’:
?, help
The following object is masked from ‘package:base’:
system.file
> search()
[1] ".GlobalEnv" "package:devtools" "package:stats"
[4] "package:graphics" "package:grDevices" "package:utils"
[7] "package:datasets" "package:methods" "Autoloads"
[10] "package:base"
but, eg
> install_github("devtools")
Installing github repo devtools/master from hadley
Downloading master.zip from https://github.com/hadley/devtools/archive/master.zip
Errore in function (type, msg, asError = TRUE) : <not set>
The output of traceback() is
> traceback()
12: fun(structure(list(message = msg, call = sys.call()), class = c(typeName,
"GenericCurlError", "error", "condition")))
11: function (type, msg, asError = TRUE)
{
if (!is.character(type)) {
i = match(type, CURLcodeValues)
typeName = if (is.na(i))
character()
else names(CURLcodeValues)[i]
}
typeName = gsub("^CURLE_", "", typeName)
fun = (if (asError)
stop
else warning)
fun(structure(list(message = msg, call = sys.call()), class = c(typeName,
"GenericCurlError", "error", "condition")))
}(77L, "<not set>", TRUE)
10: .Call("R_curl_easy_perform", curl, .opts, isProtected, .encoding,
PACKAGE = "RCurl")
9: curlPerform(curl = handle$handle, .opts = curl_opts$values)
8: make_request("get", hu$handle, hu$url, config = config)
7: GET(url, config)
6: (function (url, name = NULL, subdir = NULL, config = list(),
before_install = NULL, ...)
{
if (is.null(name)) {
name <- basename(url)
}
message("Downloading ", name, " from ", url)
bundle <- file.path(tempdir(), name)
request <- GET(url, config)
stop_for_status(request)
writeBin(content(request), bundle)
on.exit(unlink(bundle), add = TRUE)
install_local_single(bundle, subdir = subdir, before_install = before_install,
...)
})(dots[[1L]][[1L]], dots[[2L]][[1L]], subdir = NULL, config = list(),
before_install = function (bundle, pkg_path)
{
desc <- file.path(pkg_path, "DESCRIPTION")
DESCRIPTION <- readLines(desc, warn = FALSE)
if (any(DESCRIPTION == "")) {
DESCRIPTION <- DESCRIPTION[DESCRIPTION != ""]
}
cat(DESCRIPTION, file = desc, sep = "\n")
append_field <- function(name, value) {
if (!is.null(value)) {
cat("Github", name, ":", value, "\n", sep = "",
file = desc, append = TRUE)
}
}
append_field("Repo", conn$repo)
append_field("Username", conn$username)
append_field("Ref", conn$ref)
append_field("SHA1", github_extract_sha1(bundle))
append_field("Pull", conn$pull)
append_field("Subdir", conn$subdir)
append_field("Branch", conn$branch)
append_field("AuthUser", conn$auth_user)
}, dependencies = TRUE)
5: mapply(install_url_single, url, name, MoreArgs = list(subdir = subdir,
config = config, before_install = before_install, ...))
4: install_url(conn$url, subdir = conn$subdir, config = conn$auth,
before_install = github_before_install, ...)
3: FUN("devtools"[[1L]], ...)
2: vapply(repo, install_github_single, FUN.VALUE = logical(1), username,
ref, pull, subdir, branch, auth_user, password, auth_token,
..., dependencies = TRUE)
1: install_github("devtools")
I tryied with another repo but it behaves the same way.
Any hint?
> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=it_IT.UTF-8 LC_NUMERIC=C
[3] LC_TIME=it_IT.UTF-8 LC_COLLATE=it_IT.UTF-8
[5] LC_MONETARY=it_IT.UTF-8 LC_MESSAGES=it_IT.UTF-8
[7] LC_PAPER=it_IT.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=it_IT.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] devtools_1.5
loaded via a namespace (and not attached):
[1] digest_0.6.4 evaluate_0.5.1 httr_0.2 memoise_0.1 parallel_3.1.0
[6] RCurl_1.95-4.1 stringr_0.6.2 tools_3.1.0 whisker_0.3-2
Actually, I had the same issue and after installing RCurl it worked.
In order to install RCurl in ubuntu, write the following in your terminal:
sudo apt-get install libcurl4-openssl-dev
Then in R:
install.packages('RCurl')
And that's it!
If you can't install the appropriate version of libcurl, you can try using the "install_git" function instead of "install_github". For example,
library(devtools)
install_git("https://github.com/hadley/devtools.git")
That worked for me.
Sorry, this was a bug in the r-cran-rcurl package in Debian. We eventually corrected it in the Debian distribution unstable, testing, and more recently stable (Jessie 8.2). In Ubuntu it is fixed in wily. Sorry for the problem, and thanks for using Debian or its derivatives !

cannot INSTALL shinyapps

I encountered the following error when trying to install shinyapps on my ubuntu 13.04. Can anyone help? Thanks.
Call:
require(devtools)
devtools::install_github('rstudio/shinyapps')
Error:
Installing github repo(s) rstudio/shinyapps/master from hadley
Installing rstudio/shinyapps.zip from https://github.com/hadley/rstudio/shinyapps/archive/master.zip
Error in writeBin(content(request), bundle) :
can only write vector objects
Session Info:
R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=zh_CN.UTF-8
[4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=zh_CN.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=zh_CN.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=zh_CN.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] R.oo_1.15.8 R.methodsS3_1.5.2 XML_3.98-1.1 devtools_1.4.1
loaded via a namespace (and not attached):
[1] digest_0.6.3 evaluate_0.5.1 httr_0.2 memoise_0.1 parallel_3.0.2 RCurl_1.95-4.1
[7] stringr_0.6.2 tools_3.0.2 whisker_0.3-2
*trackback() prints the following: *
8: stop("can only write vector objects")
7: writeBin(content(request), bundle)
6: (function (url, name = NULL, subdir = NULL, config = list(),
before_install = NULL, ...)
{
if (is.null(name)) {
name <- basename(url)
}
message("Downloading ", name, " from ", url)
bundle <- file.path(tempdir(), name)
request <- GET(url, config)
stop_for_status(request)
writeBin(content(request), bundle)
on.exit(unlink(bundle), add = TRUE)
install_local_single(bundle, subdir = subdir, before_install = before_install,
...)
})(dots[[1L]][[1L]], dots[[2L]][[1L]], subdir = NULL, config = list(),
before_install = function (bundle, pkg_path)
{
desc <- file.path(pkg_path, "DESCRIPTION")
if (!ends_with_newline(desc))
cat("\n", sep = "", file = desc, append = TRUE)
append_field <- function(name, value) {
if (!is.null(value)) {
cat("Github", name, ":", value, "\n", sep = "",
file = desc, append = TRUE)
}
}
append_field("Repo", repo)
append_field("Username", username)
append_field("Ref", ref)
append_field("SHA1", github_extract_sha1(bundle))
append_field("Pull", pull)
append_field("Subdir", subdir)
append_field("Branch", branch)
append_field("AuthUser", auth_user)
})
5: mapply(install_url_single, url, name, MoreArgs = list(subdir = subdir,
config = config, before_install = before_install, ...))
4: install_url(url, name = paste(repo, ".zip", sep = ""), subdir = subdir,
config = auth, before_install = github_before_install, ...)
3: FUN("shinyapps"[[1L]], ...)
2: vapply(repo, install_github_single, FUN.VALUE = logical(1), username,
ref, pull, subdir, branch, auth_user, password, ...)
1: install_github(repo = "shinyapps", username = "rstudio")
Please have a look at the parameter list of install_github. Your call should be
install_github( repo = "shinyapps", username="rstudio" )
At least for devtools prior to version 1.4.1: https://github.com/hadley/devtools/blob/master/NEWS.md
I think you have another choice. Please download the package from https://github.com/hadley/rstudio/shinyapps/archive/master.zip to your local computer. Then install the package by call install_local(). For example,
install_local("~/Downloads/shinyapps-master.zip").
shinyapps-master.zip is the shinyapps package, and ~/Downloads/ is the path.
shinyapps package has been replaced by rsconnect which can be simply installed through CRAN in the regular way.
https://github.com/rstudio/shinyapps

Resources