shinysky - how to access textInput.typeahead() value

644 views Asked by At

I could not find much information about this fairly unpopular R package (shinysky), but I am using its auto completion function. I can have the textbox to auto complete and suggest words, but I would like to access the value and have it printed out in verbatimTextOutput.

Here is what I have in server.R:

library(shiny)

my_autocomplete_list <- c("aaaa","bbbb","ccccc", "dddd","eeee")

# ============================================================================================================
# ============================================================================================================

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

  output$dateRangeText  <- renderText({
    paste("input$dateRange is", 
          paste(as.character(input$dateRange), collapse = " to ")
    )
  })

  # output$CodeText <- rederText({
  #   paste("input$code is",
  #         paste(as.character(input$code)))
  # })

  output$PercentText <- renderText({
    paste("input$percent_to_invest is",
          paste(as.character(input$percent_to_invest)))
  })

  output$InvestText <- renderText({
    paste("input$money_to_invest is",
          paste(as.character(input$money_to_invest)))
  })



})

Here is what I have in ui.R:

library(shiny)
library(shinysky)

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: blue;font-size: 20px;font-style: italic;}"),

  sidebarLayout(
    sidebarPanel(
      dateRangeInput('dateRange',
                     label = 'Date range input: yyyy-mm-dd',
                     start = Sys.Date() - 2, end = Sys.Date() + 2
      ),

      textInput.typeahead(id="search",
                          placeholder="Type your name please",
                          local=data.frame(name=c(my_autocomplete_list)),
                          valueKey = "name",
                          tokens=c(1:length(my_autocomplete_list)),
                          template = HTML("<p class='repo-language'>{{info}}</p> <p class='repo-name'>{{name}}</p>")
      ),

      numericInput("percent_to_invest", "Percent", value = 0),

      numericInput("money_to_invest", "Intial Investment ($)", value = 0),

      select2Input("select2Input3",
                   "Multiple Select 2 Input",
                   choices = c("a","b","c"),
                   selected = c("b","a"), 
                   type = "select",
                   multiple=TRUE),

      actionButton("add", "Add+"),

      verbatimTextOutput("dateRangeText"),
      # verbatimTextOutput("CodeText"),
      verbatimTextOutput("PercentText"),
      verbatimTextOutput("InvestText"),

      actionButton("submit", "Submit")
    ),

    mainPanel(
    )
  )
))
1

There are 1 answers

2
MLavoie On BEST ANSWER

Why not simply use this input$search to printed out.

You could try this:

ui <- 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: blue;font-size: 20px;font-style: italic;}"),
  tags$style(HTML("
                  .input {
                  width: 50%;
                  }
                  ")),

  tags$style(HTML("
                  .tt-hint {
                  width: 50%;
                  }
                  ")),

  sidebarLayout(
    sidebarPanel(
      dateRangeInput('dateRange',
                     label = 'Date range input: yyyy-mm-dd',
                     start = Sys.Date() - 2, end = Sys.Date() + 2
      ),

      textInput.typeahead(id="search",
                          placeholder="Type your name please",
                          local=data.frame(name=c(my_autocomplete_list)),
                          valueKey = "name",
                          tokens=c(1:length(my_autocomplete_list)),
                          template = HTML("<p class='repo-language'>{{info}}</p> <p class='repo-name'>{{name}}</p>")
      ),

      numericInput("percent_to_invest", "Percent", value = 0),

      numericInput("money_to_invest", "Intial Investment ($)", value = 0),

      select2Input("select2Input3",
                   "Multiple Select 2 Input",
                   choices = c("a","b","c"),
                   selected = c("b","a"), 
                   type = "select",
                   multiple=TRUE),

      actionButton("add", "Add+"),

      verbatimTextOutput("dateRangeText"),
      # verbatimTextOutput("CodeText"),
      verbatimTextOutput("PercentText"),
      verbatimTextOutput("InvestText"),
      textOutput("testing"),

      actionButton("submit", "Submit")
    ),

    mainPanel(
    )
  )
  )


server <- function(input, output) {

  output$dateRangeText  <- renderText({
    paste("input$dateRange is", 
          paste(as.character(input$dateRange), collapse = " to ")
    )
  })

  # output$CodeText <- rederText({
  #   paste("input$code is",
  #         paste(as.character(input$code)))
  # })

  output$PercentText <- renderText({
    paste("input$percent_to_invest is",
          paste(as.character(input$percent_to_invest)))
  })

  output$InvestText <- renderText({
    paste("input$money_to_invest is",
          paste(as.character(input$money_to_invest)))
  })

  output$testing  <- renderText({
    print(input$search)
  })


}


shinyApp(ui, server)