R dataframe from external data source to data grid

LabKey Support Forum
R dataframe from external data source to data grid dhutchison  2020-08-06 18:21
Status: Active
 

Hi,

I have an R script that queries an external data source (through jdbc, not labkey.xml) and would like to display the resulting dataframe as a data grid in labkey. I want the data to be dynamically loaded - that is, the query runs each time the report is viewed. I'm thinking this should be straightforward, but not quite seeing how to do it.

For example:

result<- dbGetQuery(con, sql)
# ${htmlout:csvfile}
write(unlist(result), file="csvfile")

will output the result text in the report tab, but not as a grid.

If I don't "unlist", I get an error:

Error in cat(x, file = file, sep = c(rep.int(sep, ncolumns - 1), "\n"), :
argument 1 (type 'list') cannot be handled by 'cat'"

because write expects an atomic list, I guess.

Any thoughts on how to wrangle the dataframe into a Data Grid?

Thanks

 
 
chetc (LabKey Support) responded:  2020-08-21 10:35
Status: Closed

Hello,

You can use (below) to get a grid like output. labkey.data would be the dataframe you pass in and be sure to include the comment, that allows labkey to display the tsv created. You can get more information on other kinds of outputs under the help tab in the report builder.

${tsvout:tsvfile}

write.table(labkey.data, file = "tsvfile", sep = "\t", qmethod = "double", col.names=NA)

Thanks,
Chet