从server.R中的.js接收数据 [英] Receiving data from .js in server.R shiny

查看:79
本文介绍了从server.R中的.js接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何接收在 server.R 中的 .js 文件中创建的数据? / p>

我正在使用leaflat库,我需要扩展当前地图视图的LatLngBounds。我需要在 server.R 中使用此变量进行更快速的处理。



所以我有

  mycode.js 

//获取视图扩展的边界
$(document).ready(function( ){

var myBounds = map.getBounds();
Shiny.onInputChange(bounds,myBounds);



});

我包含在 ui.R 喜欢

 标签$ body(标签$ script(src =mycode.js))

这是我的 Server.R 的样子:

  myBoundsR<  - 被动(
as.numeric(输入$ bounds)
print(输入$ bounds)

但是,我如何收到 server.R中的数据文件来自我的 mycode.js



感觉就像 Shiny.addCustomMessageHandler 仅接收 .js (或 .R )中的数据而 session $ sendCustomMessage 只能在 .R 文件中使用?我将使用什么从 .js 文件发送到 server.R 文件?!



或者我可以简单地使用变量 bound ,就像我在 server.R中创建它一样 file?!

解决方案

关于NicE所写的内容:



file.js

  var myVariable = //做一些事情
Shiny.onInputChange(variableNameToWriteInServer.R,myVariable)

server.R

  observe({
#thats how you you访问变量
输入$ myVariable
#do与变量一起使用。看看它是否像
一样工作})

这应该可以解决问题。如果您使用 getBounds()函数,请记住 Leaflet:在边界发生变化时触发事件。此外,您可能没有收到任何输出,因为 getBounds()函数在窗口扩展未更改时不返回窗口的边界。但是,它只执行一次。在我链接的文章中是一个解决方案。



对于Shiny(server.R和ui.R)中的进一步未记录的通信,请在此处阅读: https://ryouready.wordpress .com / 2013/11/20 / send-data-from-client-to-server-and-back-using-shiny /


How would I receive data, created in a .js file in the server.R in shiny?

I am using the leaflat library and I need the extend the LatLngBounds of the current map view has. I need this variable in the server.R for furhter processing.

So i have

 mycode.js

//get bounds of extend of view
$(document).ready(function() { 

var myBounds = map.getBounds();
Shiny.onInputChange("bounds", myBounds);



});

which I include in the ui.R like

tags$body(tags$script(src="mycode.js"))

Thats how my Server.R looks like:

  myBoundsR <- reactive(
  as.numeric(input$bounds)
  print(input$bounds)
  )

However, how would I receive the data in the server.R file, coming from my mycode.js?

It feels like Shiny.addCustomMessageHandler is only to receive data in .js (or .R) while session$sendCustomMessage can only be used in the .R files? What would I use to send something from a .js file to the server.R file?!

Or can I simply use the variable bound as if I created it in the server.R file?!

解决方案

Regarding what NicE wrote:

In the file.js

var myVariable = //do something
Shiny.onInputChange("variableNameToWriteInServer.R", myVariable)

In the server.R

observe({
# thats how you access the variable
    input$myVariable
#do something with the variable. To see if it worked something like
})

That should do the trick. If you use the getBounds() function keep that in mind Leaflet : Firing an event when bounds change. Also, you might not receive any output since the getBounds() function does not return the bounds of the window when the window extend is not changed. But, its only execute once. In the article I linked is a solution for this.

For further undocumented communication in Shiny (server.R and ui.R) read here: https://ryouready.wordpress.com/2013/11/20/sending-data-from-client-to-server-and-back-using-shiny/

这篇关于从server.R中的.js接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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