Search code examples
rshinyshinydashboard

In R Shiny, use textOutput to dynamically populate downloadbutton's label


In R Shiny I am trying to dynamically set a download button's label using reactive renderText and textoutput. It works as expected but the label is always shown in the new line, and hence the button looks wacky next to a regular button as shown here

Backend logic is -

In server.R, an input field's value is used to generate conditional labels

output$mycustomlabel  <- renderText({ if(input$inputtype=="One") return("Download label 1") else return("Download label 2")})

Then in UI.R, that label is used as

downloadButton("download.button.test", textOutput("mycustomlabel"))

Can someone guide why does it display text on new line, and how can I keep it on same line?


Solution

  • If you want to change the button label you probably need to update it with javascript.

    An easier approach could be to have two different buttons and use conditional panels to display one of the buttons:

    ui <- fluidPage(
      radioButtons('inputtype', 'Set label', c('One', 'Two')),
      conditionalPanel(
        'input.inputtype == "One"',
        downloadButton('btn1', 'Download label 1')
      ),
      conditionalPanel(
        'input.inputtype == "Two"',
        downloadButton('btn2', 'Download label 2')
      )
    )
    

    Note that with this approach you do need two observers in the server function.