如何在一个shiny模块中使用shinjs::extendShinyjs()?

huangapple go评论79阅读模式
英文:

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 &lt;-  tagList(
  fluidPage(
    shinyjs::useShinyjs(),
    shinyjs::extendShinyjs(text = &quot;shinyjs.increase_click_count = function(val){
        Shiny.setInputValue(&#39;count&#39;, +val + 1, { priority: &#39;event&#39; })
      }&quot;, functions = &quot;increase_click_count&quot;
    ),
    actionButton(&quot;increase&quot;,&quot;Increase&quot;),
    numericInput(&quot;count&quot;, &quot;Count&quot;, 0),
    textOutput(&quot;clicks&quot;)
  )
)


server &lt;- 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 &lt;- function(id) {
  tagList(
    shinyjs::useShinyjs(),
    shinyjs::extendShinyjs(text = paste0(&quot;shinyjs.increase_click_count = function(val){
        Shiny.setInputValue(&#39;&quot;, NS(id, &quot;count&quot;), &quot;&#39;, +val + 1, { priority: &#39;event&#39; })
      }&quot;), functions = &quot;increase_click_count&quot;
    ),
    actionButton(&quot;increase&quot;,&quot;Increase&quot;),
    numericInput(NS(id, &quot;count&quot;), &quot;Count&quot;, 0),
    textOutput(NS(id,&quot;clicks&quot;))
  )
}

module_server &lt;- function(id) {
  moduleServer(id, function(input, output, session) {
    observeEvent(input$increase, {
      shinyjs::js$increase_click_count(input$count)
    })
    
    output$clicks = renderText(input$count)
  })
}

ui &lt;-  fluidPage(
  module_ui(&quot;mod&quot;)
)

server &lt;- function(input, output, session) {
  module_server(&quot;mod&quot;)
}

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 &lt;- function(id) {
  tagList(
    shinyjs::useShinyjs(),
    shinyjs::extendShinyjs(text = paste0(&quot;shinyjs.increase_click_count = function(val){
        Shiny.setInputValue(&#39;&quot;, NS(id, &quot;count&quot;), &quot;&#39;, +val + 1, { priority: &#39;event&#39; })
      }&quot;), functions = &quot;increase_click_count&quot;
    ),
    actionButton(NS(id, &quot;increase&quot;), &quot;Increase&quot;),
    numericInput(NS(id, &quot;count&quot;), &quot;Count&quot;, 0),
    textOutput(NS(id,&quot;clicks&quot;))
  )
}

module_server &lt;- function(id) {
  moduleServer(id, function(input, output, session) {
    observeEvent(input$increase, {
      shinyjs::js$increase_click_count(input$count)
    })
    
    output$clicks = renderText(input$count)
  })
}

ui &lt;-  fluidPage(
  module_ui(&quot;mod&quot;)
)

server &lt;- function(input, output, session) {
  module_server(&quot;mod&quot;)
}

shinyApp(ui, server)

huangapple
  • 本文由 发表于 2023年7月28日 05:08:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定