如何在R Shiny中插入单选按钮选择器的代码作为HTML代码?

6rqinv9w  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(133)

我正在寻找一种方法,如何插入代码为R闪亮的单选按钮选择器内的“标签”类型的代码。这里是一个代码的例子。

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
    })
    
  }
))

任何想法和解决方案都是受欢迎的,并会喜欢!

vsdwdz23

vsdwdz231#

若要将单选按钮选择器的代码插入到标记代码中,您可以首先将单选按钮的UI呈现为变量,然后将该变量插入到标记代码中。以下是一种方法:

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
    })

  }
))

此代码首先将单选按钮选择器呈现到uo_range输出,然后将该输出的呈现UI插入到标记代码中。然后,您可以使用rbtn_days_code变量将单选按钮插入到标记代码中。

相关问题