【问题标题】:Using insertUI and actionButton Combination in R Shiny Modules在 R Shiny 模块中使用 insertUI 和 actionButton 组合
【发布时间】:2021-11-11 09:32:22
【问题描述】:

我正在尝试使用 Action Button 和 InsertUI 的组合来帮助用户输入 excel 文件。一旦用户点击按钮,就会出现 FileInput 框(稍后我将处理 fileinput$datapath 以创建数据框。)

在下面的代码中,我尝试:

  1. 在 UI 代码中使用 actionButton 生成按钮
  2. 通过 ObserveEvent 使用 actionButton 来触发 InsertUI 代码,理想情况下,一旦单击 1. 中的按钮,该代码应该插入代码的 fileInput 部分。我希望文件输入位于代码的服务器部分,而不是代码的 UI 部分。
  3. 我尝试了下面的代码,我可以在其中生成一个按钮,但无法生成 触发observeEvent。非常感谢这里的任何帮助。
library(shiny)

Import.Excel.Data.UI <- function(id){
  
   ns <- NS(id)
  
  tagList(
    actionButton(ns("AddExcelDataButton"), label = "Click Here to Add Excel Data"),
  
  )
  
}


Import.Excel.Data.Server <- function(id){
  moduleServer(id, function(input, output, session){
    ns <- session$ns
    observeEvent(eventExpr = input$AddExcelDataButton,
                
                 insertUI(selector="#AddExcelDataButton",
                          where = "afterEnd",
                          ui = fileInput(inputId = paste0("File",input$AddExcelDataButton),
                                         label = paste0("Path for File",input$AddExcelDataButton),
                                         multiple  = FALSE)))
    
  })
}


Import.Excel.Data.App <- function(){
  ui <- fluidPage(
   Import.Excel.Data.UI("File1")
  )
  
  server <- function(input, output, session){
    Import.Excel.Data.Server("File1")
  }
  
  shinyApp(ui, server)
}

Import.Excel.Data.App()

【问题讨论】:

    标签: r shiny server module


    【解决方案1】:

    你需要使用命名空间:

    insertUI(selector=paste0("#", ns("AddExcelDataButton")), ......
    

    【讨论】:

      猜你喜欢
      • 2021-11-06
      • 2022-01-25
      • 1970-01-01
      • 2021-06-28
      • 2020-07-29
      • 2020-04-11
      • 2019-10-17
      • 2021-02-19
      • 2020-07-27
      相关资源
      最近更新 更多