r - Shiny + selectize.js : render custom item from local source

标签 r shiny selectize.js

我正在开发一个应用程序,用户将上传数据,然后从该数据中选择要绘制在图表上的变量。我希望变量的类型显示在选择器中。

我正在使用示例 herehere (see "email contacts") 。 Shiny 的 selectize 示例通过 ajax 获取数据,但我不知道如何使其适应用户上传的数据,这些数据作为 reactiveValues 对象存储在我的应用程序中。我还尝试使用 selectize 网站示例中的语法。我所有的努力都导致选择器出现,没有任何可见的数据。

  # the code that creates the data I'm hoping to pass to the selector 
  stored_data$variable_selector <- data_frame(
    varlist = names(stored_data$data), 
    type = map_chr(names(stored_data$data), ~class(stored_data$data[[.x]]))
    ) %>% 
    purrr::transpose()

  # the code which currently renders an empty selector
  selectizeInput('variable', 'select a variable:', choices = '', options = list(
    valueField = 'varlist',
    labelField = 'type',
    searchField = 'varlist',
    options = stored_data$variable_types,
    create = FALSE,
    render = I("{
      option: function(item, escape) {
      return '<div>' +
           ' <em>' + escape(item.varlist) + '</em>' +
           ' (by ' + escape(item.type) + ')' +
    '</div>';
  }
}")))

我应该将variable_types 制作成json 对象,然后再将其传递给selectize 中的渲染吗?我还有什么遗漏的吗?

最佳答案

看起来我测试不正确:下面呈现了一个 selectizeInput,其类型信息在下拉列表中可见。

library(shiny)
library(tidyverse)

iris_type <- data_frame(
  varlist = names(iris), 
  type = map_chr(names(iris), ~class(iris[[.x]]))
) %>% 
  purrr::transpose()

ui <- basicPage(


  # the code which currently renders an empty selector

    tagList(

    selectizeInput('variable', 'select a variable:', choices = '',
      options = list(
        valueField = 'varlist',
        labelField = 'varlist',
        searchField = c('varlist', 'type'),
        options = iris_type,
        create = FALSE,
        render = I(
          "{
            option: function(item, escape) {
            return '<div>' +
               ' <em>' + escape(item.varlist) + '</em>' +
               ' (type: ' + escape(item.type) + ')' +
               '</div>';
               }
             }"
            )
          )
        ),
    textOutput("out")
    )


)

server <- function(input, output, session) {
  output$out <- renderText(input$variable)
}

shinyApp(ui, server)

关于r - Shiny + selectize.js : render custom item from local source,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51576624/

相关文章:

r - 如何更改 heatmap.2 中的色键高度

r - Rmarkdown 文档中的源嵌套 R 文件

每当使用 R 在向量中为零时重置累积和

r - 在多行对齐上绘制轴标签

html - 在 Shinyapps.io 上托管时, Shiny 的 Rmarkdown selectInput 不起作用 - 选项包含 <a0>

javascript - 如何根据另一个选择下拉列表中的选择从选择下拉列表中删除选项?

html - 如何在 Shiny 的应用程序的 navbarPage 中设置 tabPanel 标题的颜色?

R Shiny : dygraphs series reacting to `date_window`

javascript - 如何从 Selectize 中删除一个项目?

javascript - 从 selectizeInput 获取不完整的输入