s-u / rJava

R to Java interface
https://RForge.net/rJava
235 stars 77 forks source link

Can objects generated by rJava be saved locally? #324

Closed yunmika closed 8 months ago

yunmika commented 8 months ago

Hi, I attempted to save objects generated by rJava using saveRDS to my local machine. However, upon attempting to re-read them using readRDS, I found that the objects were corrupted. Are there alternative methods to avoid this issue?

s-u commented 8 months ago

Yes, they can as long as the Java objects you save support serialization.

Example:

> library(rJava)
> .jinit()
> (s = .jnew("java.lang.String", "hello!"))
[1] "Java-Object{hello!}"
> .jcache(s)
> saveRDS(s, "java.rds")

(new session)

> library(rJava)
> .jinit()
> (s = readRDS("java.rds"))
[1] "Java-Object{hello!}"
> s$toString()
[1] "hello!"
> 

If you have issues, you'll have to elaborate on the exact Java and R code you are using to create, save and restore the state.

yunmika commented 8 months ago

Thanks for your reply, the issue has been successfully resolved.