Failed to load file through getopt package in R - r

I am trying to use getopt package to open my file but the codes seem not working:
> library(getopt)
args <- commandArgs(trailingOnly = FALSE)
spec = matrix(c(
'help' , 'h', 0, "character",
'input' , 'i', 1, "file",
'output' , 'o', 1, "character"), byrow=TRUE, ncol=4)
opt = getopt(spec)
if(opt$input){
file <- read.table(args[1])
}
print(file)
I am trying to use command line to run the codes like:
Rscript --slave filename.R -i file.txt
The error information is:
Error in storage.mode(peek.optstring) <- mode : invalid value
Calls: getopt ... tryCatch -> tryCatchList -> tryCatchOne -> doTryCatch
Execution halted
Can someone help me out?

You need to test the components, and if -i filename.txt is given then opt$file is what you use to access it.
A repaired version is
#!/usr/bin/Rscript
library(getopt)
spec <- matrix(c(
'help' , 'h', 0, "character",
'input' , 'i', 1, "character",
'output' , 'o', 1, "character"),
byrow=TRUE, ncol=4)
opt <- getopt(spec)
if ( !is.null(opt$input) ) {
file <- read.table(opt$input)
}
print(file)
I used to use this package quite a bit, but these days I much prefer docopt which is even easier to use and more featureful.

In case others run into this same problem, I got the same error message
Error in getopt_options(object, args) :
Error in storage.mode(this.argument) <- mode : invalid value
Calls: parse_args -> parse_options -> getopt_options
My error turned out to be from accidentally misspelling my type argument:
make_option(c("--chr_len"), type = 'chaqracter', default = NULL, help = "file path: length of all chromosomes")
This error was resolved when I fixed the spelling
make_option(c("--chr_len"), type = 'character', default = NULL, help = "file path: length of all chromosomes")

Related

R Error in py_get_attr_impl(x, name, silent) : AttributeError: module 'tensorflow' has no attribute 'placeholder'

