trouble when exporting notebook to html on Jupyter with R kernel - jupyter

The following code works fine when writing an ipython notebook equipped with the R kernel. Unfortunately, the export to html fails for the second barplot (both with the embedded option of jupyter and manual use of nbconvert).
library(NLP)
library(tm)
# here I used the EBook of Ulysses, by James Joyce, but any text file can fit
# the text is available here: https://www.gutenberg.org/cache/epub/4300/pg4300.txt
book <- readLines("pg4300.txt", encoding="UTF-8")
corpus <- Corpus(VectorSource(book))
corpus <- tm_map(corpus, content_transformer(tolower))
corpus <- tm_map(corpus, removeNumbers)
corpus <- tm_map(corpus, removePunctuation)
dtm <- TermDocumentMatrix(corpus)
m <- as.matrix(dtm)
freq <- rowSums(m)
freq.sorted <- sort(freq, decreasing=TRUE)
# first barplot with stop words (ok for both notebook and export)
barplot(freq.sorted[1:50], xlab="Word", ylab="Frequency", las=2)
corpus.sw <- tm_map(corpus, removeWords, stopwords('english'))
dtm.sw <- TermDocumentMatrix(corpus.sw)
m.sw <- as.matrix(dtm.sw)
freq.sw <- rowSums(m.sw)
freq.sw.sorted <- sort(freq.sw, decreasing=TRUE)
# second barplot without stop words (ok on ipython notebook but fail when exporting)
barplot(freq.sw.sorted[1:50], xlab="Word", ylab="Frequency", las=2)
What is really weird, it is that the first barplot is well exported, but not the second one whereas the process is exactly the same (showing the 50 top words).
Here is my configuration:
macosx 10.11.2 el capitan
jupyter 4.0.6
ipython 4.0.1
R version 3.2.2
Thank you,
Julien

As this seems to be caused by how the svg plots are embedded in the html page, you can probably work around this issue by removing SVG plots from the plotting options:
options(jupyter.plot_mimetypes = c("text/plain", "image/png" )) # no more svg...

Related

PercTable {DescTools} - prevent output text wrapping

My goal is to export PercTable output in a clean / HTML like format as JPG or CSV import into a presentation.
I get the "raw" table I want with this code:
Library(DescTable)
PercTable(syn_gender ~ recommend, data=dfXtab, rfrq="010", expected=TRUE)
It outputs as shown in graphic.
Screen snapshot for space saving
How can I export this output without the table wrapping the last 2 column?
Thank you - newbie.
The PercTable print routine uses the console width (getOption("width")) to define the table breaks. So the first solution is to simply enlarge the console width and print again.
Another option for output if you're on Windows is to use the Office export (package RDCOMClient needed):
library(DescTools)
tt <- PercTable(table(d.pizza$operator,
cut(d.pizza$temperature, breaks=6)))
wrd <- GetNewWrd()
ToWrd(tt$ftab)
would yield:

How to make some keyword in vscode to be italicized

So my friend & I are programming in Python. He has a code in python so here it goes:
Somewhere in between some keywords like in def, there is some italicization.
How can I get this too?
I am currently using Arch Linux with vscode 1.61.2 with Jetbrains Mono font.
Here is my current behaviour
Like in
def change(x, y):
"Change snake direction."
aim.x = x
aim.y = y
he has def italicized

How to render Julia dataframes with VSCode Jupyter extension?

In a julia notebook opened with vscode the dataframe output format is latex but the result is not correctly rendered:
> using DataFrames
> df = DataFrame(a=1:3)
\begin{tabular}{r|c}
& a\\
\hline
& Int64\\
\hline
1 & 1 \\
2 & 2 \\
3 & 3 \\
\end{tabular}
Is there a way to configure jupyter extension to get a well formatted output for julia dataframes?
I'm actually using vscode 1.53.2 and julia 1.6.0-rc1.
I update the show method like this:
using DataFrames
Base.show(io::IO, ::MIME"text/latex", df::AbstractDataFrame) = show(df, backend=:html, show_row_number=true, highlighters=:tf_html_default )
Then we can see the well formatted output from this line:
df = DataFrame(a=1:3)
Try VSCodeServer.vscodedisplay(df), for an example:
using DataFrames
df = DataFrame(x=1:4,y=rand(4),z='a':'d')
VSCodeServer.vscodedisplay(df)
Here is how it works:
Unfortunately, Przemyslaw's answer doesn't actually display the table inline in the Jupyter notebook. It seems that the VSCode implementation requests LaTeX output which it asks MathJax to display. This seems to be broken for DataFrames.
An alternative which does display the table inline is the PrettyTables package.
> using PrettyTables
> pretty_table(df, backend=:html)

