在应用程序加载时将javascript变量读入/R

在应用程序加载时将javascript变量读入/R,javascript,r,wordpress,shiny,shiny-server,Javascript,R,Wordpress,Shiny,Shiny Server,我想做的是,当我在iframe中加载一个闪亮的应用程序时,从wordpress登录的用户那里获取用户ID。然后可以使用此变量保存和修改特定于用户的数据。 我通过向wordpress页面添加以下代码获得了一些方法: <?php global $current_user; get_currentuserinfo(); $user_name = $current_user->user_login; $user_ID = get_current_user_id(); ?>

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


我通过向wordpress页面添加以下代码获得了一些方法:

<?php global $current_user; 
get_currentuserinfo(); 
$user_name = $current_user->user_login; 
$user_ID = get_current_user_id(); 
?>
server.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")
))
shinyServer(function(input, output, session) {
  output$results = renderPrint({
    input$mydata
  })
})
$(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);
  };
});
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;
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") 
))
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() ) ) 

})
获取用户id.js:

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")
))
shinyServer(function(input, output, session) {
  output$results = renderPrint({
    input$mydata
  })
})
$(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);
  };
});
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;
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") 
))
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() ) ) 

})


请注意,需要域名(或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>

document.domain=“此处的域名”;



但我想在应用加载后立即获取变量,以便在应用中使用它将数据保存到数据库等。
我找不到任何方法来做这件事。我还没有能够使用“.onload”或我尝试过的几个jquery替代方案来实现这一点。如有任何提示,将不胜感激





编辑:也发布在这里:

埃里克·韦斯特隆德很好地提供了以下解决方案


获取用户id.js:

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")
))
shinyServer(function(input, output, session) {
  output$results = renderPrint({
    input$mydata
  })
})
$(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);
  };
});
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;
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") 
))
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的页面中设置它


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")
))
shinyServer(function(input, output, session) {
  output$results = renderPrint({
    input$mydata
  })
})
$(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);
  };
});
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;
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") 
))
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() ) ) 

})
库(闪亮)
shinyUI(自举(
#将变量保存到的隐藏输入框
HTML(“”),
HTML(“”),
#包括js代码
IncludeDescript(“获取用户id.js”),
#显示输出
文本输出(“视图”)
))
根据需要更改脚本的路径


server.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")
))
shinyServer(function(input, output, session) {
  output$results = renderPrint({
    input$mydata
  })
})
$(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);
  };
});
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;
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") 
))
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() ) ) 

})
shinyServer(函数(输入、输出、会话){
用户标识;
var userID=;
设置域和iframe:


document.domain=“MYDOMAIN.com”;

如果您想在闪亮的应用程序启动后立即调用
onInputChange
,则需要使用以下JavaScript代码,而不是侦听单击事件

$(function() {
  setTimeout(function() {
    Shiny.onInputChange("mydata", userID);
  }, 10)
});

谢谢@peter,这很有魅力!onInputChange不需要任何点击事件