如何在模块化的应用程序中插入RTUI?

如何在模块化的应用程序中插入RTUI?,r,shiny,R,Shiny,我正在尝试构建一个应用程序,用户可以通过单击按钮添加一个UI(在本例中是一个输入文本字段)。由于这只是一个更大的应用程序的一部分,我希望在我的项目中使用模块来保持结构 但是,单击“操作”按钮后,我的文本字段不会显示。我正在使用最近由shinny引入的新函数moduleServer() 这是一个雷普雷克斯 library(shiny) ModularizedUI <- function(id) { ns <- NS(id) fluidPage( # Input:

我正在尝试构建一个应用程序,用户可以通过单击按钮添加一个UI(在本例中是一个输入文本字段)。由于这只是一个更大的应用程序的一部分,我希望在我的项目中使用模块来保持结构

但是,单击“操作”按钮后,我的文本字段不会显示。我正在使用最近由
shinny
引入的新函数
moduleServer()

这是一个雷普雷克斯

library(shiny)

ModularizedUI <- function(id) {
  ns <- NS(id)
  
  fluidPage(
    # Input: Action button to add text field
    actionButton(inputId = ns("add_text"),
                 label = "Add text field"),
  )
}

ModularizedServer <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      
      ns <- session$ns
      
      # Track the number of input boxes to render for test training
      counter_text <- reactiveVal(0)
      
      # Input/Output fields for start dates test training
      observeEvent(input$add_text, {
        
        counter_text(counter_text() + 1)
        
        # Add UI if this button is clicked
        insertUI(
          selector = "#add",
          where = "afterEnd",
          ui = textInput(inputId = ns(paste0("textfield_", counter_text())), 
                         label = "New text")
        )
      })
    }
  )
}

ui <- navbarPage("Dashboard",
                 tabPanel("Text fields",
                          ModularizedUI(id = "Text_Fields")
                 )
                 
)

server <- function(input, output, session) {
  ModularizedServer("Text_Fields")
}

shinyApp(ui = ui, server = server)
库(闪亮)

moduledui您提供了
insertUI
选择器
#add
,它定义了新元素必须插入的位置。但是在您的UI中没有id为
#add
的元素

操作按钮之后添加
div(id='#Add')
,它应该可以工作

(然后我会使用
where=beforeed
,这样所有输入都在新的
div
中,并且可以使用CSS或JS轻松定位,或者……)

完整示例:

library(shiny)

ModularizedUI <- function(id) {
  ns <- NS(id)
  
  fluidPage(
    # Input: Action button to add text field
    actionButton(inputId = ns("add_text"),
                 label = "Add text field"),
    div(id = "add")
  )
}

ModularizedServer <- function(id) {
  moduleServer(id, function(input, output, session) {
      ns <- session$ns
      
      # Track the number of input boxes to render for test training
      counter_text <- reactiveVal(0)
      
      # Input/Output fields for start dates test training
      observeEvent(input$add_text, {
        counter_text(counter_text() + 1)
        
        # Add UI if this button is clicked
        insertUI(
          selector = "#add",
          where = "beforeEnd",
          ui = textInput(inputId = ns(paste0("textfield_", counter_text())), 
                         label = "New text")
        )
      })
    }
  )
}

ui <- navbarPage("Dashboard",
                 tabPanel("Text fields",
                          ModularizedUI(id = "Text_Fields")))

server <- function(input, output, session) {
  ModularizedServer("Text_Fields")
}

shinyApp(ui = ui, server = server)
库(闪亮)

moduledui
Id='add'
不带散列。谢谢!然而,我面临着下一个问题:我生成的
inputId
似乎没有定义。因此,UI可以工作,但我无法为其分配任何服务器逻辑。有什么想法吗?一般来说:新问题->新问题。当我在模块服务器内部使用observer
observeEvent(输入$add_text,{print(输入$textfield_1)})
时,它工作正常。