javascript - 从 server.R 中的 .js 接收数据

标签 javascript r shiny shiny-server

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

我正在使用 leaflat 库,我需要扩展当前 map View 的 LatLngBounds。我需要在 server.R 中使用此变量进行进一步处理。

所以我有

 mycode.js

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

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



});

我将其包含在 ui.R 中,例如

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

这就是我的 Server.R 的样子:

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

但是,我如何接收来自 mycode.jsserver.R 文件中的数据?

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

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

最佳答案

关于 NicE 所写的内容:

file.js

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

server.R

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

这应该可以解决问题。如果您使用 getBounds() 函数,请记住这一点 Leaflet : Firing an event when bounds change 。此外,您可能不会收到任何输出,因为当窗口扩展未更改时,getBounds() 函数不会返回窗口的边界。但是,它只执行一次。我链接的文章中提供了一个解决方案。

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

关于javascript - 从 server.R 中的 .js 接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32141128/

相关文章:

javascript - 图像调整大小的 jQuery 事件?

javascript - 在javascript中从json构建树数组

r - 如何减小 Shiny 中的侧边栏面板文本大小?

r - 在大量列的情况下,在 R Shiny DataTable 中设置列​​宽不起作用

javascript - 如何在Nodejs中调用祖 parent 方法

javascript - React JS 从另一个位置导入/需要文件/页面

R 无法使用 hist() 因为 "content not numeric"由于负十进制数?

r - 如何从 R 中的大文件中采样特定比例的行?

r - 用重复次数对数据帧进行分组

javascript - 如果在 Rhandsontable 中编辑了单元格,则突出显示该行并跟踪所有更改