Skip to content

R

from hyperscript import Hyperscript
from hyperscript.convergence import Convergence
def r_script(name):
from hyperscript.Shell import Shell
from hyperscript.R import Rscript
shell = Rscript(src=f"""
data <- data.frame(greeting = c("Hello, {name}!"))
write.csv(data, file="output.csv", row.names=FALSE)
""")
for stdout in shell.execute():
print(stdout)
return File("output.csv") # Return the output file
# Alternative: Load R script from file
def r_script_from_file(name, script_path):
from hyperscript.Shell import Shell
from hyperscript.R import Rscript
shell = Rscript(path=script_path, args=[name])
for stdout in shell.execute():
print(stdout)
return File("output.csv") # Return the output file
async def main():
# Set up Convergence as our backend
convergence = Convergence(api_key=os.getenv("CONVERGENCE_API_KEY"))
# Initialize Hyperscript with Convergence's storage and event systems
async with Hyperscript(
storage=convergence.storage, events=convergence.events
) as hs:
task = await hs.run(
fn=r_script,
args=["Cammy"],
executor=convergence.executor
)
task_2 = await hs.run(
fn=r_script_from_file,
args=["Cammy", "path/to/script.R"],
files=["path/to/script.R"], # Upload the R script file
executor=convergence.executor
)
# Retrieve and print the result
async for file in task.files():
print(f"Got file: {file.name}")
async for file in task_2.files():
print(f"Got file from script file: {file.name}")
# Hyperscript leverages asyncio, so we need to run our main function in an event loop
if __name__ == "__main__":
import asyncio
asyncio.run(main())