At the end of a run, files found in the /results
folder are collected and made available in the Reproducibility pane, where users can view and download them. Files saved elsewhere will be discarded at the end of the run.
Published results will be labeled as such in the reproducibility pane:
Why don't my graphics just show up?
Code Ocean has no built-in display. This is called headless execution; code executed this way is said to be run in batch mode. Plots, figures, and other graphics must be saved explicitly to the /results
folder.
In MATLAB:
Use either print
or saveas
. The following snippet will save the same plot twice:
figure;
plot(1:10)
saveas(gcf, '../results/fig1.png')
print('../results/plot', '-dpdf')
In Python:
We most commonly see matplotlib
for saving python graphics on Code Ocean. You can install matplotlib
via a built-in package manager. (You may need to set the backend to Agg
first.)
import matplotlib
matplotlib.use('Agg')
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)
savefig('../results/plot.png')
In R:
In base R, you must call a graphics device, plot your graph, and then save it. ggplot2
has different syntax.
library(ggplot2)
## as png
png('../results/fig1.png')
g <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point(); g
dev.off()
## as pdf
pdf('../results/fig1.pdf')
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
dev.off()
## 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 ln -s ../results ./figures
in a shell script (typically called run.sh
on Code Ocean) before you run your analyses.
If you are saving png files to the home directory (and if you have no png files there at the outset), write mv *.png ../result
in a shell script. 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
, for example, mv *.{png,jpg,tiff} ../results
.
Creating sub-directories within results
Each capsule begins with a shell script called run.sh
. In this file, you can use the command mkdir -p
to create sub-folders within /results
. If you wished to have all figures in one subfolder and all tables in another, at the end of your analysis, you would write something like:
mkdir -p ../results/figs && mv ../results/*.png ../results/figs
mkdir -p ../results/tables && mv ../results/*.txt ../results/tables
Alternatively, you can save the results of different analyses into different subfolders, as you will see in the run.sh
script of this capsule.