Script display from an external file in Jupyter Notebook with syntax-highlighting - jupyter-notebook

I am making a tutorial using Jupyter and I would like to display the content of an external Python script. Printing the content of the file is trivial, but I am interested in a color-coded/syntax-highlighted text (either in a markdown cell or as an output).

Use Ipython's Markdown module:
from IPython.display import Markdown as md
script = """
x = 2
if x*2 > 2:
print('x > 2')
else:
x = None
"""
md("```Python" + script + "```")
Will output:

One method is to use the magic command: %load
%load testLoad.py
This is assuming the external file is in your Jupyter Notebook starting directory
If you only want specific lines (say between lines 5 and 10 and also line 15) of your python code to be displayed then:
%load -r 5-10,15 testLoad.py
You can find out the options of a magic command by adding a '?' at the end of the magic command:
%load?
Magic commands are shortcuts that are used to help do things much faster in Jupyter Notebook. They are great for beginners as they generally contain everything a beginner needs to display and test Jupyter Notebook
Here is a link to all magic commands in IPython for Jupyter Notebook:
https://ipython.readthedocs.io/en/stable/interactive/magics.html

The following magic command (starting with %) can be used to output the contents of a python script with highlighting.
%pycat script.py
Alternatively, it is possible to run shell commands by prepending them with !. If you have pygmentize installed, it is possible to use that (though I recommend %pycat for notebooks):
!pygmentize -g script.py
You can also use the old but good UNIX tool cat on all POSIX compliant systems (Unix, Mac, Linux, BSD). Then you won't get highlighting, but it might be more suitable for text files:
!cat data.txt
On POSIX compliant systems notebooks also come with an alias to cat in the form of a magic command:
%cat data.txt

Related

Running R file to render bookdown output from command line (Terminal)

