Can typeahead be implemented over a dynamically changing dataframe using shinysky?

297 Views Asked by At

I am trying to populate a Typeahead box in Shiny, using the ShinySky package in R.

I'm trying to extend the example, where the data used to prepopulate the Typeahead is hardcoded into the textInput.typeahead function:

textInput.typeahead(
        id="thti"
        ,placeholder="type 'name' or '2'"
        ,local=data.frame(name=c("name1","name2"),info=c("info1","info2")) #<- LOOK!
        ,valueKey = "name"
        ,tokens=c(1,2)
        ,template = HTML("<p class='repo-language'>{{info}}</p> <p class='repo-name'>{{name}}</p> <p class='repo-description'>You need to learn more CSS to customize this further</p>")
      )

Having a local dataframe defined in the middle of the function is not what I would like to do, as the example has done here:

,local=data.frame(name=c("name1","name2"),info=c("info1","info2")) 

I would like to supply an argument to local that is a reactive object, which is created elsewhere in Shiny.

So far I've been unable to do so.

Here's my strategy for attempting to populate the Lookhead options dynamically using reactivity:

1) Let the user subset a dataframe using a slider.
2) Set up the Lookahead to read in the subsetted dataframe, using something like ,local=subset(DF)
3) Hope that the Lookahead works as it's supposed to.

Seems simple enough? Here's a screenshot, where you can clearly see that the Lookhead doesn't appear underneath the user input of 111. Below is my code. Any help would be greatly appreciated.

enter image description here

library(shiny)
library(shinysky)
options(shiny.trace = F)  # change to T for trace

DF <- data.frame(ID=c("111", "222", "333", "444"), info=c("info1", "info2", "info3", "info4"))

runApp(list(ui = pageWithSidebar(
  headerPanel("This is a test"),
  sidebarPanel(
    helpText("I couldn't live without StackOverflow"),

    sliderInput("range",
                label = "Pick how many rows you want in your dataframe:",
                min = 2, max = 4, value = 2, step=1),

    helpText("After subsetting the dataframe using the controls above, can we make the Lookahead work?"),

    textInput.typeahead(
      id="thti"
      ,placeholder="type ID and info"
      ,local=subset(DF)
      ,valueKey = "ID"
      ,tokens=c(1,2)
      ,template = HTML("<p class='repo-language'>{{info}}</p> <p class='repo-name'>{{ID}}</p> <p class='repo-description'></p>"))

  ),
  mainPanel(textOutput("text1"),
            htmlOutput("text"),
            tableOutput('table')
  )
),
server = function(input, output, session) {

  subsetDF <- reactive({ DF <- DF[1:input$range, ]
                         DF
                         })

  output$text <- renderUI({
    str <- paste("This is how many rows you've chosen for your dataframe:",
                 input$range)
    HTML(paste(str, sep = '<br/>'))
                          })

  output$table <- renderTable(subsetDF())

}
)
)
0

There are 0 best solutions below