如何从server.R

时间:2015-12-09 16:10:23

标签: shiny-server shiny

我尝试构建一个应用程序,让用户在谷歌地图上选择矩形。不应提取坐标并将其用于进一步计算。我发现了一个很好的教程here,关于如何与google api进行交互并将数据发送给R.但是,我需要稍微修改文件以获得一个让用户选择的costum ui.R字段,但也选择其他重要的处理选项。 所以现在我在同一目录中有三个文件。 带有javascript命令的html文件,用于google api,ui.R和server.R。

的index.html

内容可以在上面的链接中找到,但重要的部分是:

//This function listen to the drawing manager and after you draw the rectangle it extract the coordinates of the NE and SW corners
  google.maps.event.addListener(drawingManager, 'rectanglecomplete', function(rectangle) {  
   var ne = rectangle.getBounds().getNorthEast();  
      var sw = rectangle.getBounds().getSouthWest();  

      //The following code is used to import the coordinates of the NE and SW corners of the rectangle into R  
      Shiny.onInputChange("NE1", ne.lat());  
      Shiny.onInputChange("NE2", ne.lng());  
      Shiny.onInputChange("SW1", sw.lat());  
      Shiny.onInputChange("SW2", sw.lng());  

ui.R

我使用tags$embedindex.html文件嵌入ui.R

require(shiny)
shinyUI(
  fluidPage(
    titlePanel("Included Content"),
    # mainPanel(
    fluidRow(
      column(9,
             div(style='height:800px; width:1200px; overflow: hidden',
                 tags$embed(src="./index.html", seamless=FALSE,width="100%",height="100%"))),
      column(3,
             h2("Some Calculations"),
             selectInput("Test",label=NA,choices=c("yes","no")),
             selectInput("Test2",label=NA,choices=c("2","3")),
             h3("Coordinates"),
             tableOutput("test"),
             h3("allInput"),
             tableOutput("test2"))

    )
  ))

server.R

# server.R  
library(sp)  
library(rjson)  
library(shiny)
library(dplyr)
library(tidyr)
shinyServer(function(input, output,session) {  
  CorLongLat<<-reactive({
    if(length(input$NE1)>0){ 
      as.data.frame(matrix(c(input$NE2,input$NE1,input$NE2,input$SW1,input$SW2,input$SW1,input$SW2,input$NE1),ncol=2,byrow=T)) 
    }
  })
  output$test<-renderTable({
    CorLongLat()
  }) 

  output$test2<-renderTable({
    as.data.frame(reactiveValuesToList(input))%>%gather(ID,Value)
  }) 

}) 

但是,如果我尝试通过output$test2<-renderTable({ as.data.frame(reactiveValuesToList(input))%>%gather(ID,Value) })

显示所有输入

它仅显示直接嵌入ui.R的控件。如何访问NE1,NE2,SW1,SW2中通过index.html创建的变量Shiny.onInputChange

修改 与此同时,我更接近问题的核心。使用options(shiny.trace=TRUE)运行应用程序在启动应用程序后告诉我

Listening on http://127.0.0.1:4840
SEND {"config":{"workerId":"","sessionId":"16000363ddc17409cd5fdf25c038b61d"}}
RECV {"method":"init","data":{"Test":"yes","Test2":"2",".clientdata_output_test4_hidden":false,".clientdata_pixelratio":1,".clientdata_url_protocol":"http:",".clientdata_url_hostname":"127.0.0.1",".clientdata_url_port":"4840",".clientdata_url_pathname":"/",".clientdata_url_search":"",".clientdata_url_hash_initial":"",".clientdata_singletons":"",".clientdata_allowDataUriScheme":true}}
SEND {"errors":[],"values":[],"inputMessages":[]}
SEND {"config":{"workerId":"","sessionId":"0881062c26c882de2d0648a96ed98296"}}
RECV {"method":"init","data":{".clientdata_output_json_hidden":false,".clientdata_pixelratio":1,".clientdata_url_protocol":"http:",".clientdata_url_hostname":"127.0.0.1",".clientdata_url_port":"4840",".clientdata_url_pathname":"/index.html",".clientdata_url_search":"",".clientdata_url_hash_initial":"",".clientdata_singletons":"",".clientdata_allowDataUriScheme":true}}
SEND {"errors":[],"values":[],"inputMessages":[]}

在地图上选择三角形后,我得到了

RECV {"method":"update","data":{"NE1":67.40748724648756,"NE2":-11.77734375,"SW1":62.552856958572896,"SW2":-26.9384765625}}

选择selectInput后:

RECV {"method":"update","data":{"Test2":"3"}}

我认为这里的问题是建立的第二个sessionID。我如何引用server.R中的那些或如何将两者结合起来?

0 个答案:

没有答案