At the end of a run, files found in the /results  folder are collected and made available in the Results pane, where users can view and download them. (Files saved elsewhere will be discarded at the end of the run.)

Why don't my graphics just show up?

Code Ocean has no built-in display. Plots, figures, and other graphics must be saved explicitly to the /results  folder. 


Use either print or saveas . The following snippet will save the same plot twice:

saveas(gcf, '../results/fig1.png')
print('../results/plot', '-dpdf')

In Python:

We most commonly see matplotlib for saving graphics on Code Ocean.  (You will need to set the backend to Agg  first -- See this help article for more.)

import matplotlib
from matplotlib.pyplot import plot, savefig, figure
from numpy import linspace, pi, sin

x = linspace(0, 4 * pi)
y = sin(x)
fig = figure()
plot(x, y)

In R:

In base R, you must call a graphics device, plot your graph, and then save it.  ggplot2  has different syntax.

## as png
g <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point(); g

## as pdf
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()

## ggsave
g <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
ggsave('../results/fig1.tiff', g)

Linking or moving files to /results

If your code is already structured to save figures somewhere besides /results , use the Linux commands mv or ln -s

To move figures currently saving to a folder called figures , write the following in a  script: ln -s ../results ./figures .

If you are saving .png files to the home directory (and if you have no .png files there at the outset), write the following in a  script: mv *.png ../results. The * character is a wildcard and the command will apply to all files ending in.png.

You may also use Brace Expansion to move multiple types of files to /results: mv *.{png,jpg,tiff} ../results .

Creating sub-directories within results

Each capsule begins with a shell script called . In this file, you can use the command mkdir -p to create sub-folders within /results .  For instance, if you wanted to have all figures in one subfolder and all tables in another, at the end of your analysis, write something like:

mkdir -p ../results/figs && mv ../results/*.png ../results/figs
mkdir -p ../results/tables && mv ../results/.*.txt ../results/tables


Did this answer your question?