Does the inclusion of schemes affine over $S$ to $S$-schemes have a left adjoint? Let $S$ be a scheme. Consider the category $\mathrm{Aff}_{/S}$ of schemes affine over $S$, by which I mean $S$-schemes $X$ such that the structure morphism $X \to S$ is affine. For simplicity, I'll also call those affine $S$-schemes (though they don't have to be affine as an absolute scheme!). Consider the inclusion functor $\mathrm{Aff}_{/S} \to \mathrm{Sch}_{/S}$. Does this have a left adjoint?
The question is motivated by the "absolute analogue". It's well-known for $S=\mathrm{Spec}(\Bbb Z)$ that a left adjoint to $\mathrm{Aff} \to \mathrm{Sch}$ exists and it is given by $X \mapsto \mathrm{Spec}(\Gamma(X,\mathcal O_X))$. The same construction works as long as $S$ is affine.
One naïve way one might try to do a similar thing in the general setting is to use the relative Spec. The relative Spec construction has a nice universal property, so that seems promising. The idea is to define a functor $\mathrm{Sch}_{/S} \to \mathrm{Aff}_{/S}$ by sending $X \xrightarrow{f} S$ to $\underline{\mathrm{Spec}}_S(f_* \mathcal O_X) \to S$. The problem with this is that $\underline{\mathrm{Spec}}_S(f_* \mathcal O_X)$ is not even defined, because for general $f$, there's no reason that $f_* \mathcal O_X$ is quasicoherent! (This is however true if $f$ is qcqs.) So that doesn't work. Of course, via the relative Spec functor, the category of affine $S$-schemes is anti-equivalent to the category of quasicoherent $\mathcal O_S$-algebras, so the question could be recast in terms of asking for a functor from $S$-schems to quasicoherent $\mathcal O_S$-algebras with certain properties. Certainly taking the pushforward of the structure sheaf gives us a canonical $\mathcal O_S$-algebra, that may however not be quasicoherent, as mentioned before. So I guess if there was a sufficiently canonical way to associate to each $\mathcal O_S$-algebra a quasicoherent $\mathcal O_S$-algebra, applying that to the pushforward of the structure sheaf and then taking the relative Spec would give us our desired functor. Actually, there are some situations where one can associate to some non-quasicoherent module a "canonical" quasicoherent module with a suitable universal property, see here. I don't see how that lemma is applicable in this situation, however.
One could of course try to use adjoint functor theorems, however the original motivation for the question was figuring out whether the inclusion $\mathrm{Aff}_{/S} \to \mathrm{Sch}_{/S}$ preserves limits. (Note that $\mathrm{Aff}_{/S}$ actually has all limits because the category of quasicoherent $\mathcal O_S$-algebras has all colimits.) That's of course asking less than the existence a left adjoint, but in practice exhibiting a left adjoint is often a good way to show that a functor preserves limits.
 A: I figured it out.
Let's write $\mathrm{QCoh}(\mathcal O_S)$ for the category of quasicoherent $\mathcal O_S$-modules and $\mathrm{Mod}(\mathcal O_S)$ for the category of all $\mathcal O_S$-modules. It turns out that the inclusion $\iota:\mathrm{QCoh}(\mathcal O_S) \to \mathrm{Mod}(\mathcal O_S)$ always has right adjoint, which we will denote by $Q:\mathrm{Mod}(\mathcal O_S) \to \mathrm{QCoh}(\mathcal O_S)$. For details, see here.
Because the inclusion $\mathrm{QCoh}(\mathcal O_S) \to \mathrm{Mod}(\mathcal O_S)$ is strong monoidal (as we all know, the tensor product of quasicoherent modules is quasicoherent), we actually have a monoidal adjunction $\iota  \dashv Q$. A monoidal adjunction always induces an adjunction between the corresponding categories of monoids (see here), thus the inclusion functor $\mathrm{QCohAlg}(\mathcal O_S) \to \mathrm{Alg}(\mathcal O_S)$ has a right adjoint, which we will also denote by $Q$.
With this, we can actually define a functor $F:\mathrm{Sch}_{/S} \to \mathrm{Aff}_{/S}$ as follows: for a $S$-scheme $X \xrightarrow{f} S$, we set $F(X \xrightarrow{f} S) = \underline{\mathrm{Spec}}_S(Q(f_*(\mathcal O_X))) \in \mathrm{Aff}_{/S}$. We now check that $F$ has the right universal property. Let $(Y \to S) \in \mathrm{Aff}_{/S}$, then $Y=\underline{\mathrm{Spec}}_S(\mathcal A)$ for a quasicoherent $\mathcal O_S$ algebra $\mathcal A$. Then we have for any $(X \xrightarrow{f} S) \in \mathrm{Sch}_{/S}$ by the universal property of the relative Spec and the fact that $Q$ is a right adjoint of the inclusion $\mathrm{QCohAlg}(\mathcal O_S) \to \mathrm{Alg}(\mathcal O_S)$:
$$\mathrm{Hom}_{\mathrm{Sch}_{/S}}(X,\underline{\mathrm{Spec}}_S(\mathcal A))\cong \mathrm{Hom}_{\mathrm{Alg}(\mathcal O_S)}(\mathcal A,f_*\mathcal O_X)\cong \mathrm{Hom}_{\mathrm{QCohAlg}}(\mathcal A,Q(f_*\mathcal O_X)) \cong \mathrm{Hom}_{\mathrm{Aff}_{/S}}(\underline{\mathrm{Spec}}_S(Q(f_*\mathcal O_X)),\underline{\mathrm{Spec}}_S(\mathcal A))=\mathrm{Hom}_{\mathrm{Aff}_{/S}}(F(X),\underline{\mathrm{Spec}}_S(\mathcal A))$$
Showing that indeed $F$ is left adjoint to the inclusion $\mathrm{Aff}_{/S} \to \mathrm{Sch}_{/S}$, as desired.
