在Shiny中使用InsertUI时如何获取正确的InputID

时间:2019-01-24 17:11:38

标签: r shiny conditional

我有一个关于InsertUI以及元素的相应InputID的问题。

在下面的示例中,selectizeInput“ Number_Product1_1”的inputID显示1的输出。

如果此InputID用作boxOutput“ Total”的输入,则不会显示任何输出。

如果添加了更多的分区,则1分区的Product1数量(在下面的示例中为“ 50”)是以下分区的输出“总计”框中的输出。但是为什么没有为1.部门显示此输出?

我很困惑。有人可以向我解释为什么会发生这种转变吗?

感谢您的输入!

library(shiny)
library(shinydashboard)

# Define UI
ui <- fluidPage(
titlePanel("Identify Total amount/Divison"),
sidebarLayout(
sidebarPanel(
  width = 12,
  # Buttons to add/remove a question
  actionButton("add", "Add Divison"),
  actionButton("remove", "Remove Divison"),
  div(id = "questions",
      style = "border: 1px solid silver;")
),
mainPanel(
)))



# Define server logic 
server <- function(input, output) {
values <- reactiveValues(num_questions = 0) 
# Add a division
observeEvent(input$add, ignoreNULL = FALSE, {

values$num_questions <- values$num_questions + 1
num <- values$num_questions
ui = tags$div(
  insertUI(
    selector = "#questions", where = "beforeEnd",
    splitLayout(
      cellWidths = c("20%","20%", "20%", "20%", "20%"), 
      cellArgs = list(style = "padding: 3px"),
      id = paste0("question", num),
      textAreaInput(inputId = paste0("Division_", num),
                    label = paste0(num, ". Division:"),
                    placeholder = "Placeholder"),

      selectizeInput(inputId =paste0("Number_Product1_", num),
                         label = paste0("Product1"), isolate(seq(from = 50, to = 100000, by = 50)), multiple=FALSE),
      selectizeInput(inputId =paste0("Number_Product2_", num),
                     label = paste0("Product2"), isolate(seq(from = 0, to = 100000, by = 50)), multiple=FALSE),
      box(
        title = "Total", width = 12, background = "black",


        input$Number_Product1_1),   #### Input from selectizeInput "Product 1" 



       box(
        title = "inputID", width = 12, background = "black",

        paste0("Number_Product1_", num))  #### inputID's of the selectizeinput "Product 1"
      )))

})

# Remove a division
observeEvent(input$remove, {
num <- values$num_questions
# Don't let the user remove the very first Row
if (num == 1) {
  return()
}
removeUI(selector = paste0("#question", num))
values$num_questions <- values$num_questions - 1
})

}


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

1 个答案:

答案 0 :(得分:0)

我可能不得不提出更好的解释,同时,该错误已修复。

我的理解是,您在insertUI中尝试访问一个ID,该ID的值仅在insertUI之后创建,因此我尝试单独渲染它,并将其输出分配给{{1 }}值。

box

图片:

enter image description here