Skip to content

Commit

Permalink
[python] add support to run r applications
Browse files Browse the repository at this point in the history
  • Loading branch information
javierluraschi committed Dec 9, 2024
1 parent 1cd45c0 commit 97d4c45
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.8.2

- Add support to run r applications with plumber and shiny

## 2.8.1

- Automatically create `.storage` folder when it does not exist
Expand Down
5 changes: 5 additions & 0 deletions python/hal9/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
from hal9.runtimes.image import run as run_image
from hal9.runtimes.python import run as run_python
from hal9.runtimes.chainlit import run as run_chainlit
from hal9.runtimes.r import run as run_r
from hal9.runtimes.plumber import run as run_plumber

runtime_types = {
"python": run_python,
"image": run_image,
"jpg": run_image,
"png": run_image,
"chainlit": run_chainlit,
"r": run_r,
"shiny": run_r,
"plumber": run_plumber,
}

def run(path :str, source :str = "app.py", runtime :str = "python", port :str = "8080") -> str:
Expand Down
8 changes: 8 additions & 0 deletions python/hal9/runtimes/plumber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import subprocess
from pathlib import Path

def run(source_path :Path, port :str):
code = f"library(plumber);pr_run(pr('{source_path}'), port={port})"
command = ['Rscript', '-e', code]
with subprocess.Popen(command) as proc:
proc.wait()
15 changes: 15 additions & 0 deletions python/hal9/runtimes/r.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import subprocess
from pathlib import Path

def run(source_path :Path, port :str):
rprofile_content = f"""
options(shiny.port = {port})
"""

rprofile_path = Path.cwd() / ".Rprofile"
with open(rprofile_path, "w") as rprofile_file:
rprofile_file.write(rprofile_content)

command = ['Rscript', source_path]
with subprocess.Popen(command) as proc:
proc.wait()
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hal9"
version = "2.8.1"
version = "2.8.2"
description = ""
authors = ["Javier Luraschi <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 97d4c45

Please sign in to comment.