使用反应函数中定义的全局变量时,R闪烁意外错误

时间:2016-02-23 03:48:44

标签: r shiny

下面提供的代码应该有效。目的是您单击表格上的哪一行,该行应显示在底部表格中。为了以后集成表格中的点击行以及地图上的标记,我试图存储两个输入变量的先前值输入$ mytable_row_last_clicked并输入$ mymap_marker_click $ id。我这样做是通过将它们分配给全局变量。

但是,我遇到了一个简单的问题。如果我用标有__________注释掉的两行来运行我的代码,那么我的代码正确初始化,但是如果我将它们取消注释,我会收到错误。错误的位置发生在被动语句中,但我无法理解这两行的评论如何影响任何事情!

library(shiny)
library(leaflet)
library(maps)
library(rgdal)

ui <- fluidPage(
    headerPanel(paste('Datasets')),
    fluidRow(
      column(4,dataTableOutput('mytable')),
      column(6,offset=1,leafletOutput("mymap")),
      column(6,offset=1,
                tabsetPanel(type="tabs", 
                            tabPanel("Primary     Information",dataTableOutput('info')))
    )   
   )  
)


library(shiny)
library(leaflet)
library(maps)
library(rgdal)
library(DT)

server <- function(input, output, session) {
########## Data Frame to be used by render table#######################     
  hospital_coordinates=data.frame(name=c('hosp1','hosp2','hosp3'),
                                  description=c('this is the first', 'This        is the second', 'This is the third'),
                                  lat=c(45.40083,43.6575,42.30836),
                                  long=c(-75.65153,-79.38717,-83.03222),
                                  description2=c('this is the first', 'This is the second', 'This is the third'))
  ##################################Producing List of hospitals#############################

  output$mytable = DT::renderDataTable({
                  DT::datatable(hospital_coordinates[,c(1,4,5)],selection='single',
                                options = list(lengthMenu = c(5, 30, 50), pageLength = 10, orderClasses=TRUE)
                                )
                  } 

                  ) 
 ######################Producing Map#######################

  output$mymap = renderLeaflet({ 
                leaflet()%>%
                addProviderTiles("Stamen.TerrainBackground")%>%
                setView(lng=-81,lat=45,zoom=5)%>%
                #addPolygons(data=ontario, fillOpacity = 0.1,weight=2)%>%
                addCircleMarkers(radius=2,~long , ~lat, data=hospital_coordinates, color = "red", popup=hospital_coordinates$HOSPITAL, layerId=hospital_coordinates$ID)

    })

  #####################Reproducing Description####################################

  return_value=reactive({
        index=input$mytable_row_last_clicked
        clicker=input$mymap_marker_click$id

        if (is.null(index) & is.null(clicker)){
          index_bef<<-1.5
          clicker_bef<<-1.5
          rows_select=1
        }
        else if (!is.null(index) & is.null(clicker)){
          index_bef<<-index
          rows_select=index
        }
        else if (!is.null(clicker) & is.null(index)){
          clicker_bef<<-clicker
          rows_select=clicker
        }


        return(rows_select)
  })


  output$info = DT::renderDataTable({


            hospital_coordinates[return_value(),] 
            print(clicker_bef)___________________________________________________________
            print(index_bef)__________________________________________________________

              }, 
              options = list(pageLength = 5, orderClasses=TRUE, searching=FALSE, paging=FALSE)
              )
}

1 个答案:

答案 0 :(得分:1)

试试这个:

print(clicker_bef)
print(index_bef)
hospital_coordinates[return_value(),] 

在data.frame之前放置print,因为renderDataTable或所有渲染****函数需要最后一行是你要呈现的内容。