Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to Disable a shiny app radio button using Shinyjs()

Tags:

r

shiny

shinyjs

I'm trying to disable a shiny app radio button ("Trend") when input$Product !=A && input$month !="All" using the Shinyjs package, but have been unsuccessful.

My ui page is defined as:

ui <- fluidPage(
   shinyjs::useShinyjs(),
   pageWithSidebar(
   titlePanel("Analytics"),

    sidebarPanel(
      selectInput("product",
              label = "Choose a Product",
              choices = list("A", "B", "C", "All")),

      selectInput("month",
              label = "Choose TimeFrame",
              choices = list("June", "July", "August", "September", "All")),

      radioButtons('ptype', label ="Plot Type",
                c(Histogram = 'histogram',
                  Box = 'boxp',
                  Trend = 'trendp')
               )

),

My server side code:

 shinyServer(function (input, output, session) {

  mydata<- read.csv("mydataQ1fy16.csv")

  observe({shinyjs::toggleState("trendp", input$product != "A" && input$month != "All") })    

    getMonthproduct <- reactive( {
      if(input$month != "All" &input$product !="All") {
         plotdata <- mydata %>% filter(Product ==input$product, Monthly ==input$month)} 

       else if (input$month =="All" & input$product =="All") {
         plotdata <- mydata
          }
         else if(input$product == "All") {
           plotdata <- mydata %>% filter(Monthly == input$month)

         }
          else if(input$month == "All") {
            plotdata <- mydata %>% filter(Product == input$product)

           }
     return(plotdata)
     })
like image 988
sgknr Avatar asked Dec 03 '25 07:12

sgknr


1 Answers

This is quite old issue, but I've encountered the same problem today and solved it with shinyjs and basic jQuery. It's a bit hacky solution, but it works for me:

Your solution can look like this:

observe({
    if (input$product != "A" && input$month !="All") {
        shinyjs::disable(selector = "[type=radio][value=trendp]")
        shinyjs::runjs("$('[type=radio][value=trendp]').parent().parent().addClass('disabled').css('opacity', 0.4)")
    }
})

The first line disables the actual button and the second disables the label and gives it "disabled look".

like image 136
user1991825 Avatar answered Dec 04 '25 21:12

user1991825



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!