Auto complete text field in Shiny

2.5k views Asked by At

This question is inspired by, though not the same as this. Specifically, I wish to match all "Maples" not just where "Maple" is the first word.

I am using a text field to help a user select a common tree name. There are 246 choices. So, I would like to use type ahead to help the user find a tree.

If a user types maple, the auto selection should include trees like: Maple, Sugar Maple, Norway Maple

However the code below using the shinysky package doesn't search past the first word. Is there a work around for this? Is shinysky the right approach here? Would selectizeInput() be better?


library(shinysky)
library(shiny)
library(tidyverse)

my_data <- read.csv("common_tree_names_246.csv", header = FALSE)$V1
my_list <- as.character(my_data)

ui <- shinyUI(
  fluidPage(tags$style(type="text/css",".shiny-output-error { visibility: hidden; }",".shiny-output-error:before { visibility: hidden; }"),
            tags$style(type="text/css","#search { top: 50% !important;left: 50% !important;margin-top: -100px !important;margin-left: -250px 
                       !important; color: red; font-size: 5px; font-style: italic;}"),         

            mainPanel(  
              # one way of doing it
              textInput.typeahead(id = "search",
                                  placeholder = "Tree type?",
                                  local = data.frame(name = c(my_list)),
                                  valueKey = "name",
                                  tokens = c(1:length(my_list)),
                                  template = HTML("<p class='repo-language'>{{info}}</p> <p class='repo-name'>{{name}}</p>")
                                  )
                    )
            )
)

server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)

