在 R Shiny 模块中使用 insertUI 和 actionButton 组合
Using insertUI and actionButton Combination in R Shiny Modules
我正在尝试结合使用操作按钮和插入UI 来帮助用户输入 excel 文件。用户单击按钮后,将出现 FileInput 框(稍后我将处理 fileinput$datapath 以创建数据框。)
在下面的代码中,我尝试:
- 在 UI 代码中使用 actionButton 生成一个按钮
- 通过 ObserveEvent 使用 actionButton 触发 InsertUI 代码,一旦单击 1. 中的按钮,理想情况下应该插入代码的 fileInput 部分。我希望文件输入位于代码的服务器部分而不是代码的 UI 部分。
- 我尝试了下面的代码,我可以在其中生成一个按钮,但是无法生成
触发观察事件。非常感谢这里的任何帮助。
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()
您需要使用命名空间:
insertUI(selector=paste0("#", ns("AddExcelDataButton")), ......
我正在尝试结合使用操作按钮和插入UI 来帮助用户输入 excel 文件。用户单击按钮后,将出现 FileInput 框(稍后我将处理 fileinput$datapath 以创建数据框。)
在下面的代码中,我尝试:
- 在 UI 代码中使用 actionButton 生成一个按钮
- 通过 ObserveEvent 使用 actionButton 触发 InsertUI 代码,一旦单击 1. 中的按钮,理想情况下应该插入代码的 fileInput 部分。我希望文件输入位于代码的服务器部分而不是代码的 UI 部分。
- 我尝试了下面的代码,我可以在其中生成一个按钮,但是无法生成 触发观察事件。非常感谢这里的任何帮助。
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()
您需要使用命名空间:
insertUI(selector=paste0("#", ns("AddExcelDataButton")), ......