Home > Mobile >  How insert code for Radio Button selector in R Shiny as HTML code?
How insert code for Radio Button selector in R Shiny as HTML code?

Time:12-05

I am looking a way how to insert code for R Shiny Radio Button selector inside "tags" type code. Here is an example of code.

library(shiny)

runApp(list(
  
  # UI
  ui = fluidPage(mainPanel(uiOutput('tests'))),
  
  # SERVER
  server = function(input, output) {
    
    # Radio button selector
    output$uo_range <- renderUI({
      list_values <- c("all", "365d", "180d", "90d", "30d")
      names(list_values) <- c("All days", "365 days", "180 days", "90 days", "30 days")
      radioButtons("rbtn_days", "Date range", choices = list_values, selected = "30d", inline = TRUE)
    })
    
    # Render UI
    output$tests <- renderUI({
      result <- tags$table(
        tags$tr(tags$td(align="center", tags$h3("Tests UI"))),
        
        # Radio button selector
        # I would like to have in 'rbtn_days_code' code for 'uo_range' component
        tags$tr(tags$td(rbtn_days_code))
      )
      
      # Result
      result
    })
    
  }
))

Any ideas and solutions are welcome and will be Liked!

CodePudding user response:

To insert the code for your radio button selector inside the tags code, you can first render the UI for the radio buttons to a variable, and then insert that variable into the tags code. Here is one way you could do this:

library(shiny)

runApp(list(

  # UI
  ui = fluidPage(mainPanel(uiOutput('tests'))),

  # SERVER
  server = function(input, output) {

    # Radio button selector
    output$uo_range <- renderUI({
      list_values <- c("all", "365d", "180d", "90d", "30d")
      names(list_values) <- c("All days", "365 days", "180 days", "90 days", "30 days")
      radioButtons("rbtn_days", "Date range", choices = list_values, selected = "30d", inline = TRUE)
    })

    # Render UI
    output$tests <- renderUI({
      
      # Get the rendered UI for the radio button selector and save it to a variable
      rbtn_days_code <- uiOutput('uo_range')
      
      result <- tags$table(
        tags$tr(tags$td(align="center", tags$h3("Tests UI"))),
        
        # Insert the radio button selector code into the tags code
        tags$tr(tags$td(rbtn_days_code))
      )
      
      # Result
      result
    })

  }
))

This code first renders the radio button selector to the uo_range output, and then inserts the rendered UI for that output into the tags code. You can then use the rbtn_days_code variable to insert the radio buttons into the tags code.

  • Related