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

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

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

问题

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

我尝试了以下内容:

R Shiny脚本

library(shiny)

ui = navbarPage(
  title = "Test",  
  id = "test", 
  selected = "One", 
  footer = tagList(
    tags$script(src = "custom.js")),
  
  tabPanel(title = "One",
    
           
    div("some links", style = "margin-top: 6rem;"),
    div(uiOutput(outputId = "test_ui")),
    
    
  )
  
)

server = function(input, output, session){
  
  
  output$test_ui  = renderUI({
    
    
    
    tagList(
      div(class = "link-section",
          tags$a("link_one"),
          tags$a("link_two"),
          tags$a("link_three"))
    )
    
  })
  
  
  
}
shinyApp(ui = ui, server = server, options = list(port = 3838, host = '0.0.0.0'))

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

let links = document.querySelectorAll("a.link-section");
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

library(shiny)

ui = navbarPage(
  title = "Test",  
  id = "test", 
  selected = "One", 
  footer = tagList(
    tags$script(src = "custom.js")),
  
  tabPanel(title = "One",
    
           
    div("some links", style = "margin-top: 6rem;"),
    div(uiOutput(outputId = "test_ui")),
    
    
  )
  
)

server = function(input, output, session){
  
  
  output$test_ui  = renderUI({
    
    
    
    tagList(
      div(class = "link-section",
          tags$a("link_one"),
          tags$a("link_two"),
          tags$a("link_three"))
    )
    
  })
  
  
  
  
}
shinyApp(ui = ui, server = server, options = list(port = 3838, host = '0.0.0.0'))

JS Script (located in www folder of app directory)

let links = document.querySelectorAll("a.link-section");
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:

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

app.R:

library(shiny)
library(shinyjs)

ui = fluidPage(
  div(
    useShinyjs(),
    shinyjs::extendShinyjs(
      script = "custom.js", # without www
      functions = c("MySelection")
    ),
    navbarPage(
      title = "Test",  
      id = "test", 
      selected = "One", 
      
      tabPanel(title = "One",
               
               div("some links", style = "margin-top: 6rem;"),
               div(uiOutput(outputId = "test_ui"))
               
      )
      
    )
    
  )
)

server = function(input, output, session){
  
  output$test_ui  = renderUI({
    tagList(
      div(class = "link-section",
          tags$a("link_one"),
          tags$a("link_two"),
          tags$a("link_three")
      ),
    )
    
  })

  # in case of more delayed events or interactions : 
  # observeEvent(input$hiddenInput,{
  #   js$MySelection()  
  # })
  
  shiny::onFlushed(
    function() {
      js$MySelection()  
    }
  )
  
  
}

shinyApp(ui = ui, server = server)

纯Shiny无shinyjs

custom.js:

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

app.R:

library(shiny)
ui = fluidPage(
  div(

    tags$head(tags$script(type="text/javascript",src="custom.js")),
    navbarPage(
      title = "Test",  
      id = "test", 
      selected = "One", 
      
      tabPanel(title = "One",
               
               div("some links", style = "margin-top: 6rem;"),
               div(uiOutput(outputId = "test_ui"))
               
      )
      
    )
    
  )
)

server = function(input, output, session){
  
  output$test_ui  = renderUI({
    tagList(
      div(class = "link-section",
          tags$a("link_one"),
          tags$a("link_two"),
          tags$a("link_three")
      ),
      tags$script(type="text/javascript", "MySelection()")
    )
    
  })

  
}

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:

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

app.R:

library(shiny)
library(shinyjs)


ui =fluidPage(
  div(
    useShinyjs(),
    shinyjs::extendShinyjs(
      script = "custom.js", # without www
      functions = c("MySelection")
    ),
    navbarPage(
      title = "Test",  
      id = "test", 
      selected = "One", 
      
      tabPanel(title = "One",
               
               div("some links", style = "margin-top: 6rem;"),
               div(uiOutput(outputId = "test_ui"))
               
      )
      
    )
    
  )
)

server = function(input, output, session){
  
  output$test_ui  = renderUI({
    tagList(
      div(class = "link-section",
          tags$a("link_one"),
          tags$a("link_two"),
          tags$a("link_three")
          # in case of more delayed events or interactions : 
          # , shinyjs::hidden( textInput("hiddenInput","hiddenlabel"))
      ),
    )
    
  })

  # in case of more delayed events or interactions : 
  # observeEvent(input$hiddenInput,{
  #   js$MySelection()  
  # })
  
  shiny::onFlushed(
    function() {
      js$MySelection()  
    }
  )
  
  
}

shinyApp(ui = ui, server = server)

Pure Shiny without shinyjs

custom.js:

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

app.R:

library(shiny)
ui =fluidPage(
  div(

    tags$head(tags$script(type="text/javascript",src="custom.js")),
    navbarPage(
      title = "Test",  
      id = "test", 
      selected = "One", 
      
      tabPanel(title = "One",
               
               div("some links", style = "margin-top: 6rem;"),
               div(uiOutput(outputId = "test_ui"))
               
      )
      
    )
    
  )
)

server = function(input, output, session){
  
  output$test_ui  = renderUI({
    tagList(
      div(class = "link-section",
          tags$a("link_one"),
          tags$a("link_two"),
          tags$a("link_three")
      ),
      tags$script(type="text/javascript", "MySelection()")
    )
    
  })

  
}

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:

确定