英文:
How do I use shinjs::extendShinyjs() in a shiny module?
问题
shinyjs::extendShinyjs()
在没有至少一些命名空间的情况下似乎无法在 Shiny 模块中正常工作。
以下是一个使用简单的JS扩展来增加输入值的示例(按预期工作):
library(shiny)
ui <- tagList(
fluidPage(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = "shinyjs.increase_click_count = function(val){
Shiny.setInputValue('count', +val + 1, { priority: 'event' })
}", functions = "increase_click_count"
),
actionButton("increase","Increase"),
numericInput("count", "Count", 0),
textOutput("clicks")
)
)
server <- function(input, output, session) {
observeEvent(input$increase, {
shinyjs::js$increase_click_count(input$count)
})
output$clicks = renderText(input$count)
}
shinyApp(ui, server)
然而,当我将此逻辑包装在模块中时,shinyjs.increase_click_count()
函数不再更新值:
library(shiny)
module_ui <- function(id) {
tagList(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = paste0("shinyjs.increase_click_count = function(val){
Shiny.setInputValue('", NS(id, "count"), "', +val + 1, { priority: 'event' })
}"), functions = "increase_click_count"
),
actionButton("increase","Increase"),
numericInput(NS(id, "count"), "Count", 0),
textOutput(NS(id,"clicks"))
)
}
module_server <- function(id) {
moduleServer(id, function(input, output, session) {
observeEvent(input$increase, {
shinyjs::js$increase_click_count(input$count)
})
output$clicks = renderText(input$count)
})
}
ui <- fluidPage(
module_ui("mod")
)
server <- function(input, output, session) {
module_server("mod")
}
shinyApp(ui, server)
如何正确处理Shiny模块中的JavaScript命名空间?
英文:
shinyjs::extendShinyjs()
does not seem to work in a Shiny module without at least some namespacing.
Here is an example of using a simple JS extension to increase an input value (which works as intended):
library(shiny)
ui <- tagList(
fluidPage(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = "shinyjs.increase_click_count = function(val){
Shiny.setInputValue('count', +val + 1, { priority: 'event' })
}", functions = "increase_click_count"
),
actionButton("increase","Increase"),
numericInput("count", "Count", 0),
textOutput("clicks")
)
)
server <- function(input, output, session) {
observeEvent(input$increase, {
shinyjs::js$increase_click_count(input$count)
})
output$clicks = renderText(input$count)
}
shinyApp(ui, server)
However, when I wrap this logic in a module, the shinyjs.increase_click_count()
function no longer updates the value:
library(shiny)
module_ui <- function(id) {
tagList(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = paste0("shinyjs.increase_click_count = function(val){
Shiny.setInputValue('", NS(id, "count"), "', +val + 1, { priority: 'event' })
}"), functions = "increase_click_count"
),
actionButton("increase","Increase"),
numericInput(NS(id, "count"), "Count", 0),
textOutput(NS(id,"clicks"))
)
}
module_server <- function(id) {
moduleServer(id, function(input, output, session) {
observeEvent(input$increase, {
shinyjs::js$increase_click_count(input$count)
})
output$clicks = renderText(input$count)
})
}
ui <- fluidPage(
module_ui("mod")
)
server <- function(input, output, session) {
module_server("mod")
}
shinyApp(ui, server)
How do I properly deal with Shiny module namespaces in Javascript?
答案1
得分: 1
你缺少了一个NS调用
library(shiny)
module_ui <- function(id) {
tagList(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = paste0("shinyjs.increase_click_count = function(val){
Shiny.setInputValue('', NS(id, 'count'), '', +val + 1, { priority: 'event' })
}"), functions = "increase_click_count"
),
actionButton(NS(id, "increase"), "Increase"),
numericInput(NS(id, "count"), "Count", 0),
textOutput(NS(id,"clicks"))
)
}
module_server <- function(id) {
moduleServer(id, function(input, output, session) {
observeEvent(input$increase, {
shinyjs::js$increase_click_count(input$count)
})
output$clicks = renderText(input$count)
})
}
ui <- fluidPage(
module_ui("mod")
)
server <- function(input, output, session) {
module_server("mod")
}
shinyApp(ui, server)
英文:
You're missing a a NS call
library(shiny)
module_ui <- function(id) {
tagList(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = paste0("shinyjs.increase_click_count = function(val){
Shiny.setInputValue('", NS(id, "count"), "', +val + 1, { priority: 'event' })
}"), functions = "increase_click_count"
),
actionButton(NS(id, "increase"), "Increase"),
numericInput(NS(id, "count"), "Count", 0),
textOutput(NS(id,"clicks"))
)
}
module_server <- function(id) {
moduleServer(id, function(input, output, session) {
observeEvent(input$increase, {
shinyjs::js$increase_click_count(input$count)
})
output$clicks = renderText(input$count)
})
}
ui <- fluidPage(
module_ui("mod")
)
server <- function(input, output, session) {
module_server("mod")
}
shinyApp(ui, server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论