I'm trying to run an R file from the command line (from Terminal -- on macOS 10.13.3).
Following the directions in this helpful Q & A, I added #! [path/to/file.R] as the first line of the R file.
When I then type Rscript [path/to/file.R] in Terminal, a part of the file to test that it worked seems to run (I added a print() statement), however, the next line:
rmarkdown::render_site(output_format = 'bookdown::pdf_book', encoding = 'UTF-8')
Leads to this error:
Error in rmarkdown::render_site(output_format = "bookdown::pdf_book", :
No site generator found.
Why does the print() statement appear to work fine, but this line - to render a bookdown book - does not?
The file I'm trying to run is on GitHub here.
For Markdown I have had the following script in the examples directory of littler for about 1 1/2 years, and rendered hundreds of times with it.
Note it calls rmarkdown::render() yet you want a bookdown format, so methinks you may need to write a very similar script calling bookdown::render_book() or a similar function.
You can of course do exactly the same as a one-liner from Makefiles, and many people do. Here is one which will map all Rmd file to pdf documents (which is my standard workflow, I don't do much html):
sources := $(wildcard *.Rmd)
slides := $(sources:.Rmd=.pdf)
all: ${slides}
%.pdf: %.Rmd
Rscript -e "rmarkdown::render(\"$<\", clean=TRUE)"
Because my editor has a shortcut for invoking make this is particularly handy.
Lastly, for Rscript (available everyhere) you'd do #!/usr/bin/env Rscript or possibly the direct path to Rscript.
You could use a simple script like this that you save to compile.book.sh for example.
#!/bin/sh
## Get the path to the book
BOOK_PATH=$1
## Get the current path
CURRENT_PATH=$(pwd)
## Get to the right path
cd ${BOOK_PATH}
## Compile the book
R -e 'rmarkdown::render_site(output_format = 'bookdown::pdf_book', encoding = 'UTF-8')'
## Get back to the previous path
cd ${CURRENT_PATH}
You can then execute the compilation as follows from wherever in your machine (from the terminal):
sh compile.book.sh path/to/Rfolder
With path/to/Rfolder being the folder where your index.Rmd is.

Magic %% commands in R inside Jupyter

How do I run %%magic in R inside Jupyter?
%%javascript
IPython.OutputArea.auto_scroll_threshold = 99999;
The auto scroll feature on longer output is vastly annoying as I have several functions and scripts that spit out a lot of output.
The above Javascript works fine in python notebooks but not in R notebooks.
When I run the %% magic command in R, it barfs:
Error in parse(text = x, srcfile = src): <text>:1:1: unexpected SPECIAL
1: %%
Any suggestions?
According to this post disable_autoscroll.py, it may be possible to put that Javascript into a profile_dir/static/js/custom.js file. Pray tell, where is the profile_dir on a Windows box?
I found: c:/Anaconda2/Lib/site-packages/notebook/static/custom/custom.js but that is the central custom.js file.
References:
Auto-scrolling of long output should be configurable in the UI
In my anaconda install of the notebook, the custom.js file is in %USERPROFILE%\.jupyter\custom\custom.js.
For the "magics": magics are a thing of the python kernel, not the notebook. The R kernel does not implement a magic system and so these don't work. As mentioned above, use IRdisplay::display_javascript('IPython.OutputArea.auto_scroll_threshold = 99999;') for your usecase.

R Run knitr from a batch-file (windows)

I run some automatic reports each day with batch-files on my windows-computer. But how do I do this with a .rmd file and generate the html-output?
So, this works for me using a batchfile with a normal .R file:
"C:\R\R-3.0.1\bin\x64\Rscript.exe" --default-packages=methods
"C:\R\R-3.0.1\bin\x64\Scripts\models.R"
But, this won't:
"C:\R\R-3.0.1\bin\x64\Rscript.exe" --default-packages=methods,knitr
"C:\R\R-3.0.1\bin\x64\Scripts\test_knitr.Rmd"
I've tried some variations inspired by command-line like:
"C:\R\R-3.0.1\bin\x64\Rscript.exe" --default-packages=methods,knitr
knit("C:\R\R-3.0.1\bin\x64\Scripts\test_knitr.Rmd")
But no succes so far! Im a total knitr/.rmd newbee, so I'm not even sure it can be done.
I use something along the lines
Rscript -e "require ('knitr'); knit ('test.Rmd')"
I use
"C:\Program Files\R\R-3.5.1\bin\Rscript.exe" -e "library('knitr'); knit('C:/Users/test_doc.Rmd')"
pause
Like "cbeleites supports Monica" said using the full path.
Don't forget that in R we must use "/" and not "\" like in Windows.
So in the R call we use "\" because we are talking to Windows
and in the knit we use "/" because we are talking to R.
I hope it help.

What is the knitr equivalent of `R CMD Sweave myfile.rnw`?

What is the command-line knitr equivalent of R CMD Sweave myfile.rnw?
The general solution (works regardless of the R version):
Rscript -e "library(knitr); knit('myfile.Rmd')"
Since R 3.1.0, R CMD Sweave has started to support non-Sweave documents (although the command name sounds a little odd), and the only thing you need to do is to specify a vignette engine in your document, e.g.
%\VignetteEngine{knitr::knitr}
To see the possible vignette engines in knitr, use
library(knitr)
library(tools)
names(vignetteEngine(package = 'knitr'))
# "knitr::rmarkdown" "knitr::knitr" "knitr::docco_classic" "knitr::docco_linear"
I have a knitme.R script:
library(knitr)
render_html()
source("hooks.R") # mods to defaults
inFile = commandArgs(trailingOnly=TRUE)[1]
outFile = commandArgs(trailingOnly=TRUE)[2]
knit(inFile,output=outFile)
so I can then do
Rscript knitme.R $SOURCE $TARGET
Where $SOURCE and $TARGET are as required.
You could also integrate this into Make, so you had a rule that all you had to do was:
make myfile.html
and it would go to myfile.Rhtml and produce the HTML file. Adjust to make PDF from .Rnw
I'm using it with SCons instead of Make, so I have a Sconscript file which is a bit more complex (partly because I've only just started learning to use SCons, so it might be a bit crufty)
env=Environment()
bld = Builder(action = '/usr/bin/Rscript knitme.R $SOURCE $TARGET',
suffix='.html',
src_suffix='Rhtml')
env.Append(BUILDERS = {'Knit' : bld})
env.Knit(source='test.Rhtml',target='test.html')
Then all I need to do is:
scons test.html
and I get test.html built from test.Rhtml if test.Rhtml has changed.
This is all part of a Sconstruct file that builds an entire web site and copies it to a server, based on all sorts of other dependencies..
Drifting off-topic now...
To add on to the other answers, if you want to knit/render the file and open the output all in one line you can use:
Rscript -e "rmarkdown::render('file.Rmd')" & open file.pdf
I prefer to do it all in one line because it's simpler to run as a reusable Vim command.
You can also replace open with a specific application if you want to use your system's non-default. I tend to use this if I'm on Windows and want to use Sumatra to overwrite a PDF output that's currently open (so I don't have to remember to close it every time).
R CMD knit file.Rmd
is the direct equivalent to R CMD Sweave file.Rmd
Lately, there are enhanced functions in rmarkdown and knitr for this kind of dirty work. For slides, I've been using the Rmarkdown YAML header to designate the intended output format and the command line is basic, like
R -e "library(rmarkdown); render(\"file.Rmd\")"

