MLavoie
MLavoie

Reputation: 9836

R - Disabled action button conditional to column's name

Below a simplified version of my shiny app. I looked through some of the examples in the shinyjs package and I did not find anything that could help me.

I want to disable the Submit button if one of the data frame uploaded (in my real example) or selected has a specific column name (Col 3 in the example below).

Can this be done with shinyjs?

library(rhandsontable)
library(shiny)
library(shinyjs)

df1 <- data.frame(col1 = rnorm(20),
                  col2 = rep(T, 20))

df2 <- data.frame(col1 = rnorm(20),
                  col2 = rep(F, 20),
                  col3 = rnorm(20))


server <- function(input, output) {

  values = reactiveValues()
  values[["df1"]] <- df1
  values[["df2"]] <- df2


  df <- reactive({
    if (input$df == "df1") {
      df <- values[["df1"]]
    } else {
      df <- values[["df2"]]
    }
    df
  })

  observeEvent(input$Submit, {
  shinyjs::alert("Thank you!")
})


#observe({
 # if (is.null(input$df) || input$df == "df1") {
  #  shinyjs::disable("submit")
  #} else {
   # shinyjs::enable("submit")
  #}
#})


  output$out <- renderRHandsontable({
    hot <- rhandsontable(df())
    hot
  })
}


ui <- fluidPage(
shinyjs::useShinyjs(),

sidebarLayout(sidebarPanel(
  selectInput(
    'df', 'Select data.frame:',
    choices = c('df1', 'df2'),
    selected = 'df1'
  ),
  actionButton("Submit", label = "Submit")
),
mainPanel(rHandsontableOutput("out"))))

shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 811

Answers (1)

Tonio Liebrand
Tonio Liebrand

Reputation: 17689

First, there is a small typo: Notice the capital "S".

shinyjs::disable("Submit")

Edit: To check for "col3" take the following code:

  observe({
    if (is.null(input$df) || sum(colnames(df()) == "col3")) {
      shinyjs::disable("Submit")
    }else{
      shinyjs::enable("Submit")
    }
  })

Same for enable of course.

Upvotes: 1

Related Questions