简体   繁体   中英

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?

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.

I'm doing this same thing with a dynamic label on a downloadButton . In my case, I want the user to choose between downloading a dataframe as an Excel file or a CSV file.

Here's what I'm doing:

In the ui definition, where you want the button to show up, use

uiOutput( 'myCustomButtonUI' )

In the server definition, include:

output$myCustomButtonUI <- renderUI({
  myCustomLabel <- 'Placeholder'
  if( input$inputtype == 'One' ) myCustomLabel <- 'Download Label 1'
  if( input$inputtype == 'Two' ) myCustomLabel <- 'Download Label 2'

  downloadButton( inputId = 'download.button.test',
                  label = myCustomLabel )
})

output$download.button.text <- downloadHandler(
  filename = "<some filename>",
  content = .... <go look up downloadHandler() if you're unfamiliar> ..."
)

The idea is that, because you want your button to be dynamic, it needs to be rendered on the server side. The output of the server side is a tiny piece of UI that is placed in your larger UI by the uiOutput function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM