如何将我的JS脚本嵌入Shiny应用程序中,以便识别所有链接?

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

How do I embed my JS script in a shiny app so that all links are recognized?

问题

我想查询所有通过renderUI()集成到HTML中的外部脚本链接。但是我得到一个空的节点列表[]。我应该在哪个地方包含脚本?

我尝试了以下内容:

R Shiny脚本

  1. library(shiny)
  2. ui = navbarPage(
  3. title = "Test",
  4. id = "test",
  5. selected = "One",
  6. footer = tagList(
  7. tags$script(src = "custom.js")),
  8. tabPanel(title = "One",
  9. div("some links", style = "margin-top: 6rem;"),
  10. div(uiOutput(outputId = "test_ui")),
  11. )
  12. )
  13. server = function(input, output, session){
  14. output$test_ui = renderUI({
  15. tagList(
  16. div(class = "link-section",
  17. tags$a("link_one"),
  18. tags$a("link_two"),
  19. tags$a("link_three"))
  20. )
  21. })
  22. }
  23. shinyApp(ui = ui, server = server, options = list(port = 3838, host = '0.0.0.0'))

JS脚本(位于应用程序目录的www文件夹中)

  1. let links = document.querySelectorAll("a.link-section");
  2. console.log(links);

在这里,我已经去掉了HTML中的HTML转义字符,并将脚本和HTML代码的一部分翻译成了中文。

英文:

I would like to query all links with an external script that I integrate into the HTML via a renderUI(). However i get a empty Nodelist []. At which point do i have to include the script?

I tried the following:

R Shiny Script

  1. library(shiny)
  2. ui = navbarPage(
  3. title = "Test",
  4. id = "test",
  5. selected = "One",
  6. footer = tagList(
  7. tags$script(src = "custom.js")),
  8. tabPanel(title = "One",
  9. div("some links", style = "margin-top: 6rem;"),
  10. div(uiOutput(outputId = "test_ui")),
  11. )
  12. )
  13. server = function(input, output, session){
  14. output$test_ui = renderUI({
  15. tagList(
  16. div(class = "link-section",
  17. tags$a("link_one"),
  18. tags$a("link_two"),
  19. tags$a("link_three"))
  20. )
  21. })
  22. }
  23. shinyApp(ui = ui, server = server, options = list(port = 3838, host = '0.0.0.0'))

JS Script (located in www folder of app directory)

  1. let links = document.querySelectorAll("a.link-section");
  2. console.log(links);

答案1

得分: 0

Shiny with shinyjs

js函数的函数名前缀为 shinyjs.,以便被 shinyjs::extendShinyjs()functions 参数识别,并在R中通过 js$MySelection() 调用。

我使用了 shiny::onFlushed(function() { js$MySelection() })

> "onFlushed 注册一个在 Shiny 刷新反应式系统后调用的函数。".

custom.js:

  1. shinyjs.MySelection=function() {
  2. let links = document.querySelectorAll(".link-section a");
  3. console.log(links);
  4. }

app.R:

  1. library(shiny)
  2. library(shinyjs)
  3. ui = fluidPage(
  4. div(
  5. useShinyjs(),
  6. shinyjs::extendShinyjs(
  7. script = "custom.js", # without www
  8. functions = c("MySelection")
  9. ),
  10. navbarPage(
  11. title = "Test",
  12. id = "test",
  13. selected = "One",
  14. tabPanel(title = "One",
  15. div("some links", style = "margin-top: 6rem;"),
  16. div(uiOutput(outputId = "test_ui"))
  17. )
  18. )
  19. )
  20. )
  21. server = function(input, output, session){
  22. output$test_ui = renderUI({
  23. tagList(
  24. div(class = "link-section",
  25. tags$a("link_one"),
  26. tags$a("link_two"),
  27. tags$a("link_three")
  28. ),
  29. )
  30. })
  31. # in case of more delayed events or interactions :
  32. # observeEvent(input$hiddenInput,{
  33. # js$MySelection()
  34. # })
  35. shiny::onFlushed(
  36. function() {
  37. js$MySelection()
  38. }
  39. )
  40. }
  41. shinyApp(ui = ui, server = server)

