【问题标题】:selectize.js and Shiny : select choices from a remote APIselectize.js 和 Shiny :从远程 API 中选择选项
【发布时间】:2017-09-16 05:13:52
【问题描述】:

由于没有 JavaScript 和 API 方面的编程知识,我很难使这个示例满足我的需求:select GitHub repo。 我正在尝试使其适应此 API:https://api-adresse.data.gouv.fr/search/? .

响应是一个 GeoJSON 文件,其中的特征存储在 response$features 中。我想获取每个功能的 properties$label 属性。

这是我到目前为止所做的。我得到一个数组,但项目没有显示在下拉列表中...

用户界面:

########
# ui.R #
########

library(shiny)

fluidPage(
  title = 'Selectize examples',
  mainPanel(
    selectizeInput('addresses', 'Select address', choices = '', options = list(
      valueField = 'properties.label',
      labelField = 'properties.label',
      searchField = 'properties.label',
      options = list(),
      create = FALSE,
      render = I("
  {
    option: function(item, escape) {
      return '<div>' + '<strong>' + escape(item.properties.name) + '</strong>' + '</div>';
    }
  }"  ),
      load = I("
  function(query, callback) {
    if (!query.length) return callback();
    $.ajax({
      url: 'https://api-adresse.data.gouv.fr/search/?',
      type: 'GET',
      data: {
        q: query
      },
      dataType: 'json',
      error: function() {
        callback();
      },
      success: function(res) {
        console.log(res.features);
        callback(res.features);
      }
    });
  }"
     )
    ))
  )
)

服务器:

############
# server.R #
############

library(shiny)

function(input, output) {
  output$github <- renderText({
    paste('You selected', if (input$github == '') 'nothing' else input$github,
          'in the Github example.')
  })
}

感谢您的帮助。

【问题讨论】:

    标签: r shiny selectize.js


    【解决方案1】:

    感谢this 评论,它可以正常工作。

    selectize 不支持使用点表示法访问嵌套值

    用户界面:

    ########
    # ui.R #
    ########
    
    library(shiny)
    
    fluidPage(
      title = 'Selectize examples',
      mainPanel(
        selectizeInput('addresses', 'Select address', choices = '', options = list(
          valueField = 'name',
          labelField = 'name',
          searchField = 'name',
          loadThrottle = '500',
          persist = FALSE,
          options = list(),
          create = FALSE,
          render = I("
      {
        option: function(item, escape) {
          return '<div>' + '<strong>' + escape(item.name) + '</strong>' + '</div>';
        }
      }"  ),
          load = I("
      function(query, callback) {
        if (!query.length) return callback();
        $.ajax({
          url: 'https://api-adresse.data.gouv.fr/search/?',
          type: 'GET',
          data: {
            q: query
          },
          dataType: 'json',
          error: function() {
            callback();
          },
                success: function (data) {
                    callback(data.features.map(function (item) {
                        return {name: item.properties.name, 
                      label: item.properties.label,
                      score: item.properties.score};
                    }));
                }
    
    
        });
      }"
         )
        ))
      )
    )
    

    服务器:

    ############
    # server.R #
    ############
    
    library(shiny)
    
    function(input, output) {
      output$github <- renderText({
        paste('You selected', if (input$github == '') 'nothing' else input$github,
              'in the Github example.')
      })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多