如何从R Shiny中获取JavaScript的屏幕分辨率? [英] How to get screen resolution from JavaScript in R Shiny?

查看:130
本文介绍了如何从R Shiny中获取JavaScript的屏幕分辨率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从JavaScript中获取屏幕分辨率,存储在Shiny Server上的 GetScreenWidth 变量中。

I want to get screen resolution from JavaScript, stored in the GetScreenWidth variable on Shiny Server.

I尝试了参考:


  1. 从server.R中的.js接收数据闪亮

  2. https://ryouready.wordpress.com/2013/ 11/20 / send-data-from-client-to-server-and-back-using-shiny /

  1. Receiving data from .js in server.R shiny
  2. https://ryouready.wordpress.com/2013/11/20/sending-data-from-client-to-server-and-back-using-shiny/

所以我有:

ui.R

shinyUI(
  bootstrapPage(

    verbatimTextOutput("results")
    ,tags$script('
        var jsWidth = screen.width;
        Shiny.onInputChange("GetScreenWidth",jsWidth);
    ')
   )
 )

server.R

 shinyServer(function(input,output){

     output$results=renderPrint({
     input$GetScreenWidth
   })

 })

它将返回 NULL by verbatimTextOutput

我应该如何修改代码?谢谢!

How should I modify the code? Thanks!

推荐答案

问题是你在Shiny初始化之前运行JavaScript代码。您可以使用新功能,告诉您何时闪亮准备就绪,这里的示例代码

The problem is that you're running the JavaScript code before Shiny is initialized. You can use the new feature that tells you when shiny is ready, here's example code

jscode <-
'$(document).on("shiny:connected", function(e) {
  var jsWidth = screen.width;
  Shiny.onInputChange("GetScreenWidth",jsWidth);
});
'

library(shiny)
runApp(shinyApp(
  ui = fluidPage(
    tags$script(jscode)
  ),
  server = function(input, output, session) {
    observe({
      cat(input$GetScreenWidth)
    })
  }
))

这篇关于如何从R Shiny中获取JavaScript的屏幕分辨率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