4

当我在 iframe 中加载闪亮的应用程序时,我想要做的是从 wordpress 登录用户那里获取用户 ID。然后可以使用此变量来保存和修改用户特定的数据。


通过将以下代码添加到 wordpress 页面,我已经获得了一些方法:

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

然后使它成为一个javascript变量:

<script type="text/javascript"> 
var username = <?php echo json_encode($user_name) ?> ; 
var userID = <?php echo json_encode($user_ID) ?> ; 
</script>




使用此处找到的示例,如果单击 div 框,我可以获取变量。

ui.R:

library(shiny)

shinyUI( bootstrapPage(

  # include the js code
  includeScript("get_user_id.js"),

  # a div named mydiv
  tags$div(id="mydiv",
           style="width: 50px; height :50px; left: 100px; top: 100px;
           background-color: gray; position: absolute"),

  # an element for unformatted text
  verbatimTextOutput("results")
))

服务器.R:

shinyServer(function(input, output, session) {
  output$results = renderPrint({
    input$mydata
  })
})

get_user_id.js:

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



请注意,需要域名(或 IP),因为 iframe 中加载的闪亮应用程序位于 wordpress 页面之外的另一个端口上。iframe应由以下人员创建:

<script type="text/javascript"> 
document.domain = "DOMAIN_NAME_HERE"; 
</script> 
<iframe id="example1" style="border: none; width: 100%; height: 500px;" src="APP_URL" height="150" frameborder="0"></iframe>




但是我想要的是在应用程序加载后立即获取变量,以便我可以在应用程序中使用它来将数据保存到数据库等。
我找不到任何方法来做到这一点。我无法使用“.onload”或我尝试过的几种 jquery 替代方案来实现这一点。任何提示将不胜感激。




编辑:也发布在这里:https ://groups.google.com/forum/#!topic/shiny-discuss/3XM2mHuzqRs

4

2 回答 2

3

Erik Westlund 非常友好地提供了以下解决方案。


get_user_id.js:

document.domain = "MYDOMAIN.com"; 

var userIDVariableName = parent.userID; 
var userID = document.getElementById("userID"); 
userID.value = userIDVariableName; 

var usernameVariableName = parent.username; 
var username = document.getElementById("username"); 
username.value = usernameVariableName;

如上所述,请记住更改域。并将其设置在加载 iframe 的页面中。


ui.R:

library(shiny) 

shinyUI( bootstrapPage( 

# Hidden input boxes to save the variable to 
HTML(‘ <input type="text" id="userID" name="userID" style="display: none;"> ‘), 
HTML(‘ <input type="text" id="username" name="username" style="display: none;"> ‘), 


# include the js code 
includeScript("get_user_id.js"), 

# Show the output 
textOutput("view") 
))

根据需要更改脚本的路径。


服务器.R:

shinyServer(function(input, output, session) { 



userID <- reactive({ input$userID }) 
username <- reactive({ input$username }) 

output$view <- renderText( paste0( "User ID is: ",userID()," and username is: ",username() ) ) 

})


将此添加到包含 iframe 的页面:

PHP 从 wordpress 中获取变量。

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

然后使它成为一个java变量:

<script type="text/javascript"> 
var username = <?php echo json_encode($user_name) ?> ; 
var userID = <?php echo json_encode($user_ID) ?> ; 
</script>

设置域和 iframe:

<script type="text/javascript"> 
document.domain = "MYDOMAIN.com"; 
</script> 
<iframe id="example1" style="border: none; width: 100%; height: 500px;" src="PATH_TO_SHINY_APP" width="300" height="150" frameborder="0"></iframe>
于 2014-05-27T12:08:03.170 回答
0

如果您想onInputChange在 Shiny 应用程序启动后立即调用,那么您需要使用以下 JavaScript 代码而不是监听点击事件。

$(function() {
  setTimeout(function() {
    Shiny.onInputChange("mydata", userID);
  }, 10)
});
于 2016-01-03T18:28:34.303 回答