【问题标题】:How to disable a button based on input condition?如何根据输入条件禁用按钮?
【发布时间】:2021-12-21 16:34:13
【问题描述】:

我试图在一个按钮上创建一个条件,所以它只在我的输入为 not empty AND is numeric 时才有效。 我的代码部分起作用,因为当输入输入时按钮以禁用状态启动,但即使输入不是数字(即输入字母将启用按钮)

用户界面

library(shinyjs)

    ui <-    tabItem(tabName = "principal1",
                        br(),
                        
                        fluidRow( 
                          column(2,
                                 textInput(inputId = "documento1", "Ingrese el numero de documento", "")
                                 ),
                          
                          column(2,
                                 br(),
                                 fluidRow(
                                   actionButton("consulta_gobutton1", 
                                                label =  "Procesar",
                                                icon = icon("clipboard-check") ) )))

服务器端

 observeEvent(input$consulta_gobutton1, {
    documento1 <- input$documento1

    ###HERE IS MY CODE###

    })

 observe({
        toggleState("consulta_gobutton1", input$documento1 != ""  & is.numeric(as.numeric(input$documento1)) )
        
       })

【问题讨论】:

    标签: r shiny shinyjs


    【解决方案1】:

    您可以使用numericInput 并执行此操作

    library(shinyjs)
    
    ui <-    fluidPage(tabItem(tabName = "principal1",
                     br(),
                     useShinyjs(),
                     fluidRow( 
                       column(2,
                              numericInput(inputId="doc1",label="Ingrese el numero de documento ",value="",min=0,max=15,step=0.1, width = "80px"),
                              #textInput(inputId = "documento1", "Ingrese el numero de documento", "")
                       ),
                       
                       column(2,
                              br(),
                              fluidRow(
                                actionButton("consulta_gobutton1", 
                                             label =  "Procesar",
                                             icon = icon("clipboard-check") ) ))
                       )
    ))
    
    server <- function(input, output,session) {
      observeEvent(input$consulta_gobutton1, {
        documento1 <- input$documento1
        
        ###HERE IS MY CODE###
        
      })
      
      observe({
        
        if (is.numeric(input$doc1)) shinyjs::enable("consulta_gobutton1")
        else shinyjs::disable("consulta_gobutton1")
        
        #toggleState("consulta_gobutton1", input$documento1 != ""  & is.numeric(as.numeric(input$documento1)) )
        
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 我无法让它以这种方式工作。即使输入数字,按钮也会保持禁用状态
    • 它对我来说很好用。我会放图片。此外,您可以将最大值更改为 15 以上,无论您需要什么有效范围。也许您需要重新启动笔记本电脑或重新启动 RStudio。
    • 谢谢。我没有数字输入字段,所以影响了按钮状态
    【解决方案2】:

    由于输入是一个字符串,首先需要以不同的方式处理它

      observe({
        toggleState("consulta_gobutton1", input$documento1 != ""  & !is.na(as.numeric(input$documento1)) )
    
        #if(is.numeric(as.numeric(nn)) & !is.na(as.numeric(nn))
    
      })
    

    【讨论】:

      猜你喜欢
      • 2021-01-06
      • 1970-01-01
      • 2019-01-13
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      相关资源
      最近更新 更多