【问题标题】:Transforming a checkboxGroupInput object into a vector将 checkboxGroupInput 对象转换为向量
【发布时间】:2014-08-10 16:31:23
【问题描述】:

经过很长时间寻找可能的答案后,我仍然无法从用户输入(特别是 checkboxGroupInput)中创建字符向量。

我的最终目的是将用户选择的条目从 checkboxGroupInput 传递到 python 列表(使用 RPython)以进行进一步处理。但是,即使使用 toString,我也只能做到“逐个字符”而不是整个单词。我已阅读帖子Getting multiple checkbox values in Shiny 建议使用双括号索引。实际上,当使用单括号和/或 toString 时,我没有看到结果有任何差异(请参阅 server.r 中注释掉的命令)。在这两种方式中,我最终在 python 中都有变量,比如“L”、“o”、“n”、“d”、“o”、“n”,而不仅仅是“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")
    )
))   

将 R Shiny 对象的内容(从 checkboxGroupInput 中选择的值)简单地传输到 R 向量或 Python 列表中的最佳方法是什么?

感谢您的建议。

【问题讨论】:

    标签: r shiny rpython


    【解决方案1】:

    嗯,问题出在你的 Python 代码中。

    您必须了解list.append()list.extend() 之间的区别。

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

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

    【讨论】:

      猜你喜欢
      • 2021-06-02
      • 2015-07-09
      • 2014-01-27
      • 1970-01-01
      • 2016-07-07
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多