How to make a figure caption in Rmarkdown?

I am thinking about writing my thesis with rmarkdown and latex. I'm getting the hang of how it all works, however, when I try to add a figure (not an R plot) to the text and render it to pdf, the caption and in-text reference dissappear.
This is the code snippet I use to add a figure:
---
title: "Untitled"
output: pdf_document
---
see figure \ref{fig1}.
![picture \label{fig1}](figure1.png)
This is what knitr creates:
This is what pandoc creates:
Question:
How do I make figure captions and in-text references to those figures in Rmarkdown that will display when rendered to pdf?
OR
How do I tell pandoc what Rmarkdown is so it will render R code and plots?
Please see the documentation of R Markdown for PDF output, and in particular, look for fig_caption. Figure captions are turned off by default in R Markdown, and you have to turn them on (fig_caption: true). You can also find this setting from the gear button on the toolbar of RStudio IDE.
Update: please check https://github.com/yihui/knitr/issues/1063.
Question: How do I make figure captions and in-text references to those figures in Rmarkdown that will display when rendered to pdf?
To get the cross-reference in the PDF produce by LaTeX you need to run LaTeX more than once. Some LaTeX IDE does it for you.
knitr is only running LaTeX once and that is the reason that you only get ??. To confirm that this was the problem I ran
library(knitr)
knitr()
in R that returned
see figure \ref{fig1}.
\begin{figure}[htbp]
\centering
\includegraphics{imagem.jpg}
\caption{picture \label{fig1}}
\end{figure}
which is a valid LaTeX code.
How do I tell pandoc what Rmarkdown is so it will render R code and plots?
Pandoc only understand Markdown (not RMarkdown). First you have to call knitr to generate the Markdown from the RMarkdown and after it call Pandoc to convert the Markdown to LaTeX.
I just found a very useful solution here.
First, include the following chunk:
```{r functions, include=FALSE}
# A function for captioning and referencing images
fig <- local({
i <- 0
ref <- list()
list(
cap=function(refName, text) {
i <<- i + 1
ref[[refName]] <<- i
paste("Figure ", i, ": ", text, sep="")
},
ref=function(refName) {
ref[[refName]]
})
})
```
After, we can add the caption of the figure/table in the figure chunk options like:
```{r, fig.cap=paste("Your caption.")}
See that fig.cap works better with paste.
In Rmarkdown
for markdown figures
![caption \label{labelx}](link_to_figure, or path of figure){figure dimensions}
# how to cite markdown figure
\ref{labelx}
for latex figures
Add following in the figure environment of latex which starts with
\begin{figure}
includegraphics[]{/path to figure}
\label{label}
\end{figure}
# how to cite latex
\ref{label}
for bookdown
add chunk names as label names like
{r label2,echo=F}
# to cite by using bookdown use
\#ref(fig:label2)
similarly for tab we cite table as \#ref(tab:table_label)
For captioner package
add a chunk at the top of rmarkdown document
tab_cap <- captioner(prefix = "table")
fig_cap <- captioner(prefix = "figure")
where prefix is the prefix for cross referencing a figure.
add this in chunk options
{r fig_cap("labelforcaptioner","caption for captioner figure"}
note that fig_cap is similar to fig_cap in chunk code for captioner.
and to cite use
`r fig_cap("label4",display='cite')

image does not display in ipython

