Search code examples
rshinyrpython

Transforming a checkboxGroupInput object into a vector


After a long time searching for a possible answer I am still unable to create a character vector out of a user input (specifically checkboxGroupInput).

My ultimate purpose is to pass the user selected entries from checkboxGroupInput into a python list (using RPython) for further process. However, I only manage to do that "character by character" instead of the whole word even when using toString. I have read the post Getting multiple checkbox values in Shiny suggesting the double brackets indexing. Actually I did not see any difference in the result when using single brackets and/or toString (see commented out command in server.r). In both ways I end up having variables in python like "L", "o", "n", "d", "o", "n" instead of just "London".

# server.R
library(shiny)
library(rPython)
copy2  <- function(test) {
    python.exec("locations = []")
    for (i in 1:length(test)) {
        python.assign("temp",toString(test[[i]]))
#       python.assign("temp",test[i])
        python.exec("locations.extend(temp)")
    }
    python.exec("print locations")
}

shinyServer(function(input, output) {
    output$chosen <- renderPrint({
        string <- copy2(input$checkGroup)
    })
  }
)


# ui.r    
library(shiny)
library(rPython)

locations <- c("London", "Paris", "Munich")

shinyUI(pageWithSidebar(
    headerPanel(
      "Map",
  ),
    sidebarPanel(
        checkboxGroupInput("checkGroup", 
            label = h3("Select Locations:"), 
            locations,)
    ),
    mainPanel(
    h3('Main Panel text'),
    p('Selected Locations:'),
    verbatimTextOutput("chosen")
    )
))   

What would be the best way to simply transfer the contents of the R Shiny object (the selected values from the checkboxGroupInput) into an R vector or Python list ?

Thank you for the advice.


Solution

  • Well, the problem is in your Python code.

    You have to understand the difference between list.append() and list.extend().

    >>> locations = []            
    >>> locations.extend("London")
    >>> locations
    ['L', 'o', 'n', 'd', 'o', 'n']
    

    versus

    >>> locations = []
    >>> locations.append("London")
    >>> locations
    ['London']