If $U$ is an open subset such that the codimension of $X-U$ is greater or equal to 2. Then by defition of codimension, $\min\{\dim \mathcal{O}_{X,x}:x\in X-U\}\geq 2$, i.e. points of $X-U$ are all of codimension $\geq 2$. Thus the points of codimension $0$ and $1$ lie in $U$.
It's easy to see that a point is of codimension 0 if and only if it is a generic point of $X$. So $U$ contains all generic points, its closure must be $X$, so $U$ is open dense.
Now we can safely pick any non-empty open affine subset to consider the restriction of our desired isomorphism.
Pick an affine open covering $\{U_i\}_{i\in I} $ of $X$, assuming we have isormophisms $\Gamma(U_i,\mathcal{O}_X)\to \Gamma(U_i \cap U,\mathcal{O}_X)$, we now try to deduce the isomorphism $\Gamma(X,\mathcal{O}_X)\to \Gamma(U,\mathcal{O}_X)$ from the sheaf properties, the exact sequence of sheaves.
It's easy to see that the isomorphism follows from the isomorphisms $\Gamma(U_i\cap U_j,\mathcal{O}_X)\to \Gamma(U_i \cap U_j \cap U,\mathcal{O}_X)$ for all $i$ and $j$ with a simple diagram chase. But $U_i \cap U_j$ is not necessarily affine.
Nevertheless it can be shown that $U_i \cap U_j$ can be covered by simultaneously distinguished open subsets of $U_i$ and $U_j$, see this question in StackExchange.
Now we just apply the same argument to $U_i \cap U_j$ with this covering, this time the intersection of two simultaneously distinguished open subsets is open affine, then we are done.
Ravi is being slightly informal. As in the comments, the correct statement is that there's a natural map coming from the adjunction between schemes and affine schemes and this natural map is an isomorphism.
Best Answer
First, let us review the definition of an affine scheme. An affine scheme $X$ is a locally ringed space isomorphic to $\operatorname{Spec} A$ for some commutative ring $A$. This means that if one knows one has an affine scheme $X$, then all one has to do to recover $A$ such that $X=\operatorname{Spec} A$ is to take global sections of the structure sheaf, ie $A\cong\Gamma(X,\mathcal{O}_X)$.
In order to prove that $\mathbb{P}^n_R$ is not affine, it suffices to show that $\operatorname{Spec}(\Gamma(\mathbb{P}^n_R,\mathcal{O}_{\mathbb{P}^n_R}))\cong \operatorname{Spec} R$ is not isomorphic to $\mathbb{P}^n_R$. This is due to a dimension argument- assume $R$ is noetherian, and $\dim R=d$. Then $\dim\mathbb{P}^n_R=d+n$, as $\dim R[x_1,\cdots,x_n]=d+n$. Unless $n=0$, the two cannot be isomorphic.