3

I have python reticulate function which returns list of two length tuples of type (id, name).

# In the python API
returned_list = [
    (1, "Jon Doe"),
    (2, "Jane Doe"),
]
library(shiny)
library(reticulate)
# source and other staff

API <- pythonAPI()
values <- API.call() # Returns R list of 2 length lists
values
#   [,1]   [,2] 
# [1,] List,2 List,2
for(index in 1:length(values)){
    message(values[index])
# list(1, "Jon Doe")
# list(2, "Jane Doe")

This values list is supposed to go as choices for shiny's selectInput choices parameter. This choices parameter should have name as the value shown to the user and id as hidden value processed in the program.

selectInput("_id", "Choose name", choices)

How to transform values returned from Python API to fit use-case?

vahvero
  • 525
  • 11
  • 24

1 Answers1

1

My coworker managed to come up with solution.

# Create dataframe scaffold
df <- data.frame("name" = NA, "id" = NA)
# Bind values from values to the scaffold by rows
for (i in seq(values)) {
    df_tmp <- rbind.data.frame(values[[i]])
    colnames(df_tmp) <- c("name", "id")
    df <- rbind(df, df_tmp)
}

## Remove empty first row
df <- df[!is.na(df$name),]
    ​
# Transform to right format for shinyapp UI
choices <- setNames(df$name, df$id)
vahvero
  • 525
  • 11
  • 24