# Downloading (graphics) files
The SWISH R interface defines the predicates below for downloading files. This can be combined with the normal R device manipulation for downloading images.
- [[r_download/0]]
- [[r_download/1]]
## Download a simple plot
The example illustrates the options using the sine function as defined below.
% Y is sin(X) for X in 0..Max
sin(Max, X, Y) :-
between(0, Max, X),
Y is sin(X*pi/180).
First, we create an [R dataframe](example/Rdataframe.swinb), load the "ggplot2" library and show the inline SVG.
r_data_frame(df, [x=X,y=Y], sin(360, X, Y)),
<- library("ggplot2"),
<- ggplot(data=df, aes(x=x, y=y)) + geom_line().
In the next examples we use r_download/0. This displays the graphics inline, but also provides a download button that allows you to download the SVG to your computer.
r_data_frame(df, [x=X,y=Y], sin(360, X, Y)),
<- library("ggplot2"),
<- ggplot(data=df, aes(x=x, y=y)) + geom_line(),
r_download.
And finally, we save the graphics to a device (in this example PDF) that we create explicitly. Note that r_download/0 calls `graphics.off()` before trying to download the generated files.
<- pdf("sine.pdf"),
r_data_frame(df, [x=X,y=Y], sin(360, X, Y)),
<- library("ggplot2"),
<- ggplot(data=df, aes(x=x, y=y)) + geom_line(),
r_download.
## Download a file
The example below shows how any file that is saved from R can be made available for download from SWISH.
<- write.csv(mtcars, file="cars.csv"),
r_download("cars.csv").