Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R API proposal #9

Open
lo5 opened this issue Jun 10, 2022 · 3 comments
Open

R API proposal #9

lo5 opened this issue Jun 10, 2022 · 3 comments
Assignees
Labels
area/protocol Related to signaling protocol area/R Related to R backend type/proposal Proposals

Comments

@lo5
Copy link
Member

lo5 commented Jun 10, 2022

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']}!')
@lo5 lo5 added area/protocol Related to signaling protocol type/proposal Proposals area/R Related to R backend labels Jun 10, 2022
@lo5 lo5 self-assigned this Jun 10, 2022
@ashrith
Copy link
Member

ashrith commented Jun 10, 2022

> 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
Copy link
Member Author

lo5 commented Jun 10, 2022

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
Copy link
Member

ashrith commented Jun 10, 2022

Thank you! That is wonderful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/protocol Related to signaling protocol area/R Related to R backend type/proposal Proposals
Projects
None yet
Development

No branches or pull requests

2 participants