The image does not load if it is part of a while loop. For e.g. the following works as expected:
from IPython.display import Image
Image(filename='someimage.jpg')
But this does not work:
while True:
Image(filename='someimage.jpg')
break
update:
How do I display several images from a list?
This works fine here:
from IPython.display import display, Image
path1 = "/some/path/to/image1.png"
path2 = "/some/path/to/image2.png"
for path in path1, path2:
img = Image(path)
display(img)
Ok, I have exactly the same problem as apler details above. I am going thru a detailed example, where IPython is used to create an image. After several attempts, I have successfully displayed a .jpg image from a file, and confirmed Mac OSX Yosemite can use Python+PIL to display an image.
I had to uninstall PIL, and use Pillow instead, in order for the libjpeg library to be recognized correctly. The Python code creates an image - but it will not display the image.
I try to create the image with:
f = BytesIO()
PIL.Image.fromarray(a).save(f, 'jpeg')
clear_output(wait = True)
display(Image(data=f.getvalue()))
What I see when I run the little program in Python is:
<IPython.core.display.Image object>
In :
This In : looks to be an IPython prompt.
It appears that the code example I am working through is specifically designed to use IPython Notebook which is not the same as IPython, or interactive Python.
To use the code above, which is from the Google TensorFlow tutorial, one needs to configure an "IPython Notebook Server" and run something called "Jupyter". The documentation for Jupyter stuff is at:
http://jupyter-notebook.readthedocs.io/en/latest/
The point apler makes is good. We should just be able to use plain Python to create a .jpg file, and display it on a Mac running OSX. I've pulled some code togther to show how to do this. It creates a .jpg image, writes it out to a file, and then displays it using Pillow version of PIL. (Note: I had to use pip to uninstall original PIL, then pip install Pillow, and I picked up the "libjpeg" library as a .dmg file from the Ethan.Tira-Thompson.com/Mac_OS_X_Ports.html site). You also need numpy and scipy.misc modules.
#
# --- Make+Show immediately a simulated Moire pattern as a .jpg file
#
# --- start Python and import this file with: import MakeShowMoire
# --- or use: execfile ("MakeShowMoire.py")
#
# --- import modules we need
from PIL import Image, ImageDraw
import numpy as np
import scipy.misc
#
# --- Ok, lets go..
width = 1020
height = 710
channels = 3
#
img = np.zeros((height, width, channels), dtype=np.uint8)
xx, yy = np.mgrid[:height, :width]
circle = (xx - 100) ** 2 + (yy - 100) ** 2
for y in range(img.shape[0]):
for x in range(img.shape[1]):
r, g, b = circle[y][x], circle[y][x], circle[y][x]
img[y] [x] [0] = r
img[y] [x] [1] = g
img[y] [x] [2] = b
#
# --- now, we have made the image, lets save it
scipy.misc.imsave("testimg.jpg", img)
#
# --- Since we have saved it, we should be able to display it
image = Image.open("testimg.jpg")
image.show()
Save this file as MakeShowMoire.py. To run this, start a terminal session, and run Python. I am using Python 2.7.10 [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang 600.0.39)] on darwin, and Mac OSX is 10.10.5 Yosemite. When Python runs, just enter:
>>> execfile ("MakeShowMoire.py")
And the example should display on your Mac, in a window called "tmpblahblah.BMP", which will be pitched when you close the window, but your testimg.jpg is retained, of course.
I was using "import MakeShowMoire" at Python prompt to run the file, but the namespace does not map to the interactive Python session. If you use the execfile function, you can re-display the file contents with image.show() again. Hope this is useful.
When working in IPython notebooks, the interpreter will give a response when typed as a separate command.
So for example:
>>>>a = 5
>>>>a
5
But if we were to do the same thing in a function or loop, we would not get the response.
So if you want to display images from within a loop/function, you will have to use the display function (IPython.display.display)
from IPython.display import display, Image
for image_path in images: # images is a list of paths
display(Image(image_path))
So the Image function is returning an image. When in the interpreter, it will get displayed, but otherwise it will not.
To ensure that it is displayed, we use the display function.
This is similar to the print command. When in the interpreter, it is not necessary to use the print command to view variables, whereas while running a program it is.
I tried the chosen answer, and just about everything else under the sun, but no matter what I tried, it would only display the last photo. I was finally able to get it working with matplotlib.
import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def display_photo(path):
print("File: {}".format(path))
img = mpimg.imread(path)
imgplot = plt.imshow(img)
plt.show()
for path in glob.iglob('my_photos/*'):
display_photo(path)
Note: The image displays in a different window - image viewer. If using QtConsole, one could set the display to inline using %matplotlib inline
Hope this helps.
Imports and that function can be rewritten as below. It perfectly opens windows photo viewer with the result fractal image
import tensorflow as tf
import numpy as np
from PIL import Image
from io import BytesIO
from IPython.display import display
def DisplayFractal(a, fmt='jpeg'):
a_cyclic = (6.28*a/20.0).reshape(list(a.shape)+[1])
img = np.concatenate([10+20*np.cos(a_cyclic),
30+50*np.sin(a_cyclic),
155-80*np.cos(a_cyclic)], 2)
img[a==a.max()] = 0
a = img
a = np.uint8(np.clip(a, 0, 255))
Image.fromarray(a).show()