gpt4 book ai didi

javascript - visnetwork 节点标题内的 Shiny 操作按钮

转载 作者:行者123 更新时间:2023-11-28 17:06:58 24 4
gpt4 key购买 nike

我正在尝试在 visNetwork 节点中嵌入一个操作按钮,以便可以通过单击工具提示中的按钮来启动操作。

我可以让按钮出现在节点标签中,但单击它时不会触发任何事件。我哪里出错了?

最小示例:

library(shiny)  
library(visNetwork)

ui <- fluidPage(
visNetworkOutput("net")
)

server <- function(input, output) {

## minimal nodes and edges example
nodes <- data.frame(id = 1, title = HTML("<button id='test' type='button' class='btn btn-default action-button'>test</button>"))
edges <- data.frame(from = c(1,1))

## render the single-node network
output$net = renderVisNetwork(visNetwork(nodes, edges))

## detect when the actionbutton is clicked
observeEvent(input$test, {
print("clicked")
})
}


shinyApp(ui,server)

最佳答案

只需向按钮添加一个 onclick 事件即可。您可以在此处触发 JavaScript 并使用 Shiny.oninputchange() 根据需要创建 input$test。由于仅当您发送的值发生更改时才会触发 input$test ,因此您应该使用 Math.random() 来生成(变化的)随机值。

可重现的示例:

 library(shiny)  
library(visNetwork)

ui <- fluidPage(
visNetworkOutput("net")
)

server <- function(input, output) {

## minimal nodes and edges example
nodes <- data.frame(id = 1, title = HTML("<button id='test' type='button'
class='btn btn-default action-button' onclick ='Shiny.onInputChange(\"test\",
Math.random());'>test</button>"))
edges <- data.frame(from = c(1,1))

## render the single-node network
output$net = renderVisNetwork(visNetwork(nodes, edges))

## detect when the actionbutton is clicked
observeEvent(input$test, {
print("clicked")
})
}

shinyApp(ui,server)

注意:

我不确定您的总体目标是什么,并且此注释可能是不必要的,而只是一个指针,您还可以将 onclick 事件绑定(bind)到操作按钮之外的其他对象。 (也只是到圆圈而不是使用弹出窗口)。

关于javascript - visnetwork 节点标题内的 Shiny 操作按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55634400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com