Default .Options setting for unzip - r

I was trying to install ramnathv's slidify package and was surprised the by the following error:
# install.packages("devtools")
library(devtools)
install_github("slidify","ramnathv")
Installing github repo slidify/master from ramnathv
Downloading slidify.zip from https://github.com/ramnathv/slidify/archive/master.zip
Installing package from /tmp/RtmpOFEJuD/slidify.zip
Error in unzip(src, exdir = target, unzip = getOption("unzip")) :
'unzip' must be a single character string
I've installed it before with no issues on another system using the same Arch Linux setup and their R package, granted it was an earlier version of R (3.0.0 or 3.0.1).
In googling the error, it comes from this bit in zip.R:
unzip <-
function(zipfile, files = NULL, list = FALSE, overwrite = TRUE,
junkpaths = FALSE, exdir = ".", unzip = "internal",
setTimes = FALSE)
{
if(identical(unzip, "internal")) {
if(!list && !missing(exdir))
dir.create(exdir, showWarnings = FALSE, recursive = TRUE)
res <- .External(C_unzip, zipfile, files, exdir, list, overwrite,
junkpaths, setTimes)
if(list) {
dates <- as.POSIXct(res[[3]], "%Y-%m-%d %H:%M", tz="UTC")
data.frame(Name = res[[1]], Length = res[[2]], Date = dates,
stringsAsFactors = FALSE)
} else invisible(attr(res, "extracted"))
} else {
WINDOWS <- .Platform$OS.type == "windows"
if(!is.character(unzip) || length(unzip) != 1L || !nzchar(unzip))
stop("'unzip' must be a single character string")
...
While the default setting for unzip() is unzip = "internal", it can also be passed getOptions("unzip"):
Usage
unzip(zipfile, files = NULL, list = FALSE, overwrite = TRUE,
junkpaths = FALSE, exdir = ".", unzip = "internal",
setTimes = FALSE)
...
unzip
The method to be used. An alternative is to use getOption("unzip"),
which on a Unix-alike may be set to the path to a unzip program.
In looking at the output of .Options, I see, indeed, that there's nothing set for unzip:
> .Options$unzip
[1] ""
From perusing documentation, I see that I can set this in ~/.Renviron, but my question is whether on a Linux system this should be picked up by default or if it's pretty standard to have to set your unzip command?
If it's not standard that this be un-populated, I'll file a bug report with Arch Linux, as perhaps the package was compiled without this as a default, as documentation seems to suggest that it would be populated with an unzip command if one was found during installation:
unzip
a character string, the path of the command used for unzipping help files, or
"internal". Defaults to the value of R_UNZIPCMD, which is set in ‘etc/Renviron’
if an unzip command was found during configuration.
The Arch package I'm using is a binary and thus was pre-compiled, so intentionally or unintentionally perhaps this was overlooked or should be checked for during installation?
P.S. Just to avoid the obvious...
$ which unzip
/usr/bin/unzip

Related

ulimit different between ubuntu terminal and call from R

I am trying to create mapbox tiles using mapboxapi::tippecanoe() in R. Unfortunately, my work computer runs Windows 10, which greatly complicates what I am trying to do. Tippecanoe is a Unix executable, so I downloaded and installed Ubuntu and am running it on a Windows subsystem for Linux. To get tippecanoe to launch, I had to edit the source code of mapboxapi::tippecanoe() to pass arguments to WSL. I then ran into an issue where Tippecanoe would give me an error that it could not open database files. Some research on Github led me to believe that this was related the number of open files limit in Ubuntu. After a lot of digging, I was able to increase ulimit -n to 65535 for on my ubuntu terminal. As soon as I launch Ubuntu, if I type in ulimit -n, I get 65535. However, when I call `sytem2("wsl", "ulimit -n"), I get the default value of 1024. I thought this was due to the user that R was calling in Ubuntu, but running system2("wsl", "whoami") returned the username for who I increased both the hard and soft nofile limits for. I am really stumped. Apologies for not pasting a reproducible example, but I am not sure how to make one for this situation. Any help would be much appreciated. Thanks!
Well after a whole lot of tinkering, this ended up being mostly a straightforward R code issue. The ulimit issue may still have been a problem, but actually I need to fix the R code in mapboxapi::tippecanoe(). Because mapboxapi::tippecanoe()uses the system() command to call tippecanoe, not only did I need to change the call to invoke wsl through a login shell using system2("wsl", "- d Ubuntu -lc 'tippecanoe <arguments to tippecanoe>'"), but I also needed to change the paths that R sent to tippecanoe to be linux paths instead of Window paths. If anyone else is having trouble with this, here is the tweaked mapboxapi::tippecanoe() command code that actually worked for me:
tippecanoe2<-function (input, output, layer_name, min_zoom = NULL,
max_zoom = NULL, drop_rate = NULL, overwrite = TRUE, other_options = NULL,
keep_geojson = FALSE)
{
check_install <- system2("wsl", "tippecanoe -v") == 0
linux_dir<-paste(getwd(), layer_name, sep="/")#make a directory in your linux directory for the .mbtiles
parsed<-strsplit(linux_dir, split="/") #parse the windows directory path
n<-length(parsed[[1]])
dir_out<-paste("",parsed[[1]][n-1], parsed[[1]][n], sep="/") #construct the linux directory path
dir.create(linux_dir)
op<-options(useFancyQuotes = FALSE)
if (!check_install) {
rlang::abort(c("tippecanoe is not installed or cannot be found by the application you are using to run mapboxapi.",
"If you haven't installed tippecanoe, please visit https://github.com/mapbox/tippecanoe for installation instructions.",
"If you have installed tippecanoe, run `Sys.getenv('PATH')` and make sure your application can find tippecanoe. If it cannot, adjust your PATH accordingly."))
}
opts <- c()
if (!is.null(min_zoom)) {
opts <- c(opts, sprintf("-Z%s", min_zoom))
}
if (!is.null(max_zoom)) {
opts <- c(opts, sprintf("-z%s", max_zoom))
}
if (is.null(min_zoom) && is.null(max_zoom)) {
opts <- c(opts, "-zg")
}
if (!is.null(drop_rate)) {
opts <- c(opts, sprintf("-r%s", drop_rate))
}
else {
opts <- c(opts, "-as")
}
if (overwrite) {
opts <- c(opts, "-f")
}
collapsed_opts <- paste0(opts, collapse = " ")
if (!is.null(other_options)) {
extra_opts <- paste0(other_options, collapse = " ")
collapsed_opts <- paste(collapsed_opts, extra_opts)
}
dir <- linux_dir
if (any(grepl("^sf", class(input)))) {
input <- sf::st_transform(input, 4326)
if (is.null(layer_name)) {
layer_name <- stringi::stri_rand_strings(1, 6)
}
if (keep_geojson) {
outfile <- paste0(layer_name, ".geojson")
path <- file.path(dir_out, outfile)
sf::st_write(input, path, quiet = TRUE, delete_dsn = TRUE,
delete_layer = TRUE)
}
else {
tmp <- tempdir("//wsl$/Ubuntu/tmp")#Here you would need to tweak to the file path for your linux distribution's temporary directory
tempfile <- paste0(layer_name, ".geojson")
path <- file.path(tmp, tempfile)
sf::st_write(input, path, quiet = TRUE, delete_dsn = TRUE,
delete_layer = TRUE)
}
call <- sprintf("tippecanoe -o %s/%s %s %s", dir_out, output,
collapsed_opts, path)
call2<-paste("-d Ubuntu /bin/bash -lc", sQuote(call, op), sep=" ")
system2("wsl", call2)
}
else if (inherits(input, "character")) {
if (!is.null(layer_name)) {
collapsed_opts <- paste0(collapsed_opts, " -l ",
layer_name)
}
call <- sprintf("tippecanoe -o %s/%s %s %s", dir_out, output,
collapsed_opts, input)
call2<-paste("-d Ubuntu /bin/bash -lc", sQuote(call, op), sep=" ")
system2("wsl", call2)
}
}

DataSpell Remote R Console Failure

First of all, I use a remote R interpreter.
When I unselect "Disable .Rprofile execution on console start" in the settings of DataSpell and save it, IDE throws a weird error when I try to start an R console as below:
CompositeException (3 nested):
------------------------------
[1]: Cannot cast org.jetbrains.plugins.notebooks.jupyter.ui.remote.JupyterRemoteTreeModelServiceVfsListener to org.jetbrains.plugins.notebooks.jupyter.remote.vfs.JupyterVFileEvent$Listener
[2]: Cannot cast org.jetbrains.plugins.notebooks.jupyter.remote.modules.JupyterRemoteEphemeralModuleManagerVfsListener to org.jetbrains.plugins.notebooks.jupyter.remote.vfs.JupyterVFileEvent$Listener
[3]: Cannot cast org.jetbrains.plugins.notebooks.jupyter.ui.remote.JupyterRemoteVfsListener to org.jetbrains.plugins.notebooks.jupyter.remote.vfs.JupyterVFileEvent$Listener
------------------------------
I tried to give an empty .Rprofile file. Nothing changed. It throws the same error. Anyway, here is my .Rprofile file:
options(java.parameters = "-Xmx4G")
options(download.file.method = "wget")
project_base <- getwd()
print(paste("getwd:", getwd()))
Sys.setenv(R_PACKRAT_CACHE_DIR = "~/.rcache")
#### -- Packrat Autoloader (version 0.7.0) -- ####
source("packrat/init.R")
#### -- End Packrat Autoloader -- ####
# These ensures that the project uses it private library
p <- .libPaths()[[1]]
Sys.setenv(R_LIBS_SITE = p)
Sys.setenv(R_LIBS_USER = p)
Sys.setenv(R_PACKRAT_DEFAULT_LIBPATHS = p)
packrat::set_opts(use.cache = TRUE)
print(paste("whoami:", system("whoami", intern = TRUE)))
print(paste("libpaths:", .libPaths()))
print(paste0("cache_path: ", packrat:::cacheLibDir()))
restore_packrat <- function(restart = FALSE) {
packrat::restore(
overwrite.dirty = TRUE, prompt = F, restart =
restart, dry.run = F
)
}
snapshot_packrat <- function() {
packrat::snapshot(
ignore.stale = TRUE, snapshot.sources = FALSE,
infer.dependencies = FALSE
)
}
I appreciate the help of anyone who faced this issue and solved it.
PS: I also issued a bug report to the developers. If you have the same problem, please upvote the issue and this question.
https://youtrack.jetbrains.com/issue/R-1393

Source multiple files in one folder in Rstudio

I have a folder with multiples R functions. I would like to source all these functions in R studio. I tried the following code, but it does not work. I found the following code here ()
sourceFolder <- function(folder, recursive = FALSE, ...)
{
files <- list.files(folder, pattern = "[.][rR]$",
full.names = TRUE, recursive = recursive)
if (!length(files))
stop(simpleError(sprintf('No R files in folder "%s"', folder)))
src <- invisible(lapply(files, source, ...))
message(sprintf('%s files sourced from folder "%s"', length(src), folder))
}
sourceFolder("C:/Users/Admin/Desktop/autots",recursive = TRUE))
However, I got this error:
> sourceFolder("C:/Users/Admin/Desktop/autots",recursive = TRUE)
Error in environment(lstar) : object 'lstar' not found
Called from: environment(lstar)
I do not know how to fix it. The package that I try to install it as a source can be found here http://stat.snu.ac.kr/heeseok/autots
Any help, please?

passing extra argumenets to devtools::build

Something seems to have changed in the devtoolspackage, so that the following commands, that used to run now give an error I can't decipher:
> Sys.setenv(R_GSCMD="C:/Program Files/gs/gs9.21/bin/gswin64c.exe")
> devtools::build(args = c('--resave-data','--compact-vignettes="gs+qpdf"'))
The filename, directory name, or volume label syntax is incorrect.
Error in (function (command = NULL, args = character(), error_on_status = TRUE, :
System command error
I've tried other alternatives with other devtools commands, like just passing a single argument, but still get the same error
args = '--compact-vignettes="gs+qpdf"'
devtools::check_win_devel(args=args)
I'm using devtools 2.2.0, under R 3.5.2

Unzip password protected zip files in R

A password cannot be specified in unzip (utils) function. The other function I am aware of, getZip (Hmisc), only works for zip files containing one compressed file.
I would like to do something like this to unzip all the files in foo.zip in Windows 8:
unzip("foo.zip", password = "mypass")
I found this question very useful but saw that no formal answers were posted, so here goes:
First I installed 7z.
Then I added "C:\Program Files\7-Zip" to my environment path.
I tested that the 7z command was recognized from the command line.
I opened R and typed in system("7z x secure.7z -pPASSWORD") with the appropriate PASSWORD.
I have multiple zipped files and I'd rather not the password show in the source code or be stored in any text file, so I wrote the following script:
file_list <- list.files(path = ".", pattern = ".7z", all.files = T)
pw <- readline(prompt = "Enter the password: ")
for (file in file_list) {
sys_command <- paste0("7z ", "x ", file, " -p", pw)
system(sys_command)
}
which when sourced will prompt me to enter the password, and the zip files will be decompressed in a loop.
I found #Kim 's answer worked for me eventually but not first off. I thought I'd just add a few extra links/steps that helped me get there in the end.
Close and reopen R so that environment path is recognised
If you've already opened R when you do steps 1-3 you need to close and reload R for R to recognise the environment path for 7z. #wush978 's answer to this question r system doesn't work when trying 7zip was informative. I used Sys.getenv("PATH") to check that 7zip was included in the environment paths.
Step 4. I opened R and typed in system("7z x secure.7z -pPASSWORD") with the appropriate PASSWORD.
I actually found this didn't work so I modified it slightly following the instructions in this post which also explains how to specify an output directory https://stackoverflow.com/a/16098709/13678913.
If you have already extracted the files the system command prompts you to choose whether you want to replace the existing file with the file from the archive and provides options
(Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit?
So the modified step 4 (Y allows replacement of files)
system("7z e -ooutput_dir secure.zip -pPASSWORD" Y)
Putting this altogether as a modified set of instructions
Install 7z.
Added "C:\Program Files\7-Zip\" to my environment path using menu options (instructions here https://www.opentechguides.com/how-to/article/windows-10/113/windows-10-set-path.html)
Closed and reopened R studio. Typed Sys.getenv("PATH") to check path to 7zip recognised in the environment (as per #wush978 's answer to question r system doesn't work when trying 7zip)
Typed in the console system("7z e -oC:/My Documents/output_dir secure.zip -pPASSWORD") with the appropriate PASSWORD (as per instructions here https://stackoverflow.com/a/16098709/13678913)
And here is a modified version of #Kim 's neat function (including specified output directory and check for existing files):
My main script
output_dir <- "C:/My Documents/output_dir " #space after directory name is important
zippedfiles_dir <- "C:/My Documents/zippedfiles_dir/"
file_list <- paste0(output_dir , zippedfiles_dir , list.files(path = zippedfiles_dir, pattern = ".zip", all.files = T))
source("unzip7z.R")
Code inside source file unzip7z.R
pw = readline(prompt = "Enter the password: ")
for (file in file_list) {
csvfile <- gsub("\\.zip", "\\.csv", gsub(".*? ", "", file)) #csvfile name (removes output_dir from 'file' and replaces .zip extension with .csv)
#check if csvfile already exists in output_dir, and if it does, replace it with archived version and if it doesn't exist, continue to extract.
if(file.exists(csvfile)) {
sys_command = paste0("7z ", "e -o", file, " -p", pw, " Y")
} else {
sys_command = paste0("7z ", "e -o", file, " -p", pw)
}
system(sys_command)
}
password <- "your password"
read.table(
text = system(paste0("unzip -p -P ", password, " yourfile.zip ", "yourfile.csv"),
intern = "TRUE"
), stringsAsFactors = FALSE, header = TRUE, sep = ","
)
password <- "your password"
system(
command = paste0("unzip -o -P ", password, " ", "yourfile.zip"),
wait = TRUE
)

Resources