纯Shiny无shinyjs

custom.js:

  1. $(document ).on("shiny:sessioninitialized", function(event) {
  2. MySelection=function() {
  3. let links = document.querySelectorAll(".link-section a");
  4. console.log(links);
  5. }
  6. })

app.R:

  1. library(shiny)
  2. ui = fluidPage(
  3. div(
  4. tags$head(tags$script(type="text/javascript",src="custom.js")),
  5. navbarPage(
  6. title = "Test",
  7. id = "test",
  8. selected = "One",
  9. tabPanel(title = "One",
  10. div("some links", style = "margin-top: 6rem;"),
  11. div(uiOutput(outputId = "test_ui"))
  12. )
  13. )
  14. )
  15. )
  16. server = function(input, output, session){
  17. output$test_ui = renderUI({
  18. tagList(
  19. div(class = "link-section",
  20. tags$a("link_one"),
  21. tags$a("link_two"),
  22. tags$a("link_three")
  23. ),
  24. tags$script(type="text/javascript", "MySelection()")
  25. )
  26. })
  27. }
  28. shinyApp(ui = ui, server = server)
英文:

Shiny with shinyjs

The function name of js function is prefixed by shinyjs. to be known by functions parameter of shinyjs::extendShinyjs() and called in R by js$MySelection().

I've used shiny::onFlushed(function() { js$MySelection() })

> "onFlushed registers a function that will be called after Shiny flushes the reactive system.".

custom.js:

  1. shinyjs.MySelection=function() {
  2. let links = document.querySelectorAll(".link-section a");
  3. console.log(links);
  4. }

app.R:

  1. library(shiny)
  2. library(shinyjs)
  3. ui =fluidPage(
  4. div(
  5. useShinyjs(),
  6. shinyjs::extendShinyjs(
  7. script = "custom.js", # without www
  8. functions = c("MySelection")
  9. ),
  10. navbarPage(
  11. title = "Test",
  12. id = "test",
  13. selected = "One",
  14. tabPanel(title = "One",
  15. div("some links", style = "margin-top: 6rem;"),
  16. div(uiOutput(outputId = "test_ui"))
  17. )
  18. )
  19. )
  20. )
  21. server = function(input, output, session){
  22. output$test_ui = renderUI({
  23. tagList(
  24. div(class = "link-section",
  25. tags$a("link_one"),
  26. tags$a("link_two"),
  27. tags$a("link_three")
  28. # in case of more delayed events or interactions :
  29. # , shinyjs::hidden( textInput("hiddenInput","hiddenlabel"))
  30. ),
  31. )
  32. })
  33. # in case of more delayed events or interactions :
  34. # observeEvent(input$hiddenInput,{
  35. # js$MySelection()
  36. # })
  37. shiny::onFlushed(
  38. function() {
  39. js$MySelection()
  40. }
  41. )
  42. }
  43. shinyApp(ui = ui, server = server)

Pure Shiny without shinyjs

custom.js:

  1. $(document ).on("shiny:sessioninitialized", function(event) {
  2. MySelection=function() {
  3. let links = document.querySelectorAll(".link-section a");
  4. console.log(links);
  5. }
  6. })

app.R:

  1. library(shiny)
  2. ui =fluidPage(
  3. div(
  4. tags$head(tags$script(type="text/javascript",src="custom.js")),
  5. navbarPage(
  6. title = "Test",
  7. id = "test",
  8. selected = "One",
  9. tabPanel(title = "One",
  10. div("some links", style = "margin-top: 6rem;"),
  11. div(uiOutput(outputId = "test_ui"))
  12. )
  13. )
  14. )
  15. )
  16. server = function(input, output, session){
  17. output$test_ui = renderUI({
  18. tagList(
  19. div(class = "link-section",
  20. tags$a("link_one"),
  21. tags$a("link_two"),
  22. tags$a("link_three")
  23. ),
  24. tags$script(type="text/javascript", "MySelection()")
  25. )
  26. })
  27. }
  28. shinyApp(ui = ui, server = server)

huangapple
  • 本文由 发表于 2023年2月16日 16:44:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469719.html
匿名

发表评论

匿名网友

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

确定