Question: If $f(\mathbb{R})$ is compact and $f$ is continuous, then is $f$ uniformly continuous?
Background:
I thought of the question when proving that "If a function is periodic and continuous, then the function is uniformly continuous" (Proof that a periodic function is bounded and uniformly continuous.)
It turns out that if $f$ has a period of $p$, then $f([0,p]) = f(\mathbb{R})$. Since the image of a continuous function on a compact set is compact, $f(\mathbb{R})$ is compact.
Therefore, I was wondering if it is possible to generalize: If $f(\mathbb{R})$ is compact and $f$ is continuous, then is $f$ uniformly continuous?
Attempt:
Let $f:\mathbb{R} \to [0,1]$ be continuous. Then $f$ is not a bijection (see: http://at.yorku.ca/cgi-bin/bbqa?forum=ask_a_topologist_2011&task=show_msg&msg=2700.0002).
Let $f(p) = 0$ and let $f(q) = 1$. Without loss of generality, let $p < q$. For all $y \in (0,1)$, by the Intermediate Value Theorem, there exists a $c \in R$ such that $f(c) = y$ and $p < c < q$.
Therefore, $f([p,q]) = f(\mathbb{R}) = [0,1]$. Recall that a continuous function on a compact set is uniformly continuous. Thus $f$ is uniformly continuous on $[0,1]$.
Let $\epsilon > 0$. For $x,y \in [p,q]^c$ such that $|x-y|< \delta_{[p,q]}$, by the definition of $f$, we have $f(x),f(y) \in [0,1]$. By the intermediate value theorem, there exists $a,b \in [p,q]$ such that $f(a) = f(x)$ and $f(b) = f(y)$. Thus, $|f(a) - f(b)| = |f(x) - f(y)| < \epsilon$.
Finally:
Is the theorem correct? Am I missing a counter-example or is there a gap in my proof?
Is it true in $\mathbb{R}^n$? Is there an intermediate value theorem for vector spaces?
Thanks