Search code examples
rshinyshinydashboardflexdashboardshinyjs

R flexdashboard Error in disabling the submit button even if there is no radio button option is selected or when any option is selected


In my flexdashboard code, what I am trying to do is:

  • initially, no radio button is selected, and there are two options only
  • option (radio button) one is to upload the data, option two is to connect to MySQL server for retrieving the data

code looks like this:

```{r setup, include = FALSE}

library(flexdashboard)
library(ggplot2)
library(bslib)
library(shiny)
library(tidyverse)
library(htmltools)
library(fontawesome)
library(shinyjs)
library(RMySQL)
library(shinyWidgets)

useShinyjs()

```

Home 
=====================================

Sidebar {.sidebar data-width=350}
-------------------------------------

```{r}

prettyRadioButtons(
        inputId = "dataChoice",
        label = "Select one:",
        icon = icon("check"),
        choices = c("Upload File", "Data from Database"),
        animation = "tada",
        inline = TRUE,
        status = "default",
        selected = F
      )


tags$div(id = "one", actionButton("submitbtn1", "Submit"))

observeEvent(input$dataChoice, {
  ifelse(is.null(input$dataChoice), shinyjs::disable(id = "one"),
                  ifelse(input$dataChoice=='Upload File',
                         shinyjs::enable(id = "one"),
                         shinyjs::disable(id = "one"))
  )}
})

```

I want to disable the submit button when there is no radio button selected or option Data from Database is selected. But submit button should be enabled when radio button option Upload File is selected.

Somehow the code above is not working and the moment I click on first option, error appears and app stops abruptly. What am I doing wrong here?

I used the conditionalPanel() from below reply, but now another issue is:

conditionalPanel(
  condition = "input.dataChoice == 'Upload File'",  {
  tags$hr(),
  checkboxInput('header', 'Your Data has Header?', TRUE),
  renderText(ifelse(input$header=='TRUE', "Headers present.", "No headers present.")),
  tags$br(),
  prettyRadioButtons(
          inputId = "separator",
          label = "Type:",
          icon = icon("check"),
          choices = c(Comma=',', Semicolon=';'),
          inline = TRUE,
          status = "default"
        ),
            
  .....
  
  actionButton("submit", "Submit")

     }
  )

Only actionButton() is working, while other items are not shown. Not sure if conditionalPanel() has such options?


Solution

  • Use conditionalPanel (https://shiny.rstudio.com/reference/shiny/1.6.0/conditionalpanel)

    Notice the condition syntax. Specifically notice input.dataChoice instead of input$dataChoice

    
    prettyRadioButtons(
      inputId = "dataChoice",
      label = "Select one:",
      icon = icon("check"),
      choices = c("Upload File", "Data from Database"),
      animation = "tada",
      inline = TRUE,
      status = "default",
      selected = F
    )
    
    conditionalPanel(
      condition = "input.dataChoice == 'Upload File'", 
      actionButton("submitbtn1", "Submit")
    )