Disabled input tags

600 views
Skip to first unread message

Robert Bruggner

unread,
Jan 8, 2013, 10:23:01 PM1/8/13
to shiny-...@googlegroups.com
Hi all, 

I would like to use the html-input-generating functions (i.e. selectInput or radioButtons) but have those generated inputs be disabled. In HTML, I expect that I would just add the disabled="disabled" attribute to the input tag. Is there any easy way to pass the disabled argument to the shiny html-input-generating functions?

Cheers,

-Rob

Joe Cheng

unread,
Jan 8, 2013, 10:25:09 PM1/8/13
to shiny-...@googlegroups.com
Interesting--can you talk more about this case? When and how do they then become enabled?



--
 
 

Robert Bruggner

unread,
Jan 8, 2013, 10:31:21 PM1/8/13
to shiny-...@googlegroups.com
I am generating a multi-paneled UI for an algorithm where in one circumstance, a user may specify algorithm parameters using the actual UI. In another use case, the user provides a config file that is parsed before the UI is launched. I would like to retain the "same-looking" UI, set the values in the UI according to the values in the configuration file, but not allow the user to change them. 

This would enable the downstream execution functionality to be identical regardless of whether or not the runtime parameters were set manually by the user or by the config file as it would always look to the UI inputs (regardless of whether or not they were disabled). 

-Rob

--
 
 

Joe Cheng

unread,
Jan 8, 2013, 11:02:21 PM1/8/13
to shiny-...@googlegroups.com
OK, interesting.

The input controls don't currently support that. Fortunately the HTML generating functions return well-structured lists of tags that are easy to munge. For example:

disable <- function(x) {
  if (inherits(x, 'shiny.tag')) {
    if (x$name %in% c('input', 'select'))
      x$attribs$disabled <- 'disabled'
    x$children <- disable(x$children)
  }
  else if (is.list(x) && length(x) > 0) {
    for (i in 1:length(x))
      x[[i]] <- disable(x[[i]])
  }
  x
}

So if you include this function definition at the top of ui.R then you could do e.g.:

sidebarPanel(
  disable(selectInput(...)),
  disable(radioButtons(...))
)

or whatever.


--
 
 

Robert Bruggner

unread,
Jan 9, 2013, 12:12:24 AM1/9/13
to shiny-...@googlegroups.com
Thanks Joe! That worked perfectly - I really appreciate the help.

-Rob

--
 
 

Cyndy Hart

unread,
Jan 15, 2014, 5:52:57 PM1/15/14
to shiny-...@googlegroups.com, rbru...@gmail.com
Hi folks  -

I want to do something very similar to the below, as Robert did.  In my case, I want my app to have several control buttons, all in the sidebar of the app,  that are disabled until certain things happen in various tabs of the rest of a multi-tab app.  (I would prefer myself to place the controls of interest IN the tabs they operate with, but I've been requested to keep all controls in the sidebar.  Not my choice!).    I can see from what Joe has written how to go about disabling (greying out, I imagine?) the control buttons, but I am far less clear on how to ENable them at will when I need to do so.  Help?

Jie Song

unread,
Dec 16, 2014, 4:47:13 PM12/16/14
to shiny-...@googlegroups.com
Hi Joe
I tried your function to disable my input tags, it works well.
But your this function can only be used to disable the input tags, I tried to alter it to lock the input tags that the user cannot change the input once he locks it.
I just add the condition argument so that when the user click certain actionbutton, the parameter he chose will be locked and cannot be changed, the chose parameter will be used in the following data analysis.

disable <- function(x,condition=T) {
if (condition==T){

  if (inherits(x, 'shiny.tag')) {
    if (x$name %in% c('input', 'select'))
      x$attribs$disabled <- 'disabled'
    x$children <- disable(x$children)
  }
  else if (is.list(x) && length(x) > 0) {
    for (i in 1:length(x))
      x[[i]] <- disable(x[[i]])
  }
}
  x
}

My problem is when the input tag is disabled , the default parameter in the input tag will be used, not the one that the user chose.
I don't know if you can understand what I mean, I have a small example below.
If the user choose the the parameter "b" and click the button "lock the parameter", the selected parameter will be refreshed to "a".
I don't know how I should update the function that the chosen parameter can be fixed when disable, can you help me out?
Thanks a lot.

##server.R
library(shiny)
 
shinyServer(function(input, output,session) {
observe({
output$btn2 <- renderUI ({
  disable(selectInput ("btn2", "choose parameter",c("a","b")),condition=isTRUE((input$btn1!=0)))
})
}) 
})

## ui.R
library(shiny)
shinyUI(basicPage(
actionButton("btn1","Lock the parameter"),
  uiOutput('btn2')
)
)

Dean Attali

unread,
Apr 24, 2015, 7:24:05 PM4/24/15
to shiny-...@googlegroups.com
Hi Jie,

Joe's solution is really nice and elegant and will work on any input tag that is generated using plain HTML. If you use a normal select tag without selectize (`selectInput(selectize = FALSE)`), his solution will probably work.  The reason disabling selectize inputs and some other inputs doesn't just work is because they use a special javascript library to construct the tag instead of plain HTML.

Disclaimer: self-promotion: you can use my new package shinyjs (https://github.com/daattali/shinyjs) to do this, it is meant to help shiny app developers do these kinds of small tasks with normal R code instead of fiddling with javsacript
Reply all
Reply to author
Forward
0 new messages