> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ns.rocks/llms.txt
> Use this file to discover all available pages before exploring further.

# R

> Run R cells in the standard code interpreter template.

The standard `code-interpreter` template includes R and an IRkernel Jupyter
kernel.

<CodeGroup>
  ```python Python SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  result = machine.run_code("""
  values <- c(1, 2, 3, 4)
  mean(values)
  """, language="r")

  print(result.results[0].text)
  ```

  ```typescript TypeScript SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  const result = await machine.code.run(`
  values <- c(1, 2, 3, 4)
  mean(values)
  `, { language: "r" });

  console.log(result.results[0].text);
  ```

  ```bash CLI theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  nullspace machine code run mch_123 "values <- c(1, 2, 3, 4)
  mean(values)" --language r
  ```

  ```bash HTTP API theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  # returns a Server-Sent Events stream of JSON events
  curl -N -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/execute" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
    -H "Content-Type: application/json" \
    -d '{"code": "values <- c(1, 2, 3, 4)\nmean(values)", "language": "r"}'
  ```
</CodeGroup>

R contexts keep variables available across cells:

<CodeGroup>
  ```python Python SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  ctx = machine.code_interpreter.create_code_context(language="r")
  machine.code_interpreter.run_code("values <- c(2, 4, 6)", context=ctx)
  result = machine.code_interpreter.run_code("sum(values)", context=ctx)
  ```

  ```typescript TypeScript SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  const ctx = await machine.code.createContext({ language: "r" });
  await machine.code.run("values <- c(2, 4, 6)", { context: ctx });
  const result = await machine.code.run("sum(values)", { context: ctx });
  ```

  ```bash CLI theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  nullspace machine code context create mch_123 --language r
  nullspace machine code run mch_123 "values <- c(2, 4, 6)" --context <context-id>
  nullspace machine code run mch_123 "sum(values)" --context <context-id>
  ```

  ```bash HTTP API theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  curl -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/contexts" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
    -H "Content-Type: application/json" -d '{"language": "r"}'
  # then run each cell against the returned context id (SSE stream)
  curl -N -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/execute" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
    -H "Content-Type: application/json" \
    -d '{"code": "values <- c(2, 4, 6)", "context_id": "<context-id>"}'
  ```
</CodeGroup>

## Related

* [Supported languages](./languages-overview)
* [Code contexts](./contexts)
