【问题标题】:How to force shiny input to be capitalized如何强制闪亮的输入大写
【发布时间】:2020-11-16 07:32:09
【问题描述】:

我发现了一些将输入文本转换为大写文本的 javascript 函数(here 和 here)。但我不确定如何将 javascript 函数应用于闪亮的输入。见下文,我已经包含了一个示例 selectizeInput 函数。

ui <- fluidPage(theme = "bootstrap.css",
                
                fluidRow(
                    column(6,
                           selectizeInput("input", "Input words here", 
                                          choices = NULL, 
                                          multiple = TRUE, 
                                          options = list(create = TRUE)),
                           column(6,
                                  textOutput("test"))
)
    )
        )

server <- function(input, output) {
    
output$test<-renderText({input$input})
    
}

【问题讨论】:

  • 您想在键入时将文本大写,还是在添加后将outpu$test 大写?
  • 附言。添加后大写可以简单地使用renderText(toupper(input$input))完成。

标签: javascript r shiny


【解决方案1】:

除了TRUE,您可以为create 选项使用JavaScript 函数,它处理输入:

createOption <- "
function(input, callback) {
  var item = input.toUpperCase();
  callback({value: item, label: item});
}
"

ui <- fluidPage(
  theme = "bootstrap.css",
  
  fluidRow(
    column(6,
           selectizeInput(
             "input", "Input words here", 
             choices = NULL, 
             multiple = TRUE, 
             options = list(
               create = I(createOption)
             )
           )
    ),
    column(6,
           textOutput("test")
    )
  )
)

【讨论】:

    【解决方案2】:

    一种方法是观察输入的变化并用大写值更新它:

    library(shiny)
    
    ui <-
        fluidPage(theme = "bootstrap.css",
                  fluidRow(
                    column(6,
                           selectizeInput("input", "Input words here", 
                                          choices = NULL, 
                                          multiple = TRUE, 
                                          options = list(create = TRUE)),
                           column(6,textOutput("test"))
                        ))
    )
    
    server <- function(input, output, session) {
      observeEvent(input$input,{
        updateSelectizeInput(session, "input",
                             choices = tools::toTitleCase(input$input),
                             selected = tools::toTitleCase(input$input))
        })
        
        output$test<-renderText({input$input})
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 2018-07-21
      相关资源
      最近更新 更多