Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Arkadi
    @aavanesy
    Thanks
    Will program a complex API function today.
    I have been testing small api requests (hosted on aws, docker, germany) and requests are pretty fast, 20-30ms.
    Is this comparable to django rest api?
    Dmitriy Selivanov
    @dselivanov
    everything depends on the handler code and network. RestRserve overhead is pretty small (~0.1ms).
    Arkadi
    @aavanesy
    Sounds great.
    Arkadi
    @aavanesy

    Hi again,
    I started to use the tool more extensively, everything is great so far.

    Just talked to a friend and he shared me this
    https://github.com/the-benchmarker/web-frameworks

    where restrserve (0.3) is quite behind vs other frameworks.

    Are these numbers accurate overall?

    I will never have more than 50-100 requests per second but just curious if R is this behind the other frameworks?

    Dmitriy Selivanov
    @dselivanov
    I would say yes. You can see pure Rserve does ~42k req/seq but it's barebone - you need to implement input validations, exception handling, etc. Which in interpreted language like R takes some time.
    On the other side when you do some useful computations (if you spend at least 1-5 ms to compute something) such benchmarks don't make much sense and platforms above will perform similarly.
    Arkadi
    @aavanesy
    I do complex computations usually, dtplyr, financial data.

    I would say yes. You can see pure Rserve does ~42k req/seq but it's barebone - you need to implement input validations, exception handling, etc. Which in interpreted language like R takes some time.
    On the other side when you do some useful computations (if you spend at least 1-5 ms to compute something) such benchmarks don't make much sense and platforms above will perform similarly.

    Thanks for the reply.

    Arkadi
    @aavanesy
    Quick question, I tried to change the default docker into this
    WORKDIR /
    
    EXPOSE 8080
    
    CMD ["Rscript", "-e", "source('/home/ubuntu/nqr_r_backend/app.R'); backend$start(app, http_port = 8080)"]
    But it keeps failing and cannot find the file
    This on the other hand works well
    FROM rocker/tidyverse
    RUN install2.r --error RestRserve
    RUN install2.r --error Quandl
    RUN install2.r --error PerformanceAnalytics
    RUN install2.r --error xts
    RUN install2.r --error quantmod
    
    COPY / /
    
    EXPOSE 8080
    
    ENTRYPOINT ["Rscript", "app.R"]
    How can I specify where my app.R is without copying it to the image?
    Arkadi
    @aavanesy
    Ideally I want to just to a git pull and docker would pick up the latest app.r file
    without rebuilding
    Dmitriy Selivanov
    @dselivanov
    This is nothing to do with RestRserve. Likely you need to mount dir from your local machine to container during docker run.
    Arkadi
    @aavanesy
    Hi again!
    Sorry to annoy with these questions, I program with this 6 hours per day now.
    I have a question about .res$set_content_type("application/json")
    How does this work precisely? Because if my .res$set_body(output) is already a JSON, it kind of wraps it one more time into JSON.
    And it wraps any output I have into JSON. Ideally I would like to configure myself the format of the JSON using jsonlite package
    Dmitriy Selivanov
    @dselivanov
    Easiest way is to set .res$encode = identity
    Arkadi
    @aavanesy
    Thanks Dmitriy, will check tomorrow.
    Arkadi
    @aavanesy
    Getting close to production launch.
    Another question, my front-end colleague gets this message when fetching from api using JS
    Access to XMLHttpRequest at 'api_url' 
    from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' 
    header is present on the requested resource
    how could I solve this? I read in the pdf documentation that CORS is disabled by default
    Arkadi
    @aavanesy

    Hi @dselivanov
    I was able to succesfully test CORS with plain text and GET.
    However I am not able to have work with JSON. If I do this

     app$add_route("/bth", method = "OPTIONS", FUN = function(req, res) {
       res$set_header("Allow", "GET, OPTIONS")
     })

    And in my function I have

      results = data.frame(x = 123)
    
      .res$set_body(results)
       .res$set_content_type("application/json")

    I receive "500 Internal Server Error (body is not character or raw)"

    So I checked the code and I see this
          if (isTRUE(is.raw(body) || is.character(body))) {
            return(list(body, response$content_type, headers, response$status_code))
          } else {
            return(list("500 Internal Server Error (body is not character or raw)", "text/plain", headers, 500L))
          }
    Is there a reason why the body must be character or raw?
    Arkadi
    @aavanesy
    I think I solved this.
    I could write an example for the page if you want.
    This package is just great
    Dmitriy Selivanov
    @dselivanov
    @aavanesy would be nice if you could add CORS vignette to https://github.com/rexyai/RestRserve/tree/master/vignettes. As for raw output - REST clients can only read bytes and interpret as per coupled MIME code. So you need to encode your R object response into bytes. RestRserve tries to do so automagically for application/json, but for other types you need to do it yourself.
    Stefano Borghi
    @stebogit
    hello, I'm totally new to R environment and I'm trying to set up a PHP REST API server which can run R scripts in some endpoints
    I know I could run R scripts via CLI (i.e. exec('Rscript -e mysscript.R', $output, $result) see https://stackoverflow.com/a/53172260/4530144)
    Stefano Borghi
    @stebogit
    however I need to pass a significant amount of data to the script and I don't think using CLI parameters/arguments would work (it would at least be ugly :D)
    now, I think a cleaner solution would be implementing an internal (to the VM running the API) HTTP (or TCP?) server which I can then interface with "standard" URL queries, where I can POST as much data as I like
    Stefano Borghi
    @stebogit
    in my understanding there are two possible options here, Rserve and FastRWeb. However it looks like to me Rserver is TCP service and I'm not sure how to interact with it in PHP, while FastRWeb is a web server that only serves static files
    is the above correct?
    any suggestions?
    in other words, ideally I'd like to set up an internal service that in my app I can interact with via $result = $client->post('http://localhost:12345/myRscript', ['parameter', 'some value']) and get back a (JSON or HTML) string as a result
    Stefano Borghi
    @stebogit
    maybe https://www.rplumber.io is what I was looking for...
    Dmitriy Selivanov
    @dselivanov

    There is an example on the very first paragraph of the readme.

    library(RestRserve)
    app = Application$new()
    
    app$add_get(
      path = "/health", 
      FUN = function(.req, .res) {
        .res$set_body("OK")
      })
    
    app$add_post(
      path = "/addone", 
      FUN = function(.req, .res) {
        result = list(x = .req$body$x + 1L)
        .res$set_content_type("application/json")
        .res$set_body(result)
      })
    
    
    backend = BackendRserve$new()
    backend$start(app, http_port = 8080)

    Why it doesn't work for you?

    Stefano Borghi
    @stebogit
    ah, wow, that's right. Not sure why but I honestly did not see that...
    how does RestRserve compare to Plumber?
    Dmitriy Selivanov
    @dselivanov
    They are both libraries to build REST API. There are some nuances (syntax, performance, etc), but overall you can use any of them.
    Stefano Borghi
    @stebogit
    Thanks @dselivanov!
    Stefano Borghi
    @stebogit
    is there a solution for automatic restart of the server while development?
    like whenever a file changes