dput(my_list)
c("American Chestnut", "American Beech", "Beech", "European Beech", 
"European Copper Beech", "European Purpleleaf Beech", "European Beech \"Columnar\"", 
"White Oak", "Swamp White Oak", "Bur Oak", "Chinkapin Oak", "Pin Oak", 
"English Oak", "Pyramidal English Oak", "Red Oak", "Oak", "Black Oak", 
"Mophead Catalpa", "Northern Catalpa", "European Alder", "Alder", 
"Yellow Birch", "Black Birch", "River Birch", "River Birch 'Heritage", 
"White Birch", "European Birch", "European Silver Birch", "European Cutleaf Birch", 
"European Young's Birch", "Grey Birch", "Birch", "Himalayan Birch 'Jacquemonti'", 
"European Hornbeam", "European Hornbeam Pyramidal", "Blue-Beech (American Hornbeam)", 
"American Hazel", "Turkish Hazel", "Hazel", "Iron-Wood (Hop-Hornbeam)", 
"Gum Black", "European Buckthorn", "Amur Cork", "False Cypress", 
"Juniper", "Eastern Red Cedar", "Dawn Redwood", "Bald Cypress", 
"Eastern White Cedar", "Cedar", "Pink Flowering Dogwood", "Cornelliancherry Dogwood", 
"Dogwood", "Accolade Elm", "White Elm", "White Elm 'Delaware'", 
"Pioneer Elm", "White Elm 'Princeton'", "American Liberty Elm", 
"Valley Forge Elm", "Davidiana Elm", "Scotch Elm", "Japanese Elm", 
"Chinese Elm", "English Elm", "Siberian Elm", "Slippery (Red) Elm", 
"Elm", "Rock Elm", "Halka Zelkova", "Zelkova", "Ginkgo", "Ginkgo 'Autumn Gold'", 
"Ginkgo \"Columnar\"", "Eastern Hemlock", "Hack-Berry", "Japanese Katsura", 
"Sassafras", "Basswood", "Redmond Linden 'Continental", "Redmond Linden", 
"Littleleaf Linden", "Littleleaf Glenleven Linden", "Littleleaf Greenspire Linden", 
"Summer Sprite Linden", "Linden", "Crimean Linden", "Tulip-Tree", 
"Cucumber Magnolia", "Saucer Magnolia", "Hedge Maple", "Amur Maple", 
"Paperbark Maple", "Manitoba Maple", "Black Maple", "Black Maple 'Green Column'", 
"Japanese Maple", "Striped Maple", "Norway Maple", "'Autumn Blaze' Norway Maple", 
"Columnar Norway Maple", "Crimson King Norway Maple", "Deborah Norway Maple", 
"Emerald Queen Norway Maple", "Globe Norway Maple", "Royal Red Norway Maple", 
"Schwedler Norway Maple", "Summershade Norway Maple", "Superform Norway Maple", 
"Sycamore Maple", "Red Maple", "Armstrong Columnar Red Maple", 
"Karpick Red Maple", "Silver Maple", "Silver Pyramid Maple", 
"Silver Queen Silver Maple", "Sugar Maple", "Green Mountain Sugar Maple", 
"Legacy Sugar Maple", "Maple", "Mountain Maple", "Tatarian Maple", 
"Shantung Maple 'Warrenred'", "Freeman Maple 'Armstrong'", "Freeman Maple 'Autumn Blaze'", 
"Freeman Maple 'Celebration'", "Freeman Maple 'Jeffersred'", 
"Freeman Maple 'Sienna Glen'", "Freeman Maple 'Marmo'", "White Mulberry", 
"Weeping White Mulberry", "Black (Red) Mulberry", "Mulberry", 
"Russian-Olive", "White Ash", "White Ash 'Autumn Applause'", 
"White Ash 'Autumn Purple'", "European Ash", "Black Ash", "Fall Gold Black Ash", 
"Red (Green) Ash", "Marshall Red Ash", "Patmore Ash", "Summit Red Ash", 
"Blue Ash", "Ash", "Japanese Tree Lilac", "Lilac", "Siberian Peashrub", 
"Red-Bud", "Yellow-Wood", "Ruby Lace Honey Locust", "Honey Locust", 
"Shademaster Honey Locust", "Skyline Honey Locust", "Sunburst Honey Locust", 
"Kentucky-Coffee Tree", "Golden-Chain", "Amur Maackia", "Black Locust", 
"Black Locust Purple Robe", "Japanese Pagodatree", "Silver Fir", 
"Balsam Fir", "White Fir", "Fir", "Larch (Tamarack)", "Norway Spruce", 
"White Spruce", "Black Spruce", "Serbian Spruce", "Colorado Blue Spruce", 
"Spruce", "Jack Pine", "Austrian Pine", "Red Pine", "Pine", "White Pine", 
"Scots Pine", "Douglas Fir", "Service-Berry", "Hawthorn", "Hawthorn 'Winter King'", 
"Hawthorn 'Tobas'", "Crab Apple", "Apple", "Apricot", "Pin Cherry", 
"Peach", "Sargent Cherry", "Black Cherry", "Japanese Cherry", 
"Cherry", "Plum", "Cherry Weeping", "Flowering Almond", "Choke Cherry", 
"Shubert Choke Cherry", "Purpleleaf Cherry", "Bradford Pear", 
"Callery Pear 'Chanticleer'", "Pear", "American Mountainash", 
"Whitebeam Mountainash", "European Mountainash", "Mountain Ash", 
"Oakleaf Mountainash", "American Smoketree", "Ohio Buckeye", 
"Horsechestnut", "Yellow Buckeye", "Horsechestnut (Ruby Red)", 
"Varnish Tree", "Winged Euonymus (Burning Bush)", "Sumach Staghorn", 
"American Sweetgum", "Sycamore Plane", "London Plane", "Bloodgood London Plane", 
"Varnish Tree", "Bitternut Hickory", "Shagbark Hickory", "Hickory", 
"Butternut (White Walnut)", "Butternut Hybrid (White Walnut)", 
"Black Walnut", "English Walnut", "Walnut", "Boleana White Poplar", 
"Silver White Poplar", "Balsam Poplar", "Cottonwood Poplar", 
"Largetooth Aspen (Poplar)", "Lombardy Poplar", "Poplar", "Trembling Aspen (Poplar)", 
"Carolina Poplar", "Pussy Willow", "Corkscrew Willow", "Black Willow", 
"Laurel Willow", "Willow", "Weeping Willow", "Witch-Hazel", "Japanese Yew", 
"Yew")
1

There are 1 answers

4
Mahdi Baghbanzadeh On BEST ANSWER

one way of doing this: 1-read your data in the server.r file 2-make a textinput module in server.r file

in server.r :

library(shiny)
library(shinydashboard)

function(input, output){

  my_list <- reactive({
    data <- read.csv("common_tree_names_246.csv", header = FALSE)$V1
    my_list <- as.character(data)

  })

  output$tree <- renderUI({

    selectInput(inputId = "tree", label = "Trees", choices = my_list())
  })
}

and in ui.r:

library(shiny)
library(shinydashboard)

dashboardPage(
    dashboardHeader(),
    dashboardSidebar(),
    dashboardBody(
      uiOutput("tree")
    )
)