oracle / graal

GraalVM compiles Java applications into native executables that start instantly, scale fast, and use fewer compute resources 🚀
https://www.graalvm.org
Other
20.39k stars 1.64k forks source link

R integration how to apply ajax response on data #1459

Closed akashchavda closed 5 years ago

akashchavda commented 5 years ago

I am new in R integration with Spring boot and i got below example for R integration with Spring boot, https://medium.com/graalvm/enhance-your-java-spring-application-with-r-data-science-b669a8c28bea

I was follow all step from that link and its working properly, but now i need to use data that come from API.

plot.R

library(ggplot2)
data <<- numeric(100)

function(dataHolder) {
    svg()
    data <<- c(data[2:100], dataHolder$value)

    logHolder <- java.type("org.graalvm.demos.springr.LogHolder")
    logHolder$log(dataHolder$value, data[90:100])

    plot <- ggplot(data = data.frame(systemLoad = data, time = -99:0),
                aes(x=time, y=systemLoad, group=1)) +
                geom_line(color="orange") +
                expand_limits(x=0, y=0)
    print(plot)
    svg.off()
}

How to apply my ajax response on that data field?

steve-s commented 5 years ago

Hello akashchavda,

can you specify in more detail what you would like to achieve? If you want to pass additional parameters from Java to the R function, you can just add more parameters to the R function itself:

function(myData, dataHolder) {
    svg()
    data <<- c(as.double(myData), dataHolder$value)
     # ...

then the R function will no longer conform to the Function<Double, String> (because it now takes 2 parameters), so this line:

return ctx.eval(source).as(Function.class);

has to change to

return ctx.eval(source).as(BiFunction.class);

and the return type and the field type have to be changed accordingly. Now when invoking the function, you have to provide 2 arguments:

plotFunction.apply(new double[] { 1.1, 2.2, 3.3 }
       ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage())

Some more documentation on Truffle languages <-> Java interop in general: https://www.graalvm.org/docs/reference-manual/embed/

High-level documentation of GraalVM R engine: https://www.graalvm.org/docs/reference-manual/languages/r/#graalvm-integration

Executable specification of how R treats Java objects: https://github.com/oracle/fastr/blob/master/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/library/fastr/R/interop-array-conversion-test.R#L158

akashchavda commented 5 years ago

I have read csv file and showing content on ggplot but i got below exception Error:datamust be a data frame, or other object coercible byfortify(), not an S3 object with class gg/ggplot can you tell me how to solve this error?

library(ggplot2)

function(dataHolder) {
    dataContent =   read.csv("test.csv")
    dataplot_leafdashbd <- ggplot(data = dataContent, mapping = aes(x= date ,y = value, pch=name)) +
    theme_light() +
    geom_point() +
    scale_shape_manual(values = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19))+
    geom_hline(data = dataContent, linetype="dotted", colour="#ff710c",alpha = 0.5, aes(yintercept = range_min)) +
    geom_hline(data = dataContent, linetype="dotted", colour="#ff710c", alpha = 0.5, aes(yintercept = range_max))

    attrnames <- c(
    '1'="A1",
    '2'="A2",
    '3'="A3",
    '4'="A4",
    '5'="A5",
    '6'="A6",
    '10'="A7",
    '13'="A8",
    '15'="A9"
    )

    theleafplot <- (dataplot_leafdashbd +
    theme(
    axis.ticks = element_blank(),
    axis.title.x = element_blank(),
    axis.text.x = element_blank(),
    legend.title = element_blank(),
    panel.grid.major.x=element_blank()
    )+
    facet_wrap(order_seq ~ ., scales = "free_y", ncol=3, labeller = labeller(order_seq = attrnames)))
    ggplot(theleafplot)
}
steve-s commented 5 years ago

Thank you for providing more context. This is not an issue of R integration with the rest of GraalVM, but issue with the use of the R programming language itself. I am guessing that the error comes from this line:

ggplot(theleafplot)

perhaps you wanted to do

plot(theleafplot)

note that if you wanted to return SVG code of the plot, you should do something along these lines:

svg()
plot(theleafplot)
return(svg.off())
akashchavda commented 5 years ago

is it possible with graalvm to show chart with tooltips and zoom feature?

steve-s commented 5 years ago

For interactive graphics, you will be probably better off with https://ggvis.rstudio.com/ than ggplot2 R package. We haven't done any experiments integrating ggvis with GraalVM, but it should work like with any other R package.

akashchavda commented 5 years ago

ok thanks @steve-s . thanks for your help..