【发布时间】:2020-10-20 09:48:59
【问题描述】:
这是我第一次使用 Shiny Modules,但在让内部服务器模块正常工作时遇到了一些问题。
本质上,在外部 UI 中,用户可以单击一个操作按钮,从而通过内部 UI 模块将一堆 UI 输入插入到 UI 中(可以多次创建)
但是,我希望内部模块中创建的四个输入中的两个对其他两个具有反应性,因此是内部服务器代码。然而,尽管是相同的命名空间,内部模块中的 observeEvents 似乎并没有触发
#UI elements
specificTransactionOuterUI<-function(id,data){
useShinyjs()
ns <- NS(id)
tagList(
actionButton(inputId=ns("createSpecificFlow"), "Add New Specific Transaction Column"),
uiOutput(ns("specificTransactionUI"))
)
}
#####sever code inner UI
specificTransactionInnerUiTemplate<-function(id, data){
useShinyjs()
ns=NS(id)
div(id =ns("specifcTransactionInnerUiDiv"),
fluidRow(
column(4,
textInput("newColSpecificTransaction", "Give new column a name", value = ""),
br(),
pickerInput( inputId=ns("creditLevelSelector"),
label = "Select level",
choices=colnames(data),
selected = NULL,
multiple = FALSE
),
br(),
pickerInput( inputId=ns("debitLevelSelector"),
label = "Select Level",
choices= colnames(data),
selected = NULL,
multiple = FALSE
)
),
column(4,
br(),
br(),br(),
br(),
pickerInput( inputId=ns("creditValues"),
label = "Select credit side",
choices=NULL,
selected = NULL,
multiple = TRUE,
options = pickerOptions(
actionsBox = TRUE,
selectedTextFormat = "count",
liveSearch = TRUE
)
),
br(),
pickerInput( inputId=ns("debitValues"),
label = "Select debit side",
choices=NULL,
selected = NULL,
multiple = TRUE,
options = pickerOptions(
actionsBox = TRUE,
selectedTextFormat = "count",
liveSearch = TRUE
)
)
),
column(4,
br(),br(),
br(),br(),br(),br(),
actionButton( inputId=ns("RemoveSpecificTransaction"), "Remove Specific Flow Column")
)
)
)
}
#updates
specificTransactionInnerServer<-function(id,data){
moduleServer(
id,
function(input, output, session) {
ns <- session$ns
#
observeEvent(input$creditLevelSelector,{
updatePickerInput(
session,
inputId="creditValues",
choices = unique(data[[input$creditLevelSelector]])
)
})
#updateValuesDebits
observeEvent(input$debitLevelSelector,{
updatePickerInput(
session,
inputId="debitValues",
choices = unique(data[[input$debitLevelSelector]])
)
})
# ###remove button server side
observeEvent(input$RemoveSpecificTransaction, {
removeUI(selector =paste0("#", ns("specifcTransactionInnerUiDiv")))
remove_shiny_inputs(id, input)
# session$specificFlow$removeFlow$destroy()
# session$specificFlow$debitLevel$destroy()
# session$specificFlow$creditLevel$destroy()
})
}
)
}
##########server code - outer UI
specificTransactionOuterServer<- function(id,data){
moduleServer(
id,
function(input, output, session) {
counter<-reactiveValues()
counter$count=0
ns <-session$ns
observeEvent(input$createSpecificFlow, {
counter$count=counter$count+1
insertUI(selector=paste0("#",ns("specificTransactionUI")),where="afterEnd", specificTransactionInnerUiTemplate(id=paste0("specificFlow", counter$count ), data) )
specificTransactionInnerServer(id=paste0("specificFlow", counter$count ), data)
}
)
}
)
}
如果它有助于 input$creditLevelSelector 在内部服务器中评估为 NULL。
但是它应该是数据的列名,因为这是它显示的内容。
【问题讨论】:
-
如果没有完整的 MRE,我不能 100% 确定出了什么问题;但我发现了一个错误:在模块的
server中,您使用ns <- session$ns获得命名空间功能,NS(id)不起作用 -
session$ns 和 NS() 之间实际上是否存在实际差异,因为它们似乎都在互联网上的示例中使用?
-
在shiny docs 中@starja 提到了ns(id) ns
-
我更改了服务器端的ns
-
我尝试将 test
标签: r shiny shinymodules