~variable and access column without $

Hi guys,

Can you guys please clear me from these examples Leaflet for R - Using Leaflet with Shiny 2 things ?

1st: What are tilde sings doing here

fitBounds(~min(long), ~min(lat), ~max(long), ~max(lat))

or

addCircles(radius = ~10^mag/10, weight = 1, color = "#777777",
        fillColor = ~pal(mag), fillOpacity = 0.7, popup = ~paste(mag)
      )

etc.

2nd: How the R can access variable without file/table name ? like it is accessing mag here.

proxy %>% addLegend(position = "bottomright",
        pal = pal, values = ~mag
      )

but before that it uses the parent quakes to access mag

filteredData <- reactive({
    quakes[quakes$mag >= input$range[1] & quakes$mag <= input$range[2],]
  })

also please why is there a , (comma) at the end of index above statement 2]**,**]? and if not put in it gives error Error in [.data.frame: undefined columns selected.

Thank you.

1 Like

Your first question: - the tilde notation - is kinda tricky. I tells leaflet to interpret the formula within context of the leaflet data. The examples should start with something like leaflet(data = something) where the something is a data frame - likely quakes.

So what the fit bounds does is it checks the data object of the leaflet call, and calculates the min & max over long and lat colums. Similar thing is with variables "without file name" - the values = ~mag means it will pull values from column mag from the data object.

I mentioned it was tricky because this is a construction you don't see very often.

As for the second: this is on the other hand rather common use case. You are subsetting the quakes data frame by a somewhat complicated logical vector - quakes$mag >= input$range[1] & quakes$mag <= input$range[2] that will filter out rows meeting criteria specified. Since you do not wish to filter columns in any way you just leave the part after comma blank, but the coma has to be there since the quakes object is two dimensional - it has both rows and columns.

To put it into more context: mtcars[1, ] gives you first row of mtcars (Mazda RX4). mtcars[,1] will give you the first column of mtcars (miles per gallon). mtcars[2,2] will give you the value of second column & second row of mtcars = 6 cylinders of Mazda RX4 Wag.

1 Like

Thanks for your answer @jlacko .

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.