I am plotting countries on map using plotly and r shiny. I would like subset of data containing rows about the country to appear in the form of data table on clicking on country on a map. But I am unable to implement it. I get the table but there is not data displayed in the table. Any help would be appreciated!
Mapbox_Token= 'Mapbox token'
library(plotly)
library("readxl")
library(dplyr)
library(readxl)
library(writexl)
library(shiny)
library(htmlwidgets)
data_1<- read.csv(".file.csv")
print(data_1)
library(formattable)
Sys.setenv("MAPBOX_TOKEN" = Mapbox_Token) # for Orca
ui <- fluidPage(
plotlyOutput(outputId = "Plot"),
DT::dataTableOutput('click')
)
server <- function(input, output,session) {
output$Plot <- renderPlotly({
df=read.csv("file2.csv")
render_value(data_1)
fig <- df%>% plot_mapbox(lat = ~lat, lon = ~lng,split = ~Country,
size=0, type= 'scattermapbox',mode='markers',hoverinfo="none",showlegend=F,source='subset'
)
fig <- fig %>% layout(title = 'No Of Companies',font=
list(color='white'),plot_bgcolor = '#191A1A', paper_bgcolor =
'#191A1A',mapbox = list(style = 'dark'),legend = list(orientation ='v',font = list(size = 6)),margin = list(l = 25, r = 25,b = 75, t = 25,pad =
2))
fig<-fig %>% add_annotations(text ='Map shows number of
companies by country. The size of the circles correspond to
the number of
companies.',x=0.5,y=-0.2,showarrow=FALSE,font=list(color='red'))
fig<- fig %>% add_markers(text = ~paste(paste('Country:',Country),
paste("Number of Companies:",Name ), paste("Dataset:", Url),sep = "
<br />"), size=~Name, hoverinfo = "text",marker=list(sizeref=0.1,
sizemode="area"),showlegend=T)%>%
onRender(fig, "function(el) {el.on('plotly_click', function(d) {var
url = d.points[0].customdata;window.open(url);});}")
fig <- fig %>% config(mapboxAccessToken = Sys.getenv("MAPBOX_TOKEN"))
})
render_value=function(df){
output$click <- renderDataTable({
s <- event_data("plotly_click",source = "subset")
print(s$y)
return(DT::datatable(data_1[data_1$Country==s$y,]))
})
}
}
shinyApp(ui,server)