More efficient R / Sweave / TeXShop work-flow?

I've now got everything to work properly on my Mac OS X 10.6 machine so that I can create decent looking LaTeX documents with Sweave that include snippets of R code, output, and LaTeX formatting together. Unfortunately, I feel like my work-flow is a bit clunky and inefficient:
Using TextWrangler, I write LaTeX code and R code (surrounded by <<>>= above and # below R code chunk) together in one .Rnw file.
After saving changes, I call the .Rnw file from R using the Sweave command
Sweave(file="/Users/mymachine/Documents/Assign4.Rnw",
syntax="SweaveSyntaxNoweb")
In response, R outputs the following message:
You can now run LaTeX on 'Assign4.tex'
So then I find the .tex file (Assign4.tex) in the R directory and copy it over to the folder in my documents ~/Documents/ where the .Rnw file is sitting (to keep everything in one place).
Then I open the .tex file (e.g. Assign4.tex) in TeXShop and compile it there into pdf format. It is only at this point that I get to see any changes I have made to the document and see if it 'looks nice'.
Is there a way that I can compile everything with one button click? Specifically it would be nice to either call Sweave / R directly from TextWrangler or TeXShop. I suspect it might be possible to code a script in Terminal to do it, but I have no experience with Terminal.
Please let me know if there's any other things I can do to streamline or improve my work flow.
I use a Makefile of the following form for my Sweave documents:
pdf: myfile.tex
R CMD texi2pdf myfile.tex
myfile.tex: myfile.Rnw
R CMD Sweave myfile.Rnw
Then I can build the document in one step in the Mac OS Terminal by running the command make pdf
I'm sure there is a way to bring this closer to your one-click goal in Mac OS X, but this works well enough for me.
One-click Sweaving is easy to do in TeXShop using the Sweave.sh script by Gregor Gorjanc.
Get it from http://cran.r-project.org/contrib/extra/scripts/Sweave.sh and put it in your ~/Library/TeXShop/bin/ folder.
Then add the following files to your ~/Library/TeXShop/engines/ folder:
As Sweave.engine:
#!/bin/bash
~/Library/TeXShop/bin/Sweave.sh -ld "$1"
As SweaveNoClean.engine:
#!/bin/bash
~/Library/TeXShop/bin/Sweave.sh -nc -ld "$1"
You'll have to set the permissions on Sweave.sh and the two engine files to allow execution.
To Sweave with one click, restart TeXShop after adding these files, open the Sweave document (with Rnw extension) and in the dropdown menu above the document window, change it from LaTeX to Sweave or SweaveNoClean.
BEWARE: The "Sweave" option wll clean up after itself, deleting all the extra files LaTeX and Sweave creates. If your file is called myfile.Rnw, this will include files called myfile.R and myfile.tex. So a word to the wise: make sure the basename of your Rnw file is unique; then nothing unexpected will be written over and then deleted.
The SweaveNoClean option does not clean up after itself. This makes sure you don't delete anything unexpected; though it could still write over a file called myfile.tex if you Sweave a myfile.Rnw. This also doesn't delete any graphics that have been created, in case you want to have them separate from your full typeset document.
On the bash shell command line:
R CMD Sweave foo.Rnw && pdflatex foo.tex
Runs Sweave, and if that succeeds it goes on to do pdflatex. Out pops a pdf. If you've got this in a bash Terminal then just hit up-arrow to get it back and do it again. And again. And Again.
Makefile solution also good.
RStudio has a button that does this in one go. One caveat is that it runs in its own session, so any workspace variables you may have set are ignored.
Just a note: you can actually call things like pdflatex etc. directly from R using texi2dvi (in the tools package). For example:
Sweave(file="/Users/mymachine/Documents/Assign4.Rnw")
texi2pdf("Assign4.tex")
would compile your Rnw file into a pdf. Thus, no need to leave R to handle the tex->pdf step.
I use these (saved as sweave.engine and sweavebibtex.engine) for custom engines in texshop. I usually work up a code chunk in R, then copy the block into the rnw file I have open in texshop. I'd love a solution that lets me do syntax highlighting and spelling correction of R and tex in the same document (that isnt emacs).
#!/bin/bash
echo 'SWEAVE | PDFLATEX. Custom engine--Akasurak-16Nov2009'
export PATH=$PATH:/usr/texbin:/usr/local/bin
R CMD Sweave "$1"
pdflatex "${1%.*}"
and the second, for doing bibtex as well:
#!/bin/bash
date
before="$(date +%s)"
echo 'SWEAVE | PDFLATEX | BIBTEX | PDFLATEX | PDFLATEX. Custom engine--Akasurak-16Nov2009'
#Updated 20Jul2010 for auto including Sweave.sty
export PATH=$PATH:/usr/texbin:/usr/local/bin
R CMD Sweave "$1"
R CMD pdflatex "${1%.*}"
bibtex "${1%.*}.aux"
R CMD pdflatex "${1%.*}"
R CMD pdflatex "${1%.*}"
after="$(date +%s)"
elapsed_seconds="$(expr $after - $before)"
date
echo Elapsed time '(m:s)': $(date -r $elapsed_seconds +%M:%S)
Can't say they are the best way of doing things, but they do work.
I use either Aquamacs or Eclipse to do the editing of the .Rnw file, then I use the following shell function to compile & view it:
sweaveCache () {
Rscript -e "library(cacheSweave); setCacheDir(getwd());
Sweave('$1.Rnw', driver = cacheSweaveDriver)" &&
pdflatex --shell-escape $1.tex &&
open $1.pdf
}
Notice that I'm using the cacheSweave driver, which helps avoid constantly re-executing code sections that take a long time to run.
BTW, I'm also trying to switch over to Babel instead of Sweave; not sure which I'll end up using more often, but there are definitely aspects of Babel that I like.
The best solution is here: you create a new *.engine for TeXShop to use, then typeset using the shortcut or the 1 button.
http://cameron.bracken.bz/sweave-for-texshop
Cameron is also very responsive, so I highly recommend his solution.
If you are open to switching to a (paid) solution, TextMate has a Sweave plugin that takes you from .Rnw to PDF in one step: Sweave, typeset, and view. Combined with Skim, which can be configured to reload PDFs, it makes tweaking files pretty easy.
I had this same issue (I use Mac OSX) and I opted to download Eclipse Classic 3.6.2. and then installed the StatET plugin. It's a bit hairy to get set up but once you do this environment is nice because you can one-click compile your .Rwn Sweave document using pdflatex and set options for your favorite viewer so the .pdf automatically pops up when you compile like it does in TeXShop. You can do this in TeXShop as well, but TeXShop is lousy for debugging .Rnw files and it doesn't highlight the R-code in the .Rwn file. In Eclipse you can customize the syntax highlighting (not the greatest from the Texclipse end, but ok) so that you can easily distinguish between your R and LaTeX code. You can also launch the R console from within Eclipse and it has a graphical object browser. Anyway, I could go on. If you want details about how to get it all installed, message me.
Guess I'm late to the party on this, but I put together a webpage that documents my Sweave workflow based on Eclipse (with one-touch sweave):
http://www.stanford.edu/~messing/ComputationalSocialScienceWorkflow.html

Resources