shiny.Rmd
In globe4r nearly every function is shiny friendly: the vast majority have a proxy method. Let’s a very create a basic application.
library(shiny)
ui <- fluidPage(
globeOutput("globe")
)
server <- function(input, output){
output$globe <- render_globe({
create_globe()
})
}
shinyApp(ui, server)
With our basic shiny application setup we can demonstrate how proxies and their methods work in globe4r. Let’s add a dropdown to allow users to change the globe map.
library(shiny)
ui <- fluidPage(
globeOutput("globe"),
actionButton("draw", "Draw map")
)
server <- function(input, output){
output$globe <- render_globe({
create_globe()
})
observeEvent(input$draw, {
globe_proxy("globe") %>%
globe_img_url(image_url("blue"))
})
}
shinyApp(ui, server)
The globe_proxy
function creates a “proxy” of our visualisation and enables modifying it in place, without redrawing th entire globe.
library(shiny)
df <- agriland
scale_color <- scales::col_numeric(c("#2c7fb8", "#7fcdbb", "#edf8b1"), c(0, 1))
df$color <- scale_color(df$percent)
df$percent <- (df$percent / 2) + .1
ui <- fluidPage(
globeOutput("globe", height = 500),
actionButton("draw", "Draw polygons")
)
server <- function(input, output){
output$globe <- render_globe({
create_globe()
})
observeEvent(input$draw, {
globe_proxy("globe") %>%
globe_choropleth(
data = df,
coords(
country = country_code,
altitude = percent,
cap_color = color
)
)
})
}
shinyApp(ui, server)
You can also pick up callback events: how users interacti with you globe visualisation. Every globe layer (hex bin, points, labels, arcs, choropleth), can send data information on element clicked, right clicked, or hovered. These can be accessed in the R server like a normal input which you construct from the id
of the globe visualisation you want to watch and the event you want to catch, e.g.: input$myGlobe_click_bar
, or input$myOtherGlobe_hover_arc
, note that you capture events related to globe_chorpleth
with *_polygon
, e.g.: input$theGlobeId_right_click_polygon
.
Events:
_click_bar
, _click_label
, _click_polygon
, _click_hex
, _click_arc
)._right_click_bar
, _right_click_label
, _right_click_polygon
, _right_click_hex
, _right_click_arc
)._hover_bar
, _hover_label
, _hover_polygon
, _hover_hex
, _hover_arc
).Examples, where globeId
is the id of the globe.
input$globeId_click_bar
will return data on bar clicked by the user.input$globeId_hover_hex
will return data on hex bin hovered.input$globeId_right_click_polygon
will return data on polygon right clicked.Note that these are not enabled by default as it can be draining on the client/server as you send data from the client to the server, you enable it in the various globe_*
functions by setting on_click
, on_right_click
, or on_hover
to TRUE
. Let’s demonstrate with a shiny app. Below we have a bog standard shiny application that draws our globe.
library(shiny)
ui <- fluidPage(
globeOutput("globe")
)
server <- function(input, output){
output$globe <- renderGlobe({
create_globe() %>%
globe_img_url() %>%
globe_bars(coords(lat, long, color = mag), data = quakes) %>%
globe_pov(-21, 179) %>%
scale_bars_color()
})
}
shinyApp(ui, server)
We can now use events to capture which bar the user clicked, notice the on_click = TRUE
in globe_bars
.
library(shiny)
ui <- fluidPage(
globeOutput("globe"),
verbatimTextOutput("clicked")
)
server <- function(input, output){
output$globe <- renderGlobe({
create_globe() %>%
globe_img_url() %>%
globe_bars(coords(lat, long, color = mag),
data = quakes, on_click = TRUE # set on_click to TRUE
) %>%
globe_pov(-21, 179) %>%
scale_bars_color()
})
output$clicked <- renderPrint({
input$globe_click_bar
})
}
shinyApp(ui, server)
Note that we simply print the information returned by the event but ideally you would do something interesting with it.