2017-09-14 1 views
1

Ohne Programmierkenntnisse in JavaScript und APIs, habe ich einige Probleme, dieses Beispiel zu meinen Bedürfnissen passen: select GitHub repo. Ich versuche, es anzupassen, um mit dieser API zu arbeiten: https://api-adresse.data.gouv.fr/search/? .selectize.js und Shiny: Wählen Sie Optionen aus einer Remote-API

Die Antwort ist eine GeoJSON-Datei, in der die Features in Antwort $ -Funktionen gespeichert sind. Ich möchte das Attribut $ label für jedes Merkmal erhalten.

Hier ist was ich bisher gemacht habe. Ich erhalte eine Reihe, aber die Elemente sind nicht in der Dropdown-Liste angezeigt ...

UI:

######## 
# 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:

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

library(shiny) 

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

Vielen Dank für Ihre Hilfe.

Antwort

1

Es funktioniert dank this Kommentar.

selectize unterstützt keine verschachtelten Werte mit Punktnotation Zugriff

UI:

######## 
# 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:

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

library(shiny) 

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