【问题标题】:shinysky - how to access textInput.typeahead() valueshinysky - 如何访问 textInput.typeahead() 值
【发布时间】:2018-10-13 21:14:31
【问题描述】:

我找不到关于这个相当不受欢迎的 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(
    )
  )
))

【问题讨论】:

  • 你能先修复你的代码吗?它不起作用。
  • @MLavoie 这是完整代码

标签: shiny shinydashboard shinyjs shinysky


【解决方案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)

【讨论】:

  • 嗯,在我发帖之前尝试过,这似乎不起作用,但我想它确实有效!谢谢!!顺便说一句,您是否知道如何使空白框的尺寸变小?我尝试更改百分比但没有成功。
  • @user9532692' 我为您的后续问题编辑了我的回复。如果您能接受我的回答,我将不胜感激。谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-10-03
  • 2021-07-26
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 2020-12-12
  • 2023-03-28
  • 2016-04-18
相关资源
最近更新 更多