Q:

Extract the edge between two raster cells with different values

Is there a way in R to create lines (black lines in the figure below) along the edge between two raster cells that have different values ?

Update
I'm trying the functions clump() and rasterToPolygons() as in the post [https://stackoverflow.com/questions/28859181/how-to-get-contour-lines-around-the-grids-in-r-raster].
First, I reclassified my raster "r" to have cells with two different values:
new.values <- cbind(c(0, 1, 2, 3, 4, 5, 6, 7, 8), c(NA, NA, 1, NA, 2, NA, NA, NA, NA))
new.r <- reclassify(r, rcl=new.values)

Then, I used the functions rasterToPolygons and clump:
test.edge <- rasterToPolygons(clump(new.r), dissolve=TRUE)
plot(test.edge, add=TRUE)

And I obtained:

How can I extract the edges that have not contour lines (for example, edges near red arrows) because they are located between two different values? 

A:

I think it is not wise to mix the clump(..) functionality from igraph with the dissolve=TRUE parameter from the rasterToPolygon routine. They both do something with to aggregate the fields together but in a different way. At least we want to do 3 things:

read or desing a raster
select raster area where the contour goes around
define how the contour is shaped (rectangular,linear or smooth) and define which areas belong together.

In the clump code raster, the type of raster and the definition of NA seems to be important to steer clumping the process. I made some test but with bad results. I followed your sketch and here is a little analysis how to get things work:  
# Load packages
require('raster')
require('rgeos')

# Clean up everything
rm(list=ls())

# Set a defined random seed
set.seed(2)

# Create a float raster with values
# the interval [0,2] float
rs <- raster(nrow=10, ncol=10)

# Scale the random numbers from interval [0,1) to
# to [0,2.2) and shift the interval to [-0.1,2.1)
rs[] <- runif(ncell(rs)) * 2.2 - 0.1

# Cut off the raster values to [0,2]
# Everything smaller then 0 is zero
values(rs)[values(rs) < 0.0] <- 0.0

# Everything larger then 2 is two
values(rs)[values(rs) > 2.0] <- 2.0

Is the raster field well constructed?
# Construction is OK?
> quantile(values(rs))
       0%       25%       50%       75%      100% 
0.0000000 0.3928834 0.8733250 1.6027794 2.0000000 

Here is the function prototype that selects the field in the interval [1,2).
# Function of the contour ID
inOne <- function(x) { x>=1 & x<2 }

> inOne(0) 
[1] FALSE

> inOne(1) 
[1] TRUE

> inOne(2)
[1] FALSE

The contour cannot be dissolved (clump) because of the float number nature of the raster field is distinct in the ID process (dissolve-=TRUE).
# Contour of the float desing
# x := [1,2)
ct <- rasterToPolygons(rs, 
         fun=inOne , 
         dissolve=TRUE) 
plot(rs)
plot(ct, add=TRUE)

You see the right contour groups, but polygons are not joint.

So if we have a unique ID of each cell as Integer, the dissolve process should work.
# Apply integer operation (ceiling, floor, round)
# to the float number fields 
rs.int <- ceiling(rs)
values(rs.int)
ct.int <- rasterToPolygons(rs.int, 
              fun=inOne , 
              dissolve=TRUE) 
plot(rs.int)
plot(ct.int, add=TRUE)

Conclusion: I think (do know not exactly) what behind the clump stuff works a raster based region growing routine. The process  dissolve=TRUE in the rasterToPolygon (based on rgeos CRAN) seems to follow a vector approach. So I've to read the manuals of igraph and rgeos carfully.
REM: The selection of contours (float vs. int) differs, because of the nature of (ceiling, floor and round). 

