最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get screen resolution from JavaScript in R Shiny? - Stack Overflow

programmeradmin0浏览0评论

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

I tried the reference:

  1. Receiving data from .js in server.R shiny
  2. /

So I have:

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
   })

 })

It will return NULL by verbatimTextOutput.

How should I modify the code? Thanks!

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

I tried the reference:

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

So I have:

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
   })

 })

It will return NULL by verbatimTextOutput.

How should I modify the code? Thanks!

Share Improve this question edited Jul 21, 2017 at 13:29 Badacadabra 8,5277 gold badges31 silver badges54 bronze badges asked Oct 21, 2015 at 2:42 Robin ChenRobin Chen 1033 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

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)
    })
  }
))
发布评论

评论列表(0)

  1. 暂无评论