简体   繁体   English

在闪亮的R中使用观察功能

[英]Using observe function in shiny R

I am trying to send the value of a JavaScript variable from ui.R to Server.R whenever it is changed. 每当尝试更改时,我都试图将JavaScript变量的值从ui.R发送到Server.R When a user clicks on a link, its href value is alerted . 用户单击链接时,将警告其href值。 Till here it works fine. 到这里为止一切正常。 The links are of the form 链接的形式

<a href="something.com" onclick=\"clickFunction(this.href); return false;\" target=\"_blank\">Sample link </a>

Now, the href value is stored in variable link in ui.R . 现在,href值存储在ui.R中的变量link中。 I am sending the value of link to server.R using Shiny.onInputChange function. 我正在使用Shiny.onInputChange函数将链接的值发送到server.R

But the observe function in server.R is not giving me any value. 但是server.R中的观察功能没有给我任何价值。 Please tell me how to do this by using observe function or any other way if possible. 请告诉我如何通过使用观察功能或其他任何可能的方式来执行此操作。

ui.r 用户界面

library(shiny)
shinyUI(fluidPage(

  tags$script(HTML("
                      function clickFunction(link){
                          alert(link); 
                          Shiny.onInputChange(\"linkClicked\",link);   
                      }
                     "))

//rest of the code which is not related

)

server.R 服务器

library(shiny)

shinyServer(function(input, output) {

  observe({
    input$linkClicked
    print(input$linkClicked)
  })
})

I don't really fully understand where the link is coming from and how the app looks since you didn't provide enough code (for example: what does it mean "variable in the UI"?). 由于您没有提供足够的代码(例如:“ UI中的变量”是什么意思?),我不太真正理解链接的来源以及应用程序的外观。 But here's a small example that shows how the javascript sends info to the server successfully. 但是,这是一个小示例,显示了javascript如何将信息成功发送到服务器。 I'm using the shinyjs package which is a package I wrote. 我正在使用Shinyjs包,这是我写的一个包。

library(shinyjs)
jscode <- "
shinyjs.clickfunc = function(link) {
  alert(link);  
  Shiny.onInputChange('linkClicked', link);
}"

runApp(shinyApp(
  ui = fluidPage(
    useShinyjs(),
    extendShinyjs(text = jscode),
    textInput("link", ""),
    actionButton("btn", "submit")
  ),
  server = function(input, output, session) {
    observeEvent(input$btn, {
      js$clickfunc(input$link)
    })
    observe({
      input$linkClicked
      print(input$linkClicked)
    })
  }
))

EDIT : 编辑

If I understand correctly how the links are generated, this works 如果我正确理解链接的生成方式,则可以正常工作

runApp(shinyApp(
  ui = fluidPage(
    tags$script(HTML("
                      function clickFunction(link){
                          alert(link); 
                          Shiny.onInputChange('linkClicked',link);   
                      }
                     ")),
    tags$a(href="www.google.com", onclick="clickFunction('rstudio.org'); return false;", "click me")
  ),
  server = function(input, output, session) {
    observe({
      input$linkClicked
      print(input$linkClicked)
    })
  }
))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM