在闪亮的 R 中使用观察功能 [英] Using observe function in shiny R

查看:29
本文介绍了在闪亮的 R 中使用观察功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 JavaScript 变量的值从 ui.R 发送到 Server.R,只要它发生变化.当用户点击一个链接时,它的 href 值会被提醒.到这里它工作正常.链接的形式为

I am trying to send the value of a JavaScript variable from ui.R to Server.R whenever it is changed. When a user clicks on a link, its href value is alerted . 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>

现在,href 值存储在 ui.R 中的变量 link 中.我正在使用 Shiny.onInputChange 函数将链接的值发送到 server.R.

Now, the href value is stored in variable link in ui.R . I am sending the value of link to server.R using Shiny.onInputChange function.

但是 server.R 中的观察功能没有给我任何价值.如果可能,请告诉我如何使用观察功能或任何其他方式来做到这一点.

But the observe function in server.R is not giving me any value. 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)
  })
})

推荐答案

由于您没有提供足够的代码(例如:它是什么意思是用户界面中的变量"?).但这里有一个小例子,展示了 javascript 如何成功地将信息发送到服务器.我使用的是我写的一个 Shinyjs 包.

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"?). But here's a small example that shows how the javascript sends info to the server successfully. I'm using the shinyjs package which is a package I wrote.

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)
    })
  }
))

编辑:

如果我正确理解了链接是如何生成的,就可以了

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)
    })
  }
))

这篇关于在闪亮的 R 中使用观察功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