h2oai / nitro

Create apps 10x quicker, without Javascript/HTML/CSS.
https://nitro.h2o.ai
Apache License 2.0
200 stars 14 forks source link

R API proposal #9

Open lo5 opened 2 years ago

lo5 commented 2 years ago

What's the simplest, most ergonomic and idiomatic equivalent of the following Python code in R?

# Prompt for first and last names.
first_name, last_name = view(
    box('First name', value='Boaty'),
    box('Last name', value='McBoatface'),
)
# Print the entered values
view(f'Hello, {first_name} {last_name}!')

Source: https://nitro.h2o.ai/basics/#get-multiple-inputs-at-once

For context, this is what the above code would've looked like if Python didn't have destructuring assignments:

# Prompt for first and last names.
result = view(
    box('First name', name='first_name', value='Boaty'),
    box('Last name', name='last_name', value='McBoatface'),
)
# Print the entered values
view(f'Hello, {result['first_name']} {result['last_name']}!')
ashrith commented 2 years ago
> library(zeallot)
> box <- function(...){return(list(...))} 
> view <- function(box,...){return(lapply(list(box,...),function(x){return(x$value)}))}
> c(first_name,last_name) %<-% view(box(title="First Name",value="Boaty"),box(title="Last Name",value="McBoaty"))
> first_name
[1] "Boaty"
> last_name
[1] "McBoaty"

Here is a solution.

lo5 commented 2 years ago

So, roughly:

This goes into the library:

library(zeallot)
box <- function(...){return(list(...))} 
view <- function(box,...){return(lapply(list(box,...),function(x){return(x$value)}))}

This is the userland:

c(first_name,last_name) %<-% view(box(title="First Name",value="Boaty"),box(title="Last Name",value="McBoaty"))
view(paste("Hello ", first_name, " ", last_name))
ashrith commented 2 years ago

Thank you! That is wonderful.