Note: this post is seriously out-of-date, and you should ignore it completely. See the Sweave manual for instructions with latest versions of LyX.
You have been warned above.
For a long time I’ve been wondering why we are not able to use Enter in the LyX Scrap environment which was set up by Gregor Gorjanc for Sweave. Two weeks ago, I (finally!) could not help asking Gregor about this issue, as I’m using ``LyX + Sweave’’ more and more in my daily work. He explained it here: LyX-Sweave: mandatory use of control+enter in code chunks
After digging into the LyX customization manual for a while, I found a solution which allows us to press the Enter key just as we normally do when typing in a LyX document. The key is to use Environment instead of paragraph as LatexType for the style definition of Scrap. Besides, I used the LatexName as wrapsweave, as a LatexName is required by LyX. The definition for wrapsweave is simple: just two empty lines by \par. (If you define it as \newenvironment{wrapsweave}{}{}, you will run into troubles sometimes; especially when you use indent for paragraphs.)
As we know, LaTeX environment cannot be centered in LyX (only paragraphs can), so I defined a special environment ScrapCenter when I want to insert graphics via Sweave and make them center-aligned.
To put all efforts together, this is the new literate-scrap.inc (compare it with http://cran.r-project.org/contrib/extra/lyx/literate-scrap.inc):
Format 2
Preamble
\usepackage{Sweave}
\newenvironment{wrapsweave}{\par}{\par}
EndPreamble
OutputType literate
Style Scrap
LatexType Environment
LatexName wrapsweave
Margin static
TopSep 0.4
BottomSep 0.4
ParSep 0.0
LabelType static
Align left
AlignPossible block, left, right, center
NewLine 0
PassThru 1
FreeSpacing 1
TextFont
Color latex
Family typewriter
EndFont
End
Style ScrapCenter
LatexType Environment
LatexName center
Margin static
TopSep 0.4
BottomSep 0.4
ParSep 0.0
LabelType static
Align center
NewLine 0
PassThru 1
FreeSpacing 1
TextFont
Color latex
Family typewriter
EndFont
End
Enjoy pressing a single Enter key in LyX now! :)
P.S. the side effect of this modification is, your R code will be separated by empty lines. But Sweave will remove blank lines by default, so that is not really a big trouble.
P.P.S. Next time I will write my solution of using pgfSweave in LyX – if you have never tried the pgfSweave package in R, I’d strongly recommend you do it right now!! My comment on this package is: amazingly beautiful!
Donate
As a freelancer (currently working as a contractor) and a dad of three kids, I truly appreciate your donation to support my writing and open-source software development! Your contribution helps me cope with financial uncertainty better, so I can spend more time on producing high-quality content and software. You can make a donation through methods below.
-
Venmo:
@yihui_xie, or Zelle:xie@yihui.name -
Paypal
-
If you have a Paypal account, you can follow the link https://paypal.me/YihuiXie or find me on Paypal via my email
xie@yihui.name. Please choose the payment type as “Family and Friends” (instead of “Goods and Services”) to avoid extra fees. -
If you don’t have Paypal, you may donate through this link via your debit or credit card. Paypal will charge a fee on my side.
-
-
Other ways:
WeChat Pay (微信支付:谢益辉) Alipay (支付宝:谢益辉) 

When sending money, please be sure to add a note “gift” or “donation” if possible, so it won’t be treated as my taxable income but a genuine gift. Needless to say, donation is completely voluntary and I appreciate any amount you can give.
Please feel free to email me if you prefer a different way to give. Thank you very much!
I’ll give back a significant portion of the donations to the open-source community and charities. For the record, I received about $30,000 in total (before tax) in 2024-25, and gave back about $15,000 (after tax).