R API Suggestion: Return date fields as dates, not factors

LabKey Support Forum (Inactive)
R API Suggestion: Return date fields as dates, not factors Ben Bimber  2010-04-02 08:04
Status: Closed
 
when writing an R view on a query, dates fields seem to be converted to factors in labkey.data. you can convert them back, labkey.data$date = as.Date(labkey.data$date), but it would be nice if this were not needed. graphs work a lot better if dates are dates.
 
 
Peter responded:  2010-04-08 18:31
Ben,
Not sure which version of Rlabkey you're using, but the latest v 2.1.112 has been released on CRAN. I'm pretty sure it always returns non-factor columns from selectRows, getRows and executeSql. I tried it just now on a table with date field and this is what I'm getting:

> library(Rlabkey)
Loading required package: RCurl
Loading required package: bitops
Loading required package: rjson
> s<- getSession("http://localhost:8080/labkey", "/apisamples")
> sc <- getSchema(s,"lists")
> r<- getRows(s, sc$AllTypes)
> str(r)
'data.frame': 10 obs. of 11 variables:
 $ DisplayFld : chr "Row 1" "Row 2 Empty Vals" "Row 3 (plain)" "row 4 (the n word)" ...
 $ TextFld : chr "some text here" NA "More plane values" "null NULL NA and na" ...
 $ IntFld : num 99 NA -34 123 NA NA 777 98 98 98
 $ DoubleFld : num 987.7 NA 98123.4 44.3 NA ...
 $ DateTimeFld :Class 'Date' num [1:10] 14654 NA 10592 11738 NA ...
 $ BooleanFld : logi FALSE FALSE FALSE FALSE FALSE FALSE ...
 $ LongTextFld : chr "Could be some very long text" NA "The quick brown fox jumped over the fence. The quick brown fox jumped over the fence. The quick brown fox jumped over the fen"| __truncated__ NA ...
 $ AttachmentFld: chr "PlainTextDoc.txt" NA NA NA ...
 $ RequiredText : chr "I have to fill this out" "Cannot be empty" "no attachment" "lots of words for empty" ...
 $ RequiredInt : num 42 55 88 456 1 2 0 0 0 0
 $ Category : chr "PLAIN" NA "PLAIN" "EMPTY" ...

> is.factor(r$DateTimeFld)
[1] FALSE


> r1<- cbind(r[, 1:4], factor(r[, 5]), r[,6:11])
> str(r1)
'data.frame': 10 obs. of 11 variables:
 $ DisplayFld : chr "Row 1" "Row 2 Empty Vals" "Row 3 (plain)" "row 4 (the n word)" ...
 $ TextFld : chr "some text here" NA "More plane values" "null NULL NA and na" ...
 $ IntFld : num 99 NA -34 123 NA NA 777 98 98 98
 $ DoubleFld : num 987.7 NA 98123.4 44.3 NA ...
 $ factor(r[, 5]): Factor w/ 4 levels "1999-01-01","2002-02-20",..: 3 NA 1 2 NA NA 4 4 4 4
 $ BooleanFld : logi FALSE FALSE FALSE FALSE FALSE FALSE ...
 $ LongTextFld : chr "Could be some very long text" NA "The quick brown fox jumped over the fence. The quick brown fox jumped over the fence. The quick brown fox jumped over the fen"| __truncated__ NA ...
 $ AttachmentFld : chr "PlainTextDoc.txt" NA NA NA ...
 $ RequiredText : chr "I have to fill this out" "Cannot be empty" "no attachment" "lots of words for empty" ...
 $ RequiredInt : num 42 55 88 456 1 2 0 0 0 0
 $ Category : chr "PLAIN" NA "PLAIN" "EMPTY" ...
> is.factor(r1[,5])
[1] TRUE

Let me know if the update fixes your situation. Also check out the new Vignette and the major speed increase.
 
Peter responded:  2010-04-08 18:49
Whoops as I read this over I realized you were asking about the implicit labkey.data frame in an R view. You can use Rlabkey in an R view; it will utilize the security context of the logged in user.

I'm concerned that if we change the behavior of the implicit labkey.data we will break existing apps. Would the following work as well for you:

library(Rlabkey) make this implicit?
labkey.session built-in automatic current session,
labkey.query built in query object of current rows in the grid

then you could do

mydata <- getRows(labkey.session, labkey.query)

and mydata would have no factors.
 
Ben Bimber responded:  2010-04-08 19:02
the workaround i suggest in my first post of simply converting back to dates isnt all that bad. i can understand the rationale for not changing the default behavior though.

if converting is a problem, it is good to know how to re-create the dataframe though.