4

I would like to provide a web tool that requires access to a large dataset. Preferentially, this R data object should be loaded into memory once and then be available for user-defined queries.

Does the openCPU framework support persistant data objects in any way or does every query begin with a blank slate?

Superbest
  • 25,318
  • 14
  • 62
  • 134

1 Answers1

5

The OpenCPU API is stateless, there is no way to keep a process alive in between requests. But you can easily solve your problem by putting your dataset into a package, and installing that on the server.

You can use the preload option described in the server manual, or use the /etc/opencpu/Rprofile script to load the package with the dataset in memory when the server starts. That way the data will be will be ready to use when requests come in.

Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207
  • Great ! Thanks a lot for the quick reply. I am working with genomics datasets, but (up to now) it can be loaded into memory. Caching the data will thus probably work for my application. – user1678609 Sep 21 '12 at 22:30
  • A pretty simple way to handle sessions is to save user data in a database like mysql (or sqlite for smaller websites), along with a unique id. Then, after one preloads the relevant database packages in R/opencpu, each new function call will need to look at the cookies and/or request to find the session ID. One might also include some more security, such as adding a timestamp that expires, using cookies instead of url string identifiers, oauth, etc.. – Sasha Goodman Jan 21 '13 at 00:27
  • @Jeroen Is your answer still valid with the latest opencpu release ? If not, whats the best practice to query in memory big dataframes through a restful webservice ? – ajkl Sep 24 '14 at 14:03
  • Thanks @Jeroen ! Any plans in the future to make this available without writing a R package ? I have it captured here - http://stackoverflow.com/questions/26025430/is-there-a-way-to-run-a-script-through-opencpu-without-creating-a-package – ajkl Sep 25 '14 at 02:04
  • 1
    @AjinkyaKale there is no need to be scared of R packages. It's just the natural format to combine some functions, data, files, scripts into an easily maintainable and deployable container. You can make an R package in less than 10 seconds. Have a look at this video: https://www.youtube.com/watch?feature=player_detailpage&v=kAfVWxiZ-Cc#t=858 around 14m. – Jeroen Ooms Sep 25 '14 at 09:13