我试图在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)发布于 2019-04-24 13:39:43
只需向按钮添加一个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事件绑定到操作按钮以外的其他对象。(也只是圆圈而不是使用弹出窗口)。
https://stackoverflow.com/questions/55634400
复制相似问题