removeSource() with `[` subset fails on empty j argument - r

This is a follow-up to removeSource() returning error on internal package function.
In that question, it was pointed out that there may be a bug in removeSource() when the function uses [ subsetting. I want to focus on that issue, so I wrote a new question here.
When the j argument in [ is empty, removeSource() fails.
Here's an example.
foo <- function(x) { x[1, ] }
removeSource(foo)
# Error in recurse(part[[i]]) : argument "part" is missing, with no default
bar <- function(x) { x[1, seq_along(x)] }
removeSource(bar)
# function (x)
# {
# x[1, seq_along(x)]
# }
I'm hesitant to call it a bug, so I'll first ask if this was done intentionally? Also, suppose I submitted the foo() function in a package to CRAN. Would it pass the testing?
Updates:
Sept 1, 2014: Bug report filed https://bugs.r-project.org/bugzilla/show_bug.cgi?id=15957
Sept 21, 2014: This was indeed a bug and according to the confirmed bug report is "soon to be fixed in R-devel and R-patched."
Fixed in version R 3.1.2

I'd say it was a bug. No sign of it reported here though:
https://bugs.r-project.org/bugzilla3/buglist.cgi?quicksearch=removeSource
Interestingly you get a different error once you try and debug the function by making a local copy.
> dput(removeSource,file="rs.tmp.R")
> rs = dget("rs.tmp.R")
rs is now a copy of removeSource, but not in the environment of the utils package.
> foo = function(x){x[1,]}
> rs(foo)
Error in `attr<-`(`*tmp*`, "srcref", value = NULL) : '*tmp*' is missing
> removeSource(foo)
Error in recurse(part[[i]]) : argument "part" is missing, with no default
rs works fine on a function without missing subs:
> bar = function(x){x[1]}
> rs(bar)
function (x)
{
x[1]
}
If you want a really minimal failing example, you don't need any subscripts or commas:
> foo = function(x){x[]}
> removeSource(foo)
Error in recurse(part[[i]]) : argument "part" is missing, with no default
I doubt this will trigger any CRAN flags since missing dimensions in subscripts probably occur in 90% of the packages currently on there...
Suggest you report it on the bug tracker, or ask on R-devel mailing list.

Related

Is there a way to manually attach packages and globals with `future.apply::future_apply`

I am using R's excellent future package. And in the documentation it mentions %global% and %packages% for assigning global variables and packages to be evaluated in the future environment. But those seem to only work with %<-%.
My question is: is there away to do that with future_apply as well. I tried
x = 1
future.apply::future_sapply(1:50, function(y) {
glue("{x}")
}) %packages% "glue" %globals% "x"
and It doesn't work
If you look at the help page for future_sapply, you'll see that future_lapply has the arguments future.packages and future.globals, and if you read carefully, these are also used in future_sapply. So this works:
x = 1
future.apply::future_sapply(1:50, function(y) {
glue("{x}")
}, future.packages = "glue", future.globals = "x")

R numeric variable, non null, non na but empty

Hi eveyrone ##.
I got some problem with R that I can't fix: Currently i'm working with GEOquery package and I want to retrieve some informations in metadata of gse files.
More precisely I'm looking for the channel label (for exemple Cye3). Here's a sample of my code :
>library(GEOquery)
>gse<-getGEO("GSE2253",GSEMatrix=TRUE,destdir=".")
>gse<-gse[[1]]
>gse$label_ch1[1]
V2
Levels: According to Affymetrix protocol (biotin)`
And here's my problem
`> is.na(gse$label_ch1[1])
V2
FALSE
> is.null(gse$label_ch1[1])
[1] FALSE`
This GSE file is a text file and in the line corresponding to the label (!Sample_label_ch1) there is no value.So, here's what I'v done for my work:
`if(is.na(gse$label_ch1[1])){
color<-"Non specified"
} else {
label<-gse$label_ch1[1]
}`
So, if I got no informations for the channel I just say "non specified", else, I return the value. But I'v got error with this if/else statement in my script:
Error in if (file == "") file <- stdout() else if (is.character(file)) { :
the length of argument is null
Sorry if the error traduction is not exact, my R version is in French ^^.
I tried
if(as.character(gse$label_ch1[1])=="")
But it doesn't work either
If someone has an idea to help me ^^
Thanks in advance!
Script:
sample<-NULL
output<-NULL
gse<-NULL
color<-NULL
series_matrix<-dir(getwd(),pattern="*series_matrix.txt")
series_matrix<-unlist(strsplit(series_matrix,"_")[1])
for(i in 1:length(series_matrix)){
gse<-getGEO(series_matrix[i],GSEMatrix=TRUE,destdir=".")
gse<-gse[[1]]
if(length(gse$label_ch1[1])==0){
color<-"Non specified"
} else {
color<-gse$label_ch1[1]
}
print (color)
sample<-cbind(as.character(gse$title),as.character(gse$geo_accession))
outputsample<-paste(getwd(),"/sample.txt",sep="")
write.table(paste("txt",color,sep=""),output,
row.names=FALSE,col.names=FALSE,sep="\t",quote=FALSE)
write.table(sample,outputsample,
row.names=FALSE,col.names=FALSE,sep="\t",quote=FALSE,append=TRUE)
Feature_Num<-list(1:length(featureNames(gse)))
Gene_Symbol<-pData(featureData(gse)[,11])
Probe_Name<-pData(featureData(gse)[,1])
Control_Type<-pData(featureData(gse)[,3])
liste<-as.character(sampleNames(gse))
for(i in 1:lenght(liste)){
values<-cbind(Feature_Num,Gene_Symbol,Probe_name,Control_Type,exprs(gse)[,i])
colnames(values)<-c("Feature_Num","Gene_Symbol",
"Probe_Name","Control_Type","gMedianSignal")
write.table(values,paste(getwd(),"/Ech",liste[i],".txt",sep=""),
row.names=FALSE,quote=FALSE,sep="\t")
}
}
Don't hesitate if you want explication about lines in this script
Yes, in R you can create a zero-length object:
foo<-vector()
foo
logical(0)
Then change it:
foo<-NULL
foo
NULL
It's confusing at first, but if you ever took some abstract algebra, you may remember the difference between the "empty set" and a set whose only element is the "empty set."
Asking on other forum I finally get a solution for this non NULL/non NA problem:
the gse$label_ch1[1] is numeric of length 1
> length(gse$label_ch1[1])
[1] 1
but we can transform this variable in character:
> as.character(gse$label_ch1[1])
[1] ""
and with this line
> nchar(as.character(gse$label_ch1[1]))
[1] 0
we can see that I can see if the gse$label_ch1[1] value is really empty or not
Thank you all for your help!
Cheers

r devtools test() errors but testthat test_file() works

I have a function in a package I'm building that assigns a hex-code to the global environment for use by analysts...
optiplum<-function(){
assign(
x="optiplum",
value=rgb(red=129,green=61,blue=114, maxColorValue = 255),
envir=.GlobalEnv)
}
My unit test code is:
test_that("optiplum - produces the correct hex code",{
optiplum()
expect_true(identical(optiplum,"#813D72"))
})
When I run the code manually, there isn't an error:
> str(optiplum)
chr "#813D72"
> str("#813D72")
chr "#813D72"
> identical("#813D72",optiplum)
[1] TRUE
> expect_true(identical(optiplum,"#813D72"))
When I run a test_file() is also does not error
> test_file("./tests/testthat/test-optiplum.R")
optiplum : .
However, when I run the test as part of my devtools workflow:
> test()
Testing optINTERNAL
Loading optINTERNAL
optiplum : 1
1. Failure: optiplum - produces the correct hex code --------------------------------------------------------------------------------------------------------------
identical(optiplum, "#813D72") isn't true
Anyone have any ideas on why this might be occurring and how I can resolve the situation?
Assignment to the global environment is a no-no, see R Inferno and testthat isolates tests as much as possible (see test_that() details). As a consequence, the optiplum() assignment to the global environment would not succeed because the testthat function strictly prohibits such behaviour.
#Hadley rightly points out that the function should just return the string instead of assigning it, particularly since it is just two extra characters for each use.
So not
optiplum<-function(){
assign(
x="optiplum",
value=rgb(red=129,green=61,blue=114, maxColorValue = 255),
envir=.GlobalEnv)
}
but
optiplum <- function() rgb(red=102,green=17,blue=109, maxColorValue = 255)

What can be reasons for `Error in .local(conn, statement, ...)´ in dbWriteTable from package MonetDB.R?

I get the error, after I set up MonetDB and try to write a large data.frame as a new table in the default database (demo):
>dbWriteTable(conn, "table1", df)
Error in .local(conn, statement, ...) :
Unable to execute statement 'INSERT INTO table1 VALUES([...])
The data.frame has dimensions:
>dim(df)
[1] 148767 618
And has all columns formatted as character:
>all(lapply(df,class)=='character')
[1] TRUE
The error seems to stem from a string value being too long (Thanks #Hannes Mühleisen):
>dbGetException(conn)
$errNum
[1] 22001
$errMsg
[1] "value too long for type (var)char(255)"
How does MonetDB set upper bounds of new (VAR)CHAR variables (I did not find any info on upper bounds in the documentation)? Can a global upper bound be set or can the upper bound be set interactively when creating tables from R via MonetDB.R?
It might be a timeout issue (parameter to dbConnect()). If you'd like to debug, you can run
assignInNamespace("DEBUG_IO",TRUE,"MonetDB.R")
before connecting. If you post the output here, I can give you a better idea what could be going wrong. Finally, have you tried looking at the output of
dbGetException(conn)
?
UPDATE:
Sorry for that, the reason for this is that dbWriteTable uses the wrong SQL type for character data. At the moment, a VARCHAR(255) is used, which is limited to 255 characters indeed. What it should use is STRING, which has no limits. It is noted as a bug and will be fixed in the next release. Since this might take a while, here is a workaround: If you install from source, you may fix this by changing
setMethod("dbDataType", signature(dbObj="MonetDBConnection", obj = "ANY"), def=function(dbObj, obj, ...) {
if (is.logical(obj)) "BOOLEAN"
else if (is.integer(obj)) "INTEGER"
else if (is.numeric(obj)) "DOUBLE PRECISION"
else if (is.raw(obj)) "BLOB"
else "VARCHAR(255)"
}, valueClass = "character")
to
setMethod("dbDataType", signature(dbObj="MonetDBConnection", obj = "ANY"), def=function(dbObj, obj, ...) {
if (is.logical(obj)) "BOOLEAN"
else if (is.integer(obj)) "INTEGER"
else if (is.numeric(obj)) "DOUBLE PRECISION"
else if (is.raw(obj)) "BLOB"
else "STRING"
}, valueClass = "character")
in src/monetdb.R before installing the package using R CMD INSTALL. The R-forge builds will also be updated shortly, look for version 0.8.1 at https://r-forge.r-project.org/R/?group_id=1534

Which library is the pr_DB object defined in?

I am completely new to R.
I am trying to use the dist object with a custom function based on the specification here, but I was unable to pass the custom function directly by name, so I tried to add it using the registry described here, but it appears that I am missing a library.
However, I'm not sure which library I need and cannot find a reference to find the name of the library.
Here's a code sample that I'm trying to run:
library(cluster)
myfun <- function(x,y) {
numDiffs <- 0;
for (i in x) {
if (x[i] != y[i])
numDiffs <- numDiffs + 1;
}
return(numDiffs);
}
summary(pr_DB)
pr_DB$set_entry(FUN = myfun, names = c("myfun", "vectorham"))
pr_DB$get_entry("MYFUN")
Here's the error:
Error in summary(pr_DB) : object 'pr_DB' not found
Execution halted
You need to learn the conventions used by R help pages. That "{proxy}" at the top of the page you linked to is really the answer to your question. The convention for the help page construction is "topic {package_name}".

Resources