简体   繁体   English

闪亮-将JavaScript变量存储到mysql数据库

[英]Shiny - store javascript variable to mysql database

I am working with shiny server. 我正在使用闪亮的服务器。
My application has a search box input. 我的应用程序有一个搜索框输入。 Based on that input, the output is a dataTable which has clickable links . 基于该输入,输出为具有可点击链接dataTable
My application's ui.r contains a JavaScript function that sets the value of a variable whenever a link is clicked. 我的应用程序的ui.r包含一个JavaScript函数,该函数可在每次单击链接时设置变量的值。 Let the variable be clickedLink . 让变量为clickedLink Now I want to store this value of link to mysql or any other database. 现在,我想将此链接的值存储到mysql或任何其他数据库。 How to go about this? 怎么办呢?

I have tried ajax , php with no use. 我已经尝试过ajax,php没用。 What I did is described in this question: saving json data to json file using ajax PHP but I guess php files do not work with shiny. 这个问题描述了我所做的事情: 使用ajax PHP将json数据保存到json文件中,但是我认为php文件不能正常工作。 Please help. 请帮忙。

EDIT 1 编辑1

code added to ui.R 添加到ui.R中的代码

  tags$script(HTML("
     function clickFunction(clickedLink){
        //alert(clickedLink);
        var cl = clickedLink;
        Shiny.onInputChange('clickedLink',cl);
     }
  "))

code added to server.R 代码添加到server.R

observe({
    print(input$clickedLink)

  })

EDIT 2 编辑2
Just for information , the links are of the form 仅供参考,链接的格式如下

<a onclick="clickFunction(this.href); " target="_blank" href="http://SOMETING.com">SOMETHING</a>

I'm assuming you know how to save the variable once you get it in R. If not, use the RMySQL package and read their tutorial - https://github.com/rstats-db/RMySQL 我假设您知道一旦在R中获取变量,便会保存该变量。如果没有,请使用RMySQL包并阅读其教程-https: //github.com/rstats-db/RMySQL

So your main problem is getting the javascript variable into R. For that you can use Shiny.onInputChange. 因此,您的主要问题是将javascript变量放入R。为此,您可以使用Shiny.onInputChange。 There's a short easy tutorial here https://ryouready.wordpress.com/2013/11/20/sending-data-from-client-to-server-and-back-using-shiny/ 这里有一个简短的简单教程https://ryouready.wordpress.com/2013/11/20/sending-data-from-client-to-server-and-back-using-shiny/

Basically, in javascript you would have Shiny.onInputChange("jsvar", clickedLink) and then you can treat jsvar as a regular reactive input, so in R you can do `observe({ saveToDb(input$jsvar) }) (you'd have to define saveToDb) 基本上,在javascript中,您将拥有Shiny.onInputChange("jsvar", clickedLink) ,然后可以将jsvar视为常规的反应式输入,因此在R中,您可以执行`observe({saveToDb(input $ jsvar)})(您d必须定义saveToDb)

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

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