shinysky - 如何访问textInput.typeahead()值

时间:2018-05-03 14:05:17

标签: shiny shinydashboard shinyjs shinysky

我找不到关于这个相当不受欢迎的R包(shinysky)的很多信息,但我正在使用它的自动完成功能。我可以让文本框自动完成并建议单词,但我想访问该值并将其打印在verbatimTextOutput中。

以下是 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)))
  })



})

以下是 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 个答案:

答案 0 :(得分:1)

为什么不简单地使用此input$search打印出来。

你可以试试这个:

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)