TITLE: Morphisms of locally ringed spaces into affine schemes
QUESTION [6 upvotes]: In Görtz and Wedhorn's Algebraic Geometry I, there's the following proposition:
Proposition 3.4. Let $(X,\mathcal O_X)$ be a locally ringed space. If $Y$ is an affine scheme then the natural map below is an isomorphism $$\mathsf{Hom}(X,Y)\overset{\cong}{\longrightarrow} \mathsf{Hom}(R,\Gamma(X,\mathcal O_X))$$
A proof is given only for the case where $X$ is a scheme, and proceeds by reducing to the anti-equivalence of affine schemes with commutative rings and then gluing the morphisms.
For the general case, the authors cite Prop 1.6.3 of the 1971 edition of EGA I, to which I don't have access. Can anyone reproduce/explain the proof?

REPLY [6 votes]: Here is a sketch: Let $\alpha : R \to \Gamma(X,\mathcal{O}_X)$ be a ring homomorphism. We want to define a morphism $f:X \to \mathrm{Spec}(R)$ which is $\alpha$ on global sections. Let $x \in X$, and consider the composition of $\alpha$ with $\Gamma(X,\mathcal{O}_X) \to \mathcal{O}_{X,x}$, $s \mapsto s_x$. Pull back the unique maximal ideal $\mathfrak{m}_x$ of $\mathcal{O}_{X,x}$ to $R$, this is a prime ideal of $R$. Denote this by $f(x)$. This defines a map of sets $f : X \to \mathrm{Spec}(R)$. To show continuity, let $r \in R$ and observe that the preimage of $D(r) \subseteq \mathrm{Spec}(R)$ in $X$ is the set of points $x \in X$ such that $\alpha(r)_x$ is invertible in $\mathcal{O}_{X,x}$. This set $D(\alpha(r))$ is easily seen to be open. Also notice that by the universal property of localization, $\alpha$ induces a homomorphism $R[r^{-1}] \to \Gamma(D(\alpha(r)),\mathcal{O}_X)$, i.e. $\Gamma(D(r),\mathcal{O}_{\mathrm{Spec}(R)}) \to \Gamma(f^{-1}(D(r)),\mathcal{O}_X)$. These maps are compatible and therefore glue to a morphism of sheaves $f^{\#}:\mathcal{O}_{\mathrm{Spec}(R)} \to f_* \mathcal{O}_X$.