formatR: farewell to ugly R code | /en/2010/04/formatr-farewell-to-ugly-r-code/
formatR: farewell to ugly R code
https://yihui.org/en/2010/04/formatr-farewell-to-ugly-r-code/
https://yihui.org/en/2010/04/formatr-farewell-to-ugly-r-code/
Guest *anthony damico* @ 2010-04-13 21:32:28 originally posted:
This is great! Unfortunately, it doesn't work with JGR..
Yes it is a stand-alone application.
FYI: I deliberately restricted the interface to GTK+ only in the current version, but in fact it is also possible to switch to a Java interface via gWidgetsrJava in the next version (0.1-4).
## Java interface: need gWidgetsrJava package
options(guiToolkit = "rJava")
library(formatR)
Here is the screenshot:
Originally posted on 2010-04-15 03:34:15
Guest *romunov* @ 2010-04-14 00:32:24 originally posted:
I use Eclipse with plugin StatET that manages my pretty code. I've noticed a pretty folded code makes me more proficient. I also enjoy the folding block feature, where I can hide a loop in one line simply by clicking a plus/minus sign left of the line.
Sound great. Thanks for pointing it out! I never tried StatET before, nor did I want to make this package a text editor for R.
If you are a Linux user, Kate might be a good choice too: you can hide a block of code between {} by clicking the signs.
Originally posted on 2010-04-15 03:39:02
Guest *romunov* @ 2010-04-15 09:29:45 originally posted:
I use Eclipse at work and at home, I use Rgedit at home (linux). I like it because it packs a console, is relatively easy to use and has sort of nice syntax coloring. I will be migrating to Eclipse 100% in a short little while. From what I've tested so far, Eclipse on linux is, at least for me, a bit less predictable and perhaps even stable. I tried Kate, but I can't remember why I didn't like it. I think it had something to do with the console.
Guest *anw* @ 2010-04-14 10:12:27 originally posted:
FAILS TO INSTALL REQUIRED SUPPORT PACKAGES: I have tried installing formatR on R 2.10.1 on Mac OS X 10.6 at home as well as on Windows XP SP2 at the office; both installations fail because RGTk+ and gWidgetsRGtk2 fail to install properly. - anw
Installing GTK+ should not be too complicated: gWidgetsRGtk2 depends on RGtk2, and when you install RGtk2, there will be a question prompted out asking you whether to install GTK+; if you choose to install it, R will automatically download GTK+ and install it to your system. After the installation is done, you need to restart R. Let me know if there are still problems. Thanks!
Originally posted on 2010-04-15 03:46:36
Guest *gabriel cervantes* @ 2010-12-06 18:24:18 originally posted:
After load this package, ¿how can I get the window with the convert buttom?
just use formatR() to load the GUI; you have to install gWidgetsRGtk2 first, otherwise you need to use the command line version: tidy.source()
Originally posted on 2010-12-06 19:33:02
Guest *MartinInChemnitz* @ 2011-01-31 04:45:02 originally posted:
What kind of comments are allowed, exactly, Yihui?
I inserted the following chunk into your "LyX-pgfSweave-minimal-demo.lyx" between <> and <>
<>=
SomeVariable=NA
if (is.na(SomeVariable))
{ # Aligning the braces vertically prevents a lot of errors and the comment is spot on here!
SomeOldFile=1
}
@
I got in the ...Rnw.log:
3 : (label=WillItRun)
Error: chunk 3 (label=WillItRun)
Error in base::parse(text = block.text) : 6:3: unexpected SPECIAL
5:
6: { %InLiNe_IdEnTiFiEr%
^
(10089)/ ClientApp::doIt: Creating ClientApp
Error during wrapup: Take a look at the error log file: /home/andreas/Documents/Lyx/tmp/lyx_tmpdir.hX9533/lyx_tmpbuf3/LyX-pgfSweave-minimal-demo.Rnw.log
Execution halted
This is
with formatR and gWidgetsRGtk2 installed and
pgfsweave installed with the latest "lyx-sweave-exe"
following the latest "http://gitorious.org/yihui/lyx-sweave/blobs/raw/master/lyx-sweave-config.R"
under R2.12.1.
The content of the chunk does run as expected when pasted into the R Console the X-terminal.
Do I need to require(formatR) in the "lyx-sweave-exec.R"?
This is answered in the Warning section in ?tidy.source.
Note all syntactically correct R code can be formatted by formatR. I wrote the function tidy.source() originally only for my own use, because it is not an elegant solution in general. You need to read the documentation really hard to be really lazy.
BTW, you do not have to install gWidgetsRGtk2; it is not required to run pgfSweave.
Originally posted on 2011-02-01 02:36:32
Guest *MartinInFrankfurtaM* @ 2011-02-08 07:46:17 originally posted:
Reading useful stuff is fun - once I have found it.
The combination of highlighting together with indentation is great - maybe some day in Lyx.
Thank you, Yihui!
Is there perhaps also an obvious way to pass a width.cutoff value to "tidy" by way of a pgfsweave option in the lyx file or by a formateR preferences file?
My code lines are up to 88 characters long and in the comment line underneath I like to put explanations that are aligned with positions in the code line above.
However, the width.cutoff=0.75*getOption("width") appears to break my comment lines into much shorter pieces than the code lines.
If you want the line width to be 88, you can specify the global option width to be 88/.75. Here is my test:
options(width = 88/0.75)
formatR::tidy.source(text = c(paste("##", paste(rep("long", 40), collapse = " ")),
paste(rep(1, 100), collapse = "+")))
And the result does not seem to be too bad:
## long long long long long long long long long long long long long long long long
# long long long long long long long long long long long long long long long long
# long long long long long long long long
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
You can put a code chunk in the beginning of the Sweave document to set the width option, which is usually what I do to my LyX/Sweave documents.
Originally posted on 2011-02-08 11:35:31
Guest *MartinInFrankfurtaM* @ 2011-02-08 17:10:20 originally posted:
Thank you Yihui!
In your pgfSweave-minimal-demo.lyx you have a long code line that this lazy reader would format in Lyx to:
(cut and paste from lyx modulo blank lines that were inserted automatically by the cut and paste itself)
legend("bottomright"
,legend = sprintf("$hat{Y} = %.2f + %.2fx$"
,coef(model)[1]
,coef(model)[2])
,bty = "n"
)
(I focus my laziness on my frequent reading of my code rathar than the one-time writing.)
With ",width=117" added in the options line in your setup chunk as recommended, I get as a listing:
> legend("bottomright", legend = sprintf("$hat{Y} = %.2f + %.2fx$",
coef(model)[1], coef(model)[2]),
+ bty = "n")
The highlighting colors are pretty but as a format for source documentation this inconsisten line wrap appears sloppy.
Guest *MartinInFrankfurtaM* @ 2011-02-08 17:14:20 originally posted:
Some source formatting got lost from the answer window to the post:
I align the commas and parentheses vertically in my code - following the "legend(" ....
Well, this is one place that I've been wrestling with LyX. Good news is LyX 2.0 will no longer add blank lines in your R code when you hit the Enter key (or paste R code into LyX), and I've started working on a new configuration for LyX 2.0. By that time, you should be able to use tidy=FALSE to turn off the automatic formating and get what you want (highlighting will still be there unless you turn it off by highlight=FALSE). Bad news is I have no idea when LyX 2.0 will come out. Its 2.0.0 beta 4 has been released on Feb 6. I guess there won't be a beta 5...
Originally posted on 2011-02-09 02:30:47
I've finished the configuration script under Ubuntu, and it should work with other Linux distributions as well. If you are eager to the final solution to your problem, you may install LyX 2.0 beta 4 (download the source and build: http://www.lyx.org/News#item2) and run this in R:
source('http://gitorious.org/yihui/lyx-sweave/blobs/raw/lyx2/lyx-sweave-config.R')
Again, I have prepared a demo here:
http://gitorious.org/yihui/lyx-sweave/blobs/raw/lyx2/demo/pgfSweave-in-LyX-2.0.lyx
Using Sweave in LyX 2.0 is much more convenient than 1.6.x.
Originally posted on 2011-02-09 11:45:36
Guest *MartinInFrankfurtaM* @ 2011-02-09 19:58:56 originally posted:
Beautiful, Yihui !
few rough spots:
The pfgsweave.module was not downloaded at first and when I issued the download command from the script manually, the modules went into my home folder instead of the lyx...modules folder. Obvious to fix.
"Lyx-package not available"-warning for the Sweave.sty whenever I open the Document settings for your new demo. Need to investigate a little.
The Sweave-module is listed as available, however, and after selecting it, the demo runs fine including the "tidy"-option.
Still miss the syntax highlighting. Is it supposed to be on?
you are not supposed to run the script manually part by part; the script will download pgfsweave.module to a temporary directory first, then copy to ~/.lyx/layouts; however, actually you do not have to run the script -- simply to put the file pgfsweave.module to ~/.lyx/layouts is enough;
LyX should be able to find Sweave.sty if you put it under ~/texmf/tex/latex; Tools --> TeX Information --> LaTeX styles --> If you cannot see Sweave.sty, you may try the button Rescan.
Regarding the PS below: you never have to use sudo for the configuration -- this script does not need to invade your system in that way.
Sweave, the other is pgfSweave. The former one is from LyX and uses R's default Sweave (which I don't like); the latter, obviously, uses pgfSweave (and written by me), which supports highlighting.Originally posted on 2011-02-09 21:09:15
Guest *MartinInFrankfurtAM* @ 2011-02-09 20:30:52 originally posted:
PS:
Sweave.sty could not be found due to a simple rights issue.
I am trying to keep old Lyx 1.6.8 isolated from Lyx2.0beta4 experiments and have user specific texmf's
So the configure under "sudo R" put the Sweave.sty under /root/ where the user was not allowed to read.
You may want to specify which part of your configuration needs to be run as root/sudo
to save tin foil hat wearing conspiration theorists like this one to shoot themselves into their foot -- unless you want to keep confusing the Russians.
and I don't think you can really separate LyX 2.0 from LyX 1.6 -- they share the same user directory, which is ~/.lyx/. The preferences of 1.6 will be updated by 2.0; you might get into a little trouble if you want to go back to 1.6 (only a little trouble).
Originally posted on 2011-02-09 21:15:34
Guest *MartinInFrankfurtAM* @ 2011-02-10 03:16:31 originally posted:
Yihui, your remarks were very helpful - just what the doctor ordered:
The highlight option does work now - with and without tidy!
I am too lazy to cope with "a little trouble" and keep the two lyx versions installed under separate user-accounts - ie, they have separate ~/.lyx - Folders.
So the "sudo R" just served to update all R packages to the same version in "/usr/lib64/R/library" where my users do not have write access (personal paranoia).
Your new script passed the opensuse 11.2-test! Cheers!
haha, it is a smart decision to switch to another user account; in this case, you do not need to worry about anything. Feel free to do crazy things.
In terms of paranoia, I also force the users to update the packages before they use pgfSweave because I'm allergic to old packages :lol: (first try to update all the packages, then only update whatever I can update if the former step fails -- that means you do not need to sudo R -- the script will surrender if it has not enough power).
Originally posted on 2011-02-10 03:52:02
Guest *MartinInFrankfurtAM* @ 2011-02-10 04:17:51 originally posted:
Yihui,
Still mysterious about your pfgSweave layout:
When the content of your lyx2.0 demo is pasted into the new 2.0 thesis template - e.g. just under the "Dedication", the chunk lines are extremely wide spaced on-screen - much more so than in your demo.
The pdf-rendering is perfect, however, including tidy on/off, highligh on/off (modules are just pgfsweave and Custom/header/footlines - no sweave).
Any idea?
Sounds like a font problem. Can you put a screenshot somewhere? (e.g. http://imgur.com/)
Originally posted on 2011-02-10 04:59:55
Guest *MartinInFrankfurtAM* @ 2011-02-10 11:08:30 originally posted:
OK, I'm pretty sure this is due to the font for code. This defines which mono-spaced font to use:
Tools --> Preferences --> Look & Feel --> Screen Fonts --> Typewriter
If this does not work, it is probably because the layout defined a special font somewhere. I did not see a layout for "Dedication" anywhere.
Originally posted on 2011-02-10 11:18:04
Guest *MartinInFrankfurtAM* @ 2011-02-11 02:27:45 originally posted:
Yihui,
Would you not seek the differences in the Documents settings rather than in the Tools...?
Here is a screenshot with your pgf2.0Demo (left) and the same text cut and pasted into the new Thesis template (right) side by side.
The Tools –> Preferences –> Look & Feel –> Screen Fonts window is shown on top:
http://i.imgur.com/NwqFd.jpg
Can you reproduce this screen?
Narrowing in the differences between the documents:
The "Thesis"-template of lyx2.0 has document class"book(KOMAscript)" and your demo is "article".
Changing your demo to book(koma) does indeed change the screen font of the Title to sans serif but
the chunks remain unchanged.
I cannot reproduce it when I change the layout to KOMA script: http://i.imgur.com/B5WqZ.png (see the bottom that the documentclass is srcbook, which is book(KOMA script))
This does not seem to be a problem related to the font -- it is just that the lines are double-spaced... Can you take a look at the source code to check if there are really blank lines? (or just visually double spaced?) Menu: View --> View source
Maybe I can find something about the line space in the LyX customization manual.
Originally posted on 2011-02-11 03:04:14
Guest *MartinInFrankfurtAM* @ 2011-02-11 04:33:52 originally posted:
Recipe for reproduction of the mysterious screen picture below:
Guest *MartinInFrankfurtAM* @ 2011-02-11 05:06:01 originally posted:
Getting closer:
I deleted all text from the Thesis.lyx.
Changed the document class to article, and deleted the Custom header/Footlines module to keep only the pgfsweave module.
Then select/copied all test from demo-pgfSweave-in-LyX-2.0.lyx and pasted in to the empty Thesis.lyx - saved it as Test.lyx.
then ran
diff -ae Test.lyx demo_pgfSweave-in-LyX-2.0.lyx
This is the output:
329d
324d
319d
314d
309d
300d
295d
290d
277d
272d
267d
262d
257d
248d
243d
238d
220d
215d
210d
141,142c
papersides 1
paperpagestyle default
.
136,138c
tocdepth 3
paragraph_separation indent
paragraph_indentation default
.
104,130c
suppress_date false
use_refstyle 1
.
96c
use_amsmath 1
.
83,94c
use_hyperref false
papersize default
.
80,81c
paperfontsize default
.
65,67c
font_roman default
font_sans default
font_typewriter default
.
6,55d
The crucial difference was to change Document settings -> Text layout Separate paragraphs with:
Vertical Space Med Skip to Indentation.
SOLVED!!
Dear Yihui, Thank you for your patient support.
You gave me a sword - now I am learning to dance.
Guest *Subramanian* @ 2011-03-10 20:39:36 originally posted:
Hi Yihui
i installed R 2.12.1, and then installed Lyx 2.0 Beta in my windows 7 system. Then i ran the code for configuring (in http://gitorious.org/yihui/lyx-sweave/blobs/raw/lyx2/lyx-sweave-config.R) in R- i downloaded the R-file to my disk and ran this code in R. It configured Lyx. Thereafter i restarted Lyx and tried your demo in lyx (http://gitorious.org/yihui/lyx-sweave/blobs/raw/lyx2/demo/pgfSweave-in-LyX-2.0.lyx) It dint run- The exact message is this:
An error occured while running:
R script: "C:/Users/SS/Appdata/Roaming/lyx16/scripts/lyx-sweave-exec.r" "UTF8" "pgfsweave" "C:/Users/SS/Appdata/Local/Temp/lyx_tmpdir.gq3652/lyx_tmpbuf2/" "demo_pgfSweave-in-LyX-2.0.Rnw"
Please advise.
访客 *acheng99* @ 2014-09-10 08:57:30 写道:
现在这个不能用了吗?总是提示Error: could not find function "tidy.gui"
用tidy_app():http://yihui.name/formatR/ 或者干脆直接用网页版:https://yihui.shinyapps.io/formatR/
——原帖发布于 2014-09-12 03:30:25
Sign in to join the discussion
Sign in with GitHub