I am trying to Implement Auto Encoder Dimension Reduction from Tensorflow in R, in this example:
library(dimRed)
library(tensorflow)
fraud_data <- read.csv("fraud_data")
data_label <- fraud_data["class"]
my_formula <- as.formula("class ~ .")
dat <- as.dimRedData(my_formula, fraud_data)
dimen <- NULL
dimension_params <- NULL
dimen <- dimRed::AutoEncoder()
dimension_params <- dimen#stdpars
dimension_params$ndim <- 2
emb <- dimen#fun(fraud_data, dimension_params)
dimensional_data <- data.frame(emb#data#data)
x11()
plot(x=dimensional_data[,1], y=dimensional_data[,2], col=data_label, main="Laplacian Eigenmaps Projection")
legend(x=legend_pos, legend = unique(data_label), col=unique(data_label), pch=1)
I keep getting AttributeError module 'tensorflow' has no attribute 'placeholder'" as stated in this traceback:
14. stop(structure(list(message = "AttributeError: module 'tensorflow' has no attribute 'placeholder'",
call = py_get_attr_impl(x, name, silent), cppstack = NULL), class = c("Rcpp::exception",
"C++Error", "error", "condition")))
13. py_get_attr_impl(x, name, silent)
12. py_get_attr(x, name)
11. py_get_attr_or_item(x, name, TRUE)
10. `$.python.builtin.object`(x, name)
9. `$.python.builtin.module`(tf, "placeholder")
8. tf$placeholder
7. graph_params(d_in = ncol(indata), n_hidden = n_hidden, activation = activation,
weight_decay = weight_decay, learning_rate = learning_rate,
n_steps = n_steps, ndim = ndim)
6. eval(substitute(expr), data, enclos = parent.frame())
5. eval(substitute(expr), data, enclos = parent.frame())
4. with.default(pars, {
graph_params(d_in = ncol(indata), n_hidden = n_hidden, activation = activation,
weight_decay = weight_decay, learning_rate = learning_rate,
n_steps = n_steps, ndim = ndim) ...
3. with(pars, {
graph_params(d_in = ncol(indata), n_hidden = n_hidden, activation = activation,
weight_decay = weight_decay, learning_rate = learning_rate,
n_steps = n_steps, ndim = ndim) ...
2. dimen#fun(dat, dimension_params)
Error in py_get_attr_impl(x, name, silent) :
AttributeError: module 'tensorflow' has no attribute 'placeholder'
As by the common solution is to Disable Tensorflow 2 Behaviour as stated in Tensorflow 2.0 - AttributeError: module 'tensorflow' has no attribute 'Session', I tried to use reticulate and suppress the errors by this example:
library(reticulate)
x <- import("tensorflow.compat.v1", as="tf")
x$disable_v2_behavior()
but this doesn't change anything.. and I still get AttributeError, I am wondering, How should I make a proper change in Tensorflow from R in this case?
Here is Sample Data used for the example: https://drive.google.com/file/d/1Yt4V1Ir00fm1vQ9futziWbwjUE9VvYK7/view?usp=sharing
I found out deeper that tf acts as R tensorflow module, since ?tf is a valid command after using library(tensorflow), and then since Tensorflow updated to version 2+, instead using tf$placeholder, use tf$compat$v1$placeholder, so I had an idea to add the features available in tf$compat$v1 to tf
tf_synchronize <- function(){
library(tensorflow)
rm(list=c("tf")) #Delete first if there any tf variable in Global Environment
tf_compat_names <- names(tf$compat$v1)
for(x in 2:length(tf_compat_names)){
tf[[tf_compat_names[x]]] <- tf$compat$v1[[tf_compat_names[x]]]
}
}
With this executed, the AttributeError are no more, and Auto Encoder from Dimension Reduction is successfully executed

Why R CMD Check failed with weird error in winbuilder ? "type must be LANGSXP at this point"

I am trying to propose an update of my package 'antaresRead' on CRAN.
When I use the service "https://win-builder.r-project.org/upload.aspx" for R-devel to test my package, I have this error :
test_check("antaresRead")
Error in length(ans$indices) : type must be LANGSXP at this point
Calls: test_check ... eval -> eval -> suppressMessages -> withCallingHandlers
Logs are here
Why I have this error for this platform and with R-devel ? Can I correct It ?
When I use the same service with R-release and R-oldRelease, checks are OK :
Rrelease
RoldRelease
With the same code :
Travis Is
OK
appVeyor is OK for 8 combinations
rhub is also OK for 8 platforms
rhub::check(platform = 'debian-gcc-release')
rhub::check(platform = 'macos-elcapitan-release')
check_for_cran(platforms = "windows-x86_64-oldrel")
check_for_cran(platforms = "windows-x86_64-patched")
rhub::check(platform = 'fedora-clang-devel')
rhub::check(platform = 'fedora-gcc-devel')
rhub::check(platform = 'debian-gcc-patched')
rhub::check(platform = 'debian-gcc-devel')
Has anyone ever encountered this error? how to solve it?
What I did :
Download the R-devel version for Windows found here --> R 3.5.0
Build and check my pacakge antaresRead for x64 --> build and check are OK
Build and check my package for i386 --> build OK but faild in Check with the same error
Error in length(ans$indices) : type must be LANGSXP at this point
Calls: test_check ... eval -> eval -> suppressMessages -> withCallingHandlers
Solution :
- My bad, it's my code, it's my fault.
The error come from this code
districtLinks <- merge(districtLinks, districts[, .(district, x, y)],
by.x = "toDistrict", by.y = "district")
districtLinks is empty sometimes and with the new version of R and data.table this provokes an error only in Windows 32bits. I added a check before.
This error come because I wanted to merge an empty data.table with another data.table.
districtLinks <- merge(districtLinks, districts[, .(district, x, y)],
by.x = "toDistrict", by.y = "district")
What I did is to add a check before the merge
if(!dim(districtLinks)[1]==0){
districtLinks <- merge(districtLinks, districts[, .(district, x, y)],
by.x = "toDistrict", by.y = "district")
}

Trouble using the r package (random)

I want to generate random string and have installed the random package.
However, I couldn't run my code and received this error:
Error in url(urltxt, ..., method = "libcurl") : cannot open connection
In addition: Warning message: In url(urltxt, ..., method = "libcurl")
: URL 'https://www.random.org/strings/?num=7&len=6&digits=on&upperalpha=on&loweralpha=off&unique=on&format=plain&rnd=new'
: status was 'SSL connect error'
Here's my code:
library("random")
String<-randomStrings(n=7, len = 6, digits = TRUE, upperalpha = TRUE, loweralpha = FALSE, unique = TRUE, check = FALSE)
Anybody knows what's the source of this error?

KnitR unable to download documents

I am able to run this code normally in R.
setInternet2(use = TRUE)
download.file("http://d396qusza40orc.cloudfront.net/dsscapstone/dataset/Coursera-SwiftKey.zip", "Coursera-SwiftKey.zip", method = "curl", mode = 'wb')
unzip("Coursera-SwiftKey.zip", files=c("final/en_US/en_US.twitter.txt", "final/en_US/en_US.news.txt", "final/en_US/en_US.blogs.txt"), exdir=".")
But when i try to use knitR it gives me this error
Quitting from lines 15-21 (milestone.Rmd)
Error in parse(text = x, srcfile = src) : <text>:5:106: unexpected ')'
4: unzip("Coursera-SwiftKey.zip", files=c("final/en_US/en_US.twitter.txt", "final/en_US/en_US.news.txt", "final/en_US/en_US.blogs.txt"), exdir=".")
5: file.copy(c("final/en_US/en_US.twitter.txt", "final/en_US/en_US.news.txt", "final/en_US/en_US.blogs.txt"?)
^
Calls: <Anonymous> ... <Anonymous> -> parse_all -> parse_all.character -> parse
Execution halted
I am using R Studio and MAC OS X.
I tried removing the s from https but it still di not work.

overriding R's incomplete error messages

When a call exists of multiple lines, a potential error only includes the first line of match.call() resulting in some lost information and an incomplete sentence. A simple example:
#proper error message:
runif(n=1, k=5)
#incomplete error message:
runif(n=1, k={5})
What would be a way to get R to include the full call to the error message (maybe by collapsing the multiple lines or so)? I am mostly interested in using this in a tryCatch setting.
I had a go at investigating the error object in a tryCatch setting via:
tryCatch( runif(n=1,k={5}),
error = function(e) recover() )
And then selected the 4th environment (value[[3]](cond)) to examine e.
I noticed that e$call was:
Browse[1]> e$call
runif(n = 1, k = {
5
})
So it seems that the error message just uses that first line.
You can collapse all the lines together with:
Browse[1]> paste(deparse(e$call),collapse='')
[1] "runif(n = 1, k = { 5})"
So you could try something like:
tryCatch( runif(n=1,k={5}),
error = function(e) {
cat(sprintf('Error in %s: %s\n',
paste(deparse(e$call),collapse=''),
e$message))
} )
But this doesn't fix up the error message itself, just the call leading up to it:
Error in runif(n = 1, k = { 5}): unused argument(s) (k = {
So the 'Error in xxx' is complete, but the 'unused argument(s) xxx' is still not. It's a start, but not all the way there.
I'm not sure how to improve on this (and am also interested to know if it's possible).

Resources