RShiny 中的嵌套 uiOutput

Nested uiOutput within RShiny

简而言之,我相信我需要将 uiOutputs 嵌套在一起,但想不出这样做的好方法。

该应用程序很大,但对于这一部分,我想创建一个调查,以根据滑块输入呈现子调查(新面板)(我已经完成了那么多)。这些面板都是标准的,因此可以使用循环创建它们。

但是,这些面板中的答案应该会在生成它们的面板中生成更多 UI,这就是问题所在……uiOutputs 的嵌套。我已尝试在下面提供可能的最短示例,并附有注释 - 请注意,如果我指定了它应该工作的面板(在本例中为 "oh_lawd_1"),则第二个 uiOutput 调用会工作。

请告诉我您的想法!至少 4 天以来,我一直在业余时间看这个。 (我也意识到这不是闪亮的理想用法)。

library(shiny)
library(shinyWidgets)

ui <- fluidPage( #UI

  column(6, offset = 3,
    sliderInput(inputId = "my_slider",     # slider to choose number of panels
                label = "Choose Panels to be Displayed",
                min = 0, max = 5, value = 1),
    uiOutput(outputId = "update_panels")   # ui output to create panels

  )
)

server <- function(input, output, session) { #Server

  output$update_panels <- renderUI({     # rendering all the panels called for by user

    panels <- input$my_slider

    if(panels == 0){
      return("No panels being displayed")# returning 0 if none selected
    } else {
      our_ui <- list()                   # creating a list to store a standard panel
      for(i in 1:panels){
        button_id <- paste("button_id", i, sep = "_") # a unique id for each panel's radiobuttons
        oh_lawd   <- paste("oh_lawd", i, sep = "_")         # a unique id for each panel's uiOutput
        update    <- wellPanel(paste("Well Panel #", i),    # "update" is what each panel should START OFF looking like
                            radioButtons(inputId = button_id, 
                                         label = "Choose a pill", 
                                         choices = c("Red Pill", "Blue Pill")),
                            uiOutput(oh_lawd))     # this part is the issue - I would like to update individual panels with a 
                                                   # radio button selection specific to a choice in each panel... a nested uiOutput
        our_ui <- list(our_ui, update)
      }}
    our_ui})


  output$oh_lawd_1 <- renderUI({     # this works for the first... but I need to somehow create one of these for each based on
                                   # number of panels and the choice in each panel
    if(input$button_id_1 == "Red Pill"){
      radioButtons("first_output", "Next Choices", choices = c("I'm a brave boi", "Knowledge schmoledge"))
    } else {
      radioButtons("first_output", "Next Choices", choices = c("Gimme dat ignorance", "Mhmm yea") )
    }
  })             

}

# Run the application 
shinyApp(ui = ui, server = server)

是你想要的吗?我不确定。

library(shiny)
library(shinyWidgets)

ui <- fluidPage( #UI

  column(6, offset = 3,
         sliderInput(inputId = "my_slider",     # slider to choose number of panels
                     label = "Choose Panels to be Displayed",
                     min = 0, max = 5, value = 1),
         uiOutput(outputId = "update_panels")   # ui output to create panels

  )
)

server <- function(input, output, session) { #Server

  output$update_panels <- renderUI({     # rendering all the panels called for by user

    panels <- input$my_slider

    if(panels == 0){
      return("No panels being displayed")# returning 0 if none selected
    } else {
      our_ui <- list()                   # creating a list to store a standard panel
      for(i in 1:panels){
        button_id <- paste("button_id", i, sep = "_") # a unique id for each panel's radiobuttons
        oh_lawd   <- paste("oh_lawd", i, sep = "_")         # a unique id for each panel's uiOutput
        update    <- wellPanel(paste("Well Panel #", i),    # "update" is what each panel should START OFF looking like
                               radioButtons(inputId = button_id, 
                                            label = "Choose a pill", 
                                            choices = c("Red Pill", "Blue Pill")),
                               uiOutput(oh_lawd))     # this part is the issue - I would like to update individual panels with a 
        # radio button selection specific to a choice in each panel... a nested uiOutput
        our_ui <- list(our_ui, update)
      }}
    our_ui})

  observeEvent(input$my_slider, {
    lapply(seq_len(input$my_slider), function(i){
      uiID <- paste0("oh_lawd_", i)
      buttonID <- paste0("button_id_", i)
      radioID <- paste0("radio_id_", i)
      output[[uiID]] <- renderUI({
        if(input[[buttonID]] == "Red Pill"){
          choices <- c("I'm a brave boi", "Knowledge schmoledge")
        }else{
          choices <- c("Gimme dat ignorance", "Mhmm yea")
        }
        radioButtons(radioID, "Next Choices", choices = choices)
      })
    })
  })

}

# Run the application 
shinyApp(ui = ui, server = server)