Why is there an R^2 value (and what is determining it) when lm has no variance in the predicted value? Consider the following R code:
example <- function(n) {
    X <- 1:n
    Y <- rep(1,n)
    return(lm(Y~X))
}
#(2.13.0, i386-pc-mingw32)
summary(example(7))    #R^2 = .1963
summary(example(62))   #R^2 = .4529
summary(example(4540)) #R^2 = .7832
summary(example(104))) #R^2 = 0
#I did a search for n 6:10000, the result for R^2 is NaN for
#n = 2, 4, 16, 64, 256, 1024, 2085 (not a typo), 4096, 6175 (not a typo), and 8340 (not a typo)

Looking at http://svn.r-project.org/R/trunk/src/appl/dqrls.f) did not help me understand what is going on, because I do not know Fortran.  In another question it was answered that floating point machine tolerance errors were are to blame for coefficients for X that are close to, but not quite 0.  
$R^2$ is greater when the value for coef(example(n))["X"] is closer to 0. But... 


*

*Why is there an $R^2$ value at all?  

*What (specifically) is determining it?

*Why the seemingly orderly progression of NaN results?

*Why the violations of that progression?

*What of this is 'expected' behavior?

 A: I'm curious about your motivation for asking the question. I can't think of a practical reason this behavior should matter; intellectual curiosity is an alternative (and IMO much more sensible) reason. I think you don't need to understand FORTRAN to answer this question, but I think you do need to know about QR decomposition and its use in linear regression.  If you treat dqrls as a black box that computes a QR decomposition and returns various information about it, then you may be able to trace the steps ... or just go straight to summary.lm and trace through to see how the R^2 is calculated.  In particular:
mss <- if (attr(z$terms, "intercept")) 
          sum((f - mean(f))^2)
       else sum(f^2)
rss <- sum(r^2)
## ... stuff ...
ans$r.squared <- mss/(mss + rss)

Then you have to go back into lm.fit and see that the fitted values are computed as r1 <- y - z$residuals (i.e. as the response minus the residuals).  Now you can go figure out what determines the value of the residuals and whether the value minus its mean is exactly zero or not, and from there figure out the computational outcomes ...
A: As Ben Bolker says, the answer to this question can be found in the code for summary.lm().
Here's the header:
function (object, correlation = FALSE, symbolic.cor = FALSE, 
    ...) 
{

So, let x <- 1:1000; y <- rep(1,1000); z <- lm(y ~ x) and then take a look at this slightly modified extract:
    p <- z$rank
    rdf <- z$df.residual
    Qr <- stats:::qr.lm(z)
    n <- NROW(Qr$qr)
    r <- z$residuals
    f <- z$fitted.values
    w <- z$weights
    if (is.null(w)) {
        mss <- sum((f - mean(f))^2)
        rss <- sum(r^2)
    }
    ans <- z[c("call", "terms")]
    if (p != attr(z$terms, "intercept")) {
        df.int <- 1L
        ans$r.squared <- mss/(mss + rss)
        ans$adj.r.squared <- 1 - (1 - ans$r.squared) * ((n - 
            df.int)/rdf)
    }

Notice that ans\$r.squared is $0.4998923$...
To answer a question with a question: what do we draw from this?  :)
I believe the answer lies in how R handles floating point numbers.  I think that mss and rss are the sums of very small (squared) rounding errors, hence the reason $R^2$ is about 0.5.  As for the progression, I suspect this has to do with the number of values that it takes for the +/- approximations to cancel out to 0 (for both mss and rss, as 0/0 is likely the source of these NaN values).  I don't know why the values differ from a 2^(1:k) progression, though.

Update 1: Here is a nice thread from R-help addressing some of the reasons that underflow warnings are not addressed in R.
In addition, this SO Q&A has a number of interesting posts and useful links regarding underflow, higher precision arithmetic, etc.
A: $R^2$ is defined as   $R^2 = 1-\frac{\textrm{SS}_{err}}{\textrm{SS}_{tot}}$   ( http://en.wikipedia.org/wiki/R_squared ), so if the sum-of-squares-total is 0 then it is undefined. In my opinion R should show an error-message.
