Interactive plotting using R raster: mouse values

I would like to make a small program in R for interactive visualization and modification of some raster datasets considered as color images. The user must open the file (it’s OK from the terminal), enter it into the graph, select points for editing with the clicks of the mouse and insert new values.

Until now, I have achieved so easily. I use the plot() function from the raster package to render the graph, then click() to select the points and change their value through the terminal.

I would like to add the ability to show mouse values. I was looking for ways to do this, but this is not possible with standard R. packages. Is this correct?

In this case, I may be forced to use external packages such as gGobi, iPlots, Shiny or Plotly. However, I would prefer KISS and use only the “standard” graphical tools, such as the raster function plot() or maybe the lattice of graphical objects (for example, from rasterVis ).

I understand that the Shiny app is likely to be better, but it takes a lot of time to learn and improve.

+18
source share
2 answers

With leaflet and mapview you can achieve something similar:

 library(raster) library(mapview) library(leaflet) f <- system.file("external/test.grd", package="raster") r <- raster(f) leaflet() %>% addRasterImage(r, layerId = "values") %>% addMouseCoordinates() %>% addImageQuery(r, type="mousemove", layerId = "values") 

Putting this in a brilliant application, you will receive:

 library(raster) library(mapview) library(leaflet) library(shiny) f <- system.file("external/test.grd", package="raster") r <- raster(f) ui <- fluidPage( leafletOutput("map") ) server <- function(input, output){ output$map <- renderLeaflet({ leaflet() %>% addRasterImage(r, layerId = "values") %>% addMouseCoordinates() %>% addImageQuery(r, type="mousemove", layerId = "values") }) } shinyApp(ui, server) 

The following example illustrates the idea of ​​converting a raster to simple objects / shapefiles. It cannot be used for large files, but tags can be developed individually, data is editable and can be easily displayed in a table.

 library(raster) library(leaflet) library(shiny) library(sf) library(DT) library(dplyr) ## DATA f <- system.file("external/test.grd", package="raster") r <- raster(f) r1 = aggregate(r, 30) sp = st_as_sf(rasterToPolygons(r1)) cn = st_coordinates(st_transform(st_centroid(sp),4326)) sp = st_transform(sp, 4326) sp = cbind(sp, cn) sp$id <- 1:nrow(sp) colnames(sp)[1] <- "value" ## UI ui <- fluidPage( leafletOutput("map"), uiOutput("newValueUI"), textInput("newVal", label = "Enter new value"), actionButton("enter", "Enter new value"), hr(), dataTableOutput("table") ) ## SERVER server <- function(input, output){ ## Reactive Shapefile sp_react <- reactiveValues(sp = sp) ## Leaflet Map output$map <- renderLeaflet({ pal= colorNumeric(topo.colors(25), sp_react$sp$value) leaflet() %>% addPolygons(data = sp_react$sp, label= paste( "Lng: ", as.character(round(sp_react$sp$X,4)), "Lat: ", as.character(round(sp_react$sp$Y,4)), "Val: ", as.character(round(sp_react$sp$value,4))), color = ~pal(sp_react$sp$value), layerId = sp_react$sp$id ) }) ## Observe Map Clicks observeEvent(input$map_shape_click, { click_id = input$map_shape_click$id click_grid <- sp_react$sp[sp_react$sp$id == click_id,] }) ## Observe Action Button observeEvent(input$enter, { click_id <- input$map_shape_click$id sp_react$sp[sp_react$sp$id == click_id,]$value <- as.numeric(input$newVal) }) ## Data Table output$table <- DT::renderDataTable({ sp_react$sp %>% st_set_geometry(NULL) %>% dplyr::select(id,X,Y,value) }) proxy = dataTableProxy('table') ## Table Proxy observeEvent(input$map_shape_click$id, { req(input$map_shape_click$id) proxy %>% selectRows(as.numeric(input$map_shape_click$id)) }) } shinyApp(ui, server) 
+4
source

I will give a simple example of how to do this in R without external Java libraries, if you want to use Javan functions, you can adapt them, but each Java graphics library is different from the others, and I have never done anything like this.

 set.seed(123) mydata <- data.frame(x = runif(10), y = runif(10)) edit_plot <- function(data) { plot(data) sel <- locator(n = 1) if(is.null(sel)) return(TRUE) dd <- (data$x - sel$x)^2 + (data$y - sel$y)^2 data[which.min(dd),] <- edit(data[which.min(dd),]) r <- edit_plot(data) if(r) return(TRUE) } edit_plot(mydata) 

To exit, press Esc when the locator is active.

+2
source

Source: https://habr.com/ru/post/988701/


All Articles