我如何接收在 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.js
的 server.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/