1

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.
My application's ui.r contains a JavaScript function that sets the value of a variable whenever a link is clicked. Let the variable be clickedLink. Now I want to store this value of link to mysql or any other database. How to go about this?

I have tried ajax , php with no use. 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. Please help.

EDIT 1

code added to ui.R

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

code added to server.R

observe({
    print(input$clickedLink)

  })

EDIT 2
Just for information , the links are of the form

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

1 Answer 1

0

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

So your main problem is getting the javascript variable into R. For that you can use 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/

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)

Sign up to request clarification or add additional context in comments.

4 Comments

see EDIT 1. @daattali
right, that's exactly what i suggested, looks like you're almost there. Now you just need to use RMySQL to talk to the database
But it is not returning anything. NULL only. See details, I added something.
@alloyeduniv Please prove it by a minimal reproducible example when you say anything is not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.