DanRoDuq
DanRoDuq

Reputation: 258

R shiny unexpected error when using global variables defined in reactive function

The code provided below should work. The purpose is that whichever row on the table you click, that row should show up in the bottom table. In order to later integrate clicking rows in the table as well as markers on the map, I am trying to store the previous value of two input variables input$mytable_row_last_clicked and input$mymap_marker_click$id. I do this by assigning them to a global variable.

However, I am having a simple problem. If I run my code with the two lines marked __________ commented out, then my code initialized correctly, however if I leave them uncommented, I get an error. The location of the error happens inside the reactive statement, but I can't understand how commenting those two lines impacts anything!

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

Upvotes: 0

Views: 669

Answers (1)

vck
vck

Reputation: 837

try this:

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

place print before the data.frame because renderDataTable or all render**** functions needs last row is what you want to render.

Upvotes: 1

Related Questions