# Hello World Agent

This - obviously - is the most basic example. We are calling a function in R and receive its return value.

{% stepper %}
{% step %}

### Prepare R code to be called remotely

Let's pretend we wanted to remotely call this very simple R code:

```r
greet <- function(whom) {
  paste("Hello,", whom)
}
```

Then all we have to do is to load the `vrpc` library and add a single line:

*app.R*

```r
library("vrpc")

greet <- function(whom) {
  paste("Hello,", whom)
}

start_vrpc_agent(domain = "public.vrpc")
```

When you run this code, using e.g.

```r
Rscript app.R
```

a VRPC agent will be created which connects to the vrpc.io cloud and keeps listening for incoming instructions under the public and free domain "public.vrpc".
{% endstep %}

{% step %}

### Use a Node.js client to call the R code

Having started the agent, we can now call the code from any remote location using any VRPC-supported programming language.

Let's see how that would work with Javascript (Node.js):

*index.js*

```javascript
const { VrpcRemote } = require('vrpc')

if (process.argv.length < 3) {
  console.log('Usage: node index.js <agentName>')
  process.exit(1)
}

// retrieve agent name from command line
const agent = process.argv[2]

async function main () {
  // create and connect VRPC client
  const client = new VrpcRemote({ agent, domain: 'public.vrpc' })
  await client.connect()

  // call the R function as static function of the Session class
  const ret = await client.callStatic({
    className: 'Session',
    functionName: 'greet',
    args: ['world!']
  })

  // print the result
  console.log(ret) // Hello, world!
}

main()
```

{% hint style="warning" %}
**IMPORTANT**

Any R code that is remotely available will be encapsulated within a `Session` class. You may create (remote-) instances of that class if you want need stateful interaction with the R code. Otherwise, you can simply call the functions statically.
{% endhint %}

To see how it works, you need to first install the required Node.js packages:

```
npm install
```

And then execute:

```
node index.js <yourAgent>
```

using the correct agent name as displayed after having started the R code.
{% endstep %}
{% endstepper %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.heisenware.com/developers/vrpc/languages/r/examples/hello-world-agent.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
