Possible to pass parameters to shiny from website that loads shiny apps in iframes?

951 views
Skip to first unread message

Jan Stanstrup

unread,
May 11, 2014, 11:03:44 AM5/11/14
to shiny-...@googlegroups.com
Hi,

I have been looking around for a way to set up website with a number of functions using R as a backend and found Shiny to be the most obvious choice.

What I need is to set up a website (I imagine using Wordpress or Joomla and load Shiny apps in iframes) for an academic project where users should be able to:
  • Login (Wordpress/Joomla)
  • Upload own data to a database (my plan was to do: shiny --> mongodb)
  • Remove/change own data (shiny <--> mongodb)
  • Use the data of all users to make certain models/calculations (shiny/mongodb)
  • Download results (shiny/mongodb)

The problem is that my shiny apps would need to know which user is adding data etc so that the user can control their own data. I understand that shiny server pro is probably able to do this but the price tag is way too big for this project.

Is there a way I can achieve what I need? Any hints would be appreciated.


- Jan.

Jan Stanstrup

unread,
May 14, 2014, 4:26:02 AM5/14/14
to shiny-...@googlegroups.com

Ok I have gotten quite far with this now but cannot get the info into shiny.


What works:

I add the following to the wordpress page that is supposed to load shiny:
<?php global $current_user; 
get_currentuserinfo(); 
$user_name = $current_user->user_login; 
$user_ID = get_current_user_id(); 
?>
<script type="text/javascript"> 
var username = <?php echo json_encode($user_name) ?> ; 
var userID = <?php echo json_encode($user_ID) ?> ; 
</script>


I can make an iframe manually to load the data:
<script> 
document.domain = “10.0.100.99″; 
var username = parent.username; 
var userID = parent.userID; 

document.write(‘Your username is ‘+username+’ and user id is ‘+userID+’.’); 
</script>

And load it with the following on the wordpress page:

<script type="text/javascript"> 
document.domain = "10.0.100.99"; 
</script> 
<iframe id="example1" style="border: none; width: 100%; height: 500px;" src="http://10.0.100.99:3838/shiny.tests/App-7/test.html" width="300" height="150" frameborder="0"></iframe>

Note that document.domain is needed both in the wordpress page and the iframe because they are on different domains (different ports are apparently enough). 


What does not work:

I tried to hack together the code based on the guide here: http://www.r-bloggers.com/sending-data-from-client-to-server-and-back-using-shiny/

ui.R: http://pastebin.com/j3uMNPmb
server.R: http://pastebin.com/UkBY8YMF
java: http://pastebin.com/e6z1Bxht

2 problems:
1) as soon as I add the 3 lines of code to the javascript I need to the copy/pasted example shiny doesn't load anything. I just get a blank page. Even if I don't use the variables in the next piece of code.
2) Even if it worked this would still require a click on the div block to get any data. I need it to pass the variable userID on startup. Is there a way to do this?


Any hints are highly appreciated. 

Jan Stanstrup

unread,
May 15, 2014, 7:48:54 AM5/15/14
to shiny-...@googlegroups.com
I solved 1). it was a case of “ in place of ".

However, I still don't know how to get the variable without clicking the div. There must be something simple I am missing?


$(document).ready(function() {
  document.getElementById("mydiv").onclick = function() {
  document.domain = "10.0.100.99"; 
  var username = parent.username; 
  var userID = parent.userID; 
    Shiny.onInputChange("mydata", userID);
  };
});




Jan Stanstrup

unread,
May 18, 2014, 10:58:27 AM5/18/14
to shiny-...@googlegroups.com

Clément Tisseuil

unread,
Sep 24, 2016, 7:44:34 AM9/24/16
to Shiny - Web Framework for R
Hi Jan,

I am trying to apply your exemple fully described on the 'Stack Overflow' website . However I do not manage to make it work correctly. Shiny does not print any information related to the logged in user on the wordpress site.

Trying to solve this issue, I just wonder if your code is still up to date. Or if the problem might come from my Wordpress website (I am still developing it locally on my laptop for the moment) or from any other potential sources or errors.

Thanks you for confirming if your example is still working.

Kind regards,

Clément

Jan Stanstrup

unread,
Sep 24, 2016, 9:25:48 AM9/24/16
to Shiny - Web Framework for R
It works for me. In chrome right-click on the page --> inspect --> console. Check for errors.

You might try to figure it stepwise where it goes wrong.
You can start with a script on a wordpress page that should write out the username and id:

<?php global $current_user;
      get_currentuserinfo();
$user_name = $current_user->user_login;

$user_ID = get_current_user_id();
?>


<script type="text/javascript">
   var username = <?php echo json_encode($user_name) ?>;
   var userID =   <?php echo json_encode($user_ID)   ?>;
   document.write('Your username is '+username+' and user id is '+userID+'.');
</script>



Reply all
Reply to author
Forward
0 new messages