R library(RCurl) behavior change on travis-ci - r

this line of code previously worked on travis-ci but now fails. works fine on appveyor
RCurl::getURL( "ftp://ftp.cdc.gov/pub/data/yrbs/" , ftp.use.epsv = TRUE, dirlistonly = TRUE )
here's the text of the error-
Error in function (type, msg, asError = TRUE) :
server did not report OK, got 425
Calls: get_catalog ... <Anonymous> -> curlPerform -> .Call -> <Anonymous> -> fun
i think my .travis.yml is a pretty standard r configuration:
language: r
cache: packages
sudo: required
apt_packages:
- unixodbc-dev
- libarchive-dev
successful build log late february 2018 at https://api.travis-ci.org/v3/job/343635739/log.txt
failed build log early march 2018 https://api.travis-ci.org/v3/job/352115990/log.txt
the libcurl block looks nearly identical, but there's one noticeable difference between the success and the failure--
late feburary success:
2 upgraded, 35 newly installed, 1 to remove and 124 not upgraded.
mid march failure:
2 upgraded, 35 newly installed, 1 to remove and 135 not upgraded.
minor r session info changes:
late february success:
Session info ------------------------------------------------------------------
date 2018-02-20
DBI 0.7 2017-06-18 cran (#0.7)
devtools 1.13.4 2017-11-09 CRAN (R 3.4.2)
lodown 0.1.0 2018-02-20 Github (ajdamico/lodown#6a69363)
rlang 0.1.6 2017-12-21 cran (#0.1.6)
srvyr 0.3.0 2018-01-24 cran (#0.3.0)
mid march failure:
Session info ------------------------------------------------------------------
date 2018-03-12
DBI 0.8 2018-03-02 cran (#0.8)
devtools 1.13.5 2018-02-18 CRAN (R 3.4.3)
lodown 0.1.0 2018-03-12 Github (ajdamico/lodown#bef726b)
rlang 0.2.0 2018-02-20 cran (#0.2.0)
srvyr 0.3.1 2018-03-10 cran (#0.3.1)

Related

How to stop the same package to be updated again and again in R?

I always use the same script to update my packages before working. It works well, but for a reason I can't explain, when I rerun the script, the same packages keep getting uploaded again and again. And at one point, it stops.
Today for example this is the binary version of the sf package which is updated again and again. I tried to restart R and restart my computer unsuccessfully.
# ==== Option ====
base::options(install.packages.compile.from.source = "never")
rstudioapi::writeRStudioPreference("data_viewer_max_columns", 1000L)
base::Sys.setenv(LANG = "en")
# ==== Packages ====
packages_list <- c("arsenal"
,"data.table"
,"devtools"
,"dplyr"
,"ggplot2"
,"ggtext"
,"grid"
,"gridExtra"
,"gtsummary"
,"here"
,"jsonlite"
,"knitr"
,"lubridate"
,"magick"
,"moments"
,"MetBrewer"
,"pacman"
,"purrr"
,"readr"
,"readxl"
,"stringi"
,"stringr"
,"tidyr"
)
pacman::p_load(char = packages_list,
install = TRUE,
update = TRUE,
character.only = TRUE)
Comment
There is a binary version available but the source version is later:
binary source needs_compilation
sf 1.0-6 1.0-7 TRUE
Binaries will be installed
It seems like this part explains why the sf package is updated again and again. pacman check for an update, I have installed the package sf 1.0-6 obviously. But since there is a more recent source version of sf, pacman is trying to update the package, and since I set that I don't want to install packages from source, it updates the package from binary by default without checking if the version is the same as the one I already have.
Any idea how to resolve the situation ?
You're doing it to yourself by forcing it to install/update all, every time. I suspect (as I'm not the original dev) that the rationale could be simple: since it's feasible that you want/need to reinstall a package, then since you're including it you must know that you need a redo, so it'll do it.
Regardless of the rationale, though, we can filter the list to see what needs to be updated by comparing installed.packages() with available.packages().
ip <- installed.packages()
ip <- as.data.frame(ip[ ip[,1] %in% packages_list, c("Package", "Version")])
ap <- available.packages()
ap <- as.data.frame(ap[ ap[,1] %in% packages_list, c("Package", "Version")])
str(ap)
# 'data.frame': 22 obs. of 2 variables:
# $ Package: chr "arsenal" "data.table" "devtools" "dplyr" ...
# $ Version: chr "3.6.3" "1.14.2" "2.4.3" "1.0.8" ...
want <- data.frame(Package = packages_list)
quux <- Reduce(function(a, b) merge(a, b, by="Package", all.x=TRUE), list(want, ip, ap))
quux <- transform(quux,
broken = is.na(Version.x) & is.na(Version.y),
update = mapply(utils::compareVersion, quux$Version.x, quux$Version.y) < 0
)
quux
# Package Version.x Version.y broken update
# 1 arsenal <NA> 3.6.3 FALSE TRUE
# 2 data.table 1.14.2 1.14.2 FALSE FALSE
# 3 devtools 2.4.3 2.4.3 FALSE FALSE
# 4 dplyr 1.0.8 1.0.8 FALSE FALSE
# 5 ggplot2 3.3.5 3.3.5 FALSE FALSE
# 6 ggtext <NA> 0.1.1 FALSE TRUE
# 7 grid 4.1.2 <NA> FALSE FALSE
# 8 gridExtra 2.3 2.3 FALSE FALSE
# 9 gtsummary <NA> 1.5.2 FALSE TRUE
# 10 here 1.0.1 1.0.1 FALSE FALSE
# 11 jsonlite 1.7.2 1.8.0 FALSE TRUE
# 12 knitr 1.36 1.37 FALSE TRUE
# 13 lubridate 1.8.0 1.8.0 FALSE FALSE
# 14 magick 2.7.3 2.7.3 FALSE FALSE
# 15 MetBrewer <NA> 0.1.0 FALSE TRUE
# 16 moments <NA> 0.14 FALSE TRUE
# 17 pacman <NA> 0.5.1 FALSE TRUE
# 18 purrr 0.3.4 0.3.4 FALSE FALSE
# 19 readr 2.1.1 2.1.2 FALSE TRUE
# 20 readxl 1.3.1 1.3.1 FALSE FALSE
# 21 stringi 1.7.5 1.7.6 FALSE TRUE
# 22 stringr 1.4.0 1.4.0 FALSE FALSE
# 23 tidyr 1.1.4 1.2.0 FALSE TRUE
(broken is just provided as a check on packages_list.)
You would then replace your packages_list with the results from
quux$Package[quux$update]
# [1] "arsenal" "ggtext" "gtsummary" "jsonlite" "knitr" "MetBrewer" "moments" "pacman" "readr" "stringi"
# [11] "tidyr"

Polynomial Function Expansion in R

I am currently reviewing this question on SO and see that the OP stated that by adding more for loops you can expand the polynomials. How exactly would you do so? I am trying to expand to polyorder 5.
Polynomial feature expansion in R
Here is the code below:
polyexp = function(df){
df.polyexp = df
colnames = colnames(df)
for (i in 1:ncol(df)){
for (j in i:ncol(df)){
colnames = c(colnames, paste0(names(df)[i],'.',names(df)[j]))
df.polyexp = cbind(df.polyexp, df[,i]*df[,j])
}
}
names(df.polyexp) = colnames
return(df.polyexp)
}
Ultimately, I'd like to order the matrix so that it expands in order of degree. I tried using the poly function but I'm not sure if you can order the result so that it returns a matrix that starts with degree 1 then moves to degree 2, then 3, 4, and 5.
To "sort by degree" is a little ambiguous. x^2 and x*y both have degree 2. I'll assume you want to sort by total degree, and then within each of those, by degree of the 1st column; within that, by degree of the second column, etc. (I believe the default is to ignore total degree and sort by degree of the last column, within that the second last, and so on, but this is not documented so I won't count on it.)
Here's how to use polym to do this. The columns are named things like "2.0" or "1.1". You could sort these alphabetically and it would be fine up to degree 9, but if you convert those names using as.numeric_version, there's no limit. So convert the column names to version names, get the sort order, and use that plus degree to re-order the columns of the result. For example,
df <- data.frame(x = 1:6, y = 0:5, z = -(1:6))
expanded <- polym(as.matrix(df), degree = 5)
o <- order(attr(expanded, "degree"),
as.numeric_version(colnames(expanded)))
sorted <- expanded[,o]
# That lost the attributes, so put them back
attr(sorted, "degree") <- attr(expanded, "degree")[o]
attr(sorted, "coefs") <- attr(expanded, "coefs")
class(sorted) <- class(expanded)
# If you call predict(), it comes out in the default order,
# so will need sorting too:
predict(sorted, newdata = as.matrix(df[1,]))[, o]
#> 0.0.1 0.1.0 1.0.0 0.0.2 0.1.1 0.2.0
#> 0.59761430 -0.59761430 -0.59761430 0.54554473 -0.35714286 0.54554473
#> 1.0.1 1.1.0 2.0.0 0.0.3 0.1.2 0.2.1
#> -0.35714286 0.35714286 0.54554473 0.37267800 -0.32602533 0.32602533
#> 0.3.0 1.0.2 1.1.1 1.2.0 2.0.1 2.1.0
#> -0.37267800 -0.32602533 0.21343368 -0.32602533 0.32602533 -0.32602533
#> 3.0.0 0.0.4 0.1.3 0.2.2 0.3.1 0.4.0
#> -0.37267800 0.18898224 -0.22271770 0.29761905 -0.22271770 0.18898224
#> 1.0.3 1.1.2 1.2.1 1.3.0 2.0.2 2.1.1
#> -0.22271770 0.19483740 -0.19483740 0.22271770 0.29761905 -0.19483740
#> 2.2.0 3.0.1 3.1.0 4.0.0 0.0.5 0.1.4
#> 0.29761905 -0.22271770 0.22271770 0.18898224 0.06299408 -0.11293849
#> 0.2.3 0.3.2 0.4.1 0.5.0 1.0.4 1.1.3
#> 0.20331252 -0.20331252 0.11293849 -0.06299408 -0.11293849 0.13309928
#> 1.2.2 1.3.1 1.4.0 2.0.3 2.1.2 2.2.1
#> -0.17786140 0.13309928 -0.11293849 0.20331252 -0.17786140 0.17786140
#> 2.3.0 3.0.2 3.1.1 3.2.0 4.0.1 4.1.0
#> -0.20331252 -0.20331252 0.13309928 -0.20331252 0.11293849 -0.11293849
#> 5.0.0
#> -0.06299408
Created on 2020-03-21 by the reprex package (v0.3.0)

Plot elements appear outside plot region with cairo_pdf() but not pdf()

I don't know much about graphic devices etc. All I want to do is to save plots to PDF and to embed fonts.
I use cairo_pdf() for this, but I noticed that sometimes plot elements are printed outside of the box/plot region (see screenshots of the PDFs). I can reproduce the issue on different Windows machines, different R versions, using packages cairoDevice or Cairo, and with for example lines(). But plots saved via pdf() look fine.
My questions are:
Is this reproducible? If yes, is this a bug and where?
Are there any other situations were cairo_pdf()-plots look different compared to pdf()-plots? Are there any other disadvantages of using cairo_pdf()?
Below are screenshots from details of the whole PDFs illustrating the differences. Note that, in the left image, the axis overlap with some points.
capabilities("cairo")
#> cairo
#> TRUE
set.seed(123456)
N <- 10000
v1 <- rnorm(N)
v2 <- rnorm(N)
v3 <- ifelse(v1 > 1.02 | v2 > 1.02 | v1 < -.02 | v2 < -.02, 2, 1)
cairo_pdf("plot1.pdf")
plot(v1, v2, xlim = 0:1, ylim = 0:1, col = v3, pch = 16)
dev.off()
#> null device
#> 1
pdf("plot2.pdf")
plot(v1, v2, xlim = 0:1, ylim = 0:1, col = v3, pch = 16)
dev.off()
#> null device
#> 1
devtools::session_info()
#> Session info ------------------------------------------------------------------
#> setting value
#> version R version 3.4.2 (2017-09-28)
#> system x86_64, mingw32
#> ui Rgui
#> language (EN)
#> collate German_Germany.1252
#> tz Europe/Berlin
#> date 2018-03-09
#>
#> Packages ----------------------------------------------------------------------
#> package * version date source
#> base * 3.4.2 2017-09-28 local
#> compiler 3.4.2 2017-09-28 local
#> datasets * 3.4.2 2017-09-28 local
#> devtools 1.13.5 2018-02-18 CRAN (R 3.4.3)
#> digest 0.6.15 2018-01-28 CRAN (R 3.4.3)
#> graphics * 3.4.2 2017-09-28 local
#> grDevices * 3.4.2 2017-09-28 local
#> memoise 1.1.0 2017-04-21 CRAN (R 3.4.1)
#> methods * 3.4.2 2017-09-28 local
#> stats * 3.4.2 2017-09-28 local
#> utils * 3.4.2 2017-09-28 local
#> withr 2.1.1 2017-12-19 CRAN (R 3.4.3)
This bug is fixed in R 3.6.0.
From the NEWS:
The cairo_pdf graphics device (and other Cairo-based devices) now clip correctly to the right and bottom border.
There was an off-by-one-pixel bug, reported by Lee Kelvin.

How should I use the uq() function inside a package?

I'm puzzled by the behaviour of the uq() function. The behavior is not the same when I use uq() or lazyeval::uq().
Here is my reproducible example :
First, I generate a fake dataset
library(tibble)
library(lazyeval)
fruits <- c("apple", "banana", "peanut")
price <- c(5,6,4)
table_fruits <- tibble(fruits, price)
Then I write a toy function, toy_function_v1, using only uq() :
toy_function_v1 <- function(data, var) {
lazyeval::f_eval(f = ~ uq(var), data = data)
}
and a second function using lazyeval::uq() :
toy_function_v2 <- function(data, var) {
lazyeval::f_eval(f = ~ lazyeval::uq(var), data = data)
}
Surprisingly, the output of v1 and v2 is not the same :
> toy_function_v1(data = table_fruits, var = ~ price)
[1] 5 6 4
> toy_function_v2(data = table_fruits, var = ~ price)
price
Is there any explanation ?
I know it's a good practice to use the syntaxe package::function() to use the function inside a new package. So what's the best solution in that case ?
Here is my session_info :
> devtools::session_info()
Session info ----------------------------------------------------------------------------------------------------------------------------------------------------
setting value
version R version 3.3.1 (2016-06-21)
system x86_64, linux-gnu
ui RStudio (1.0.35)
language (EN)
collate C
tz <NA>
date 2016-11-07
Packages --------------------------------------------------------------------------------------------------------------------------------------------------------
package * version date source
Rcpp 0.12.7 2016-09-05 CRAN (R 3.2.3)
assertthat 0.1 2013-12-06 CRAN (R 3.2.2)
devtools 1.12.0 2016-06-24 CRAN (R 3.2.3)
digest 0.6.10 2016-08-02 CRAN (R 3.2.3)
lazyeval * 0.2.0.9000 2016-10-14 Github (hadley/lazyeval#c155c3d)
memoise 1.0.0 2016-01-29 CRAN (R 3.2.3)
tibble * 1.2 2016-08-26 CRAN (R 3.2.3)
withr 1.0.2 2016-06-20 CRAN (R 3.2.3)
It's just a bug in the uq() function. The issue is open on Github : https://github.com/hadley/lazyeval/issues/78.

Colophon for an R book

At the end of an R book, I'd like to show the versions of main R packages used to compile the book. I'm wondering if there is anything I could do better than just use sessionInfo() in a chunk, e.g.,
\section*{Colophon}
This book was produced using \Sexpr{R.version.string},
\pkg{knitr} (\Sexpr{packageDescription("knitr")[["Version"]]})
and other package versions listed below.
<<session-info, size='footnotesize',R.options=list(width=90)>>=
print(sessionInfo(), locale = FALSE)
#
In particular, sessionInfo() lists all packages loaded indirectly as well as those loaded directly.
```{r}
library(knitr)
p = devtools::loaded_packages()
p$version = unlist(lapply(p$package, function(x) as.character(packageVersion(x))))
kable(p[order(p$package),], row.names=FALSE)
```
If you do not have devtools installed, steal the code from loaded_packages.
This will give a comma separated list of the packages loaded into the current session:
pkgs <- sort(sub("package:", "", grep("package:", search(), value = TRUE)));
toString(Map(function(p) sprintf("%s (%s)", p, packageVersion(p)), pkgs))
giving this string which you can insert by placing the code above in a \Sexpr:
[1] "base (3.2.0), datasets (3.2.0), graphics (3.2.0), grDevices (3.2.0), methods (3.2.0), stats (3.2.0), utils (3.2.0)"
Only core R functions are used in this code.
I don't want to list all packages loaded (base packages, dependencies) in the current session, so I came up with a better solution for my needs. Maybe this will be useful to someone else.
Find all packages explicitly loaded via library() in the .Rnw files for the book.
Use devtools:::package_info() for formatting
For (1.), I used the following pipe of shell commands, all standard except for my trusty tcgrep perl script that find strings in files recursively
tcgrep -E Rnw '^library(.*)' . \
| grep '/ch' \
| perl -p -e 's/^.*://; s/\s*#.*//' \
| perl -p -e 's/library\(([\w\d]+)\)/"$1"/g; s/;/, /' \
| sort -u | perl -p -e 's/\n/, /' > packages-used.R
This gave me
packages <- c(
"AER", "ca", "car", "colorspace", "corrplot", "countreg", "directlabels", "effects", "ggparallel", "ggplot2", "ggtern", "gmodels", "gnm", "gpairs", "heplots", "Lahman", "lattice", "lmtest", "logmult", "MASS", "MASS", "countreg", "mgcv", "nnet", "plyr", "pscl", "RColorBrewer", "reshape2", "rms", "rsm", "sandwich", "splines", "UBbipl", "vcd", "vcdExtra", "VGAM", "xtable")
Then for (2.),
library(devtools)
pkg_info <- devtools:::package_info(packages)
# clean up unwanted
pkg_info$source <- sub(" \\(R.*\\)", "", pkg_info$source)
pkg_info <- pkg_info[,-2]
pkg_info
I like the result because it also identifies non-CRAN (development version) packages. I could also format this with kable:
> pkg_info
package version date source
AER 1.2-3 2015-02-24 CRAN
ca 0.60 2015-03-01 R-Forge
car 2.0-25 2015-03-03 R-Forge
colorspace 1.2-6 2015-03-11 CRAN
corrplot 0.73 2013-10-15 CRAN
countreg 0.1-2 2014-10-17 R-Forge
directlabels 2013.6.15 2013-07-23 CRAN
effects 3.0-4 2015-03-22 R-Forge
ggparallel 0.1.1 2012-09-09 CRAN
ggplot2 1.0.1 2015-03-17 CRAN
ggtern 1.0.5.0 2015-04-15 CRAN
gmodels 2.15.4.1 2013-09-21 CRAN
gnm 1.0-8 2015-04-22 CRAN
gpairs 1.2 2014-03-09 CRAN
heplots 1.0-15 2015-04-18 CRAN
Lahman 3.0-1 2014-09-13 CRAN
lattice 0.20-31 2015-03-30 CRAN
lmtest 0.9-33 2014-01-23 CRAN
logmult 0.6.2 2015-04-22 CRAN
MASS 7.3-40 2015-03-21 CRAN
mgcv 1.8-6 2015-03-31 CRAN
nnet 7.3-9 2015-02-11 CRAN
plyr 1.8.2 2015-04-21 CRAN
pscl 1.4.9 2015-03-29 CRAN
RColorBrewer 1.1-2 2014-12-07 CRAN
reshape2 1.4.1 2014-12-06 CRAN
rms 4.3-1 2015-05-01 CRAN
rsm 2.7-2 2015-05-13 CRAN
sandwich 2.3-3 2015-03-26 CRAN
UBbipl 3.0.4 2013-10-13 local
vcd 1.4-0 2015-04-20 local
vcdExtra 0.6-8 2015-04-16 CRAN
VGAM 0.9-8 2015-05-11 CRAN
xtable 1.7-4 2014-09-12 CRAN
If you're using LaTeX you could simply generate bibliography for all the packages using:
%% begin.rcode rubber, results = 'asis', cache = FALSE
% write_bib(file = "generated.bib")
%% end.rcode
You can put this after your \end{document} and add corresponding \bibliography{mybib,generated} entry. This way you could also reference them with usual \cite{}

Resources