在Shiny中使用InsertUI时如何获取正确的InputID [英] How to get the correct InputID while using InsertUI in Shiny

查看:80
本文介绍了在Shiny中使用InsertUI时如何获取正确的InputID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a question about InsertUI and the respective InputID of the elements.

在下面的示例中,selectizeInput Number_Product1_1的inputID显示了1的输出。在boxOutput中划分 InputID。

In the example below, the inputID of selectizeInput "Number_Product1_1" shows the output for the 1. Division in the boxOutput "InputID".

如果将此InputID用作boxOutput总计的输入,则不会显示任何输出。

If this InputID is used as input for the boxOutput "Total", no output is displayed.

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

If more Divisions are added, the quantity of Product1 (in the example below '50') of the 1. division is the output in the boxOutput "Total" of the following divisions. But why is this output not shown for the 1. division?

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

I am confused. Can someone explain to me why this shift occurs?

感谢您的输入!

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)


推荐答案

我可能不得不提出

我的理解是,在 insertUI 中,您尝试访问一个id的值只能在 insertUI 之后创建,因此我尝试单独渲染它,并将其输出分配给 box

My understanding is that within insertUI you are trying to access an id whose value would be created only after insertUI hence I tried to render it separately and assigned the output of it to the box value.

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, ignoreInit = TRUE,{

    values$num_questions <- values$num_questions + 1
    num <- values$num_questions
    #ui = tags$div(
   # observe({


      insertUI( immediate = TRUE,
        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,
                         selected = 50),
          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",

            print( input$Number_Product1_1),

            textOutput("total")

          ),   #### Input from selectizeInput "Product 1" 



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

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

  #observe({

   # require(input$Number_Product1_1)
    output$total <- renderText({
      input[["Number_Product1_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)

图片:

这篇关于在Shiny中使用InsertUI时如何获取正确的InputID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