1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function(fl=file.choose(),sep = NULL,header=TRUE,skip = 0, strip.white = FALSE,width = NULL,
col.names = NULL, nrows = NULL, stringsAsFactors=FALSE,
quote = "", comment.char="#",...){
if (grep(".xls|.xlsx",fl)){
library(xlsx)
data <-read.xlsx(fl,1)
}
else if(is.null(sep) & is.null(width) & !strip.white){
data <- read.csv(fl,header=header,
stringsAsFactors = stringsAsFactors,
quote = quote,comment.char=comment.char)
}
else if(is.null(width)){
data <- read.table(fl,header=header,sep=sep,nrows = nrows,
stringsAsFactors = stringsAsFactors,skip = skip ,
strip.white = strip.white,quote = quote,comment.char=comment.char)
}
else{
data <- read.fwf(fl, width, header = header, skip = skip,
col.names = col.names,
strip.white=strip.white,
stringsAsFactors = stringsAsFactors)
}
return(as.data.frame(data))
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function(data = NULL, file = "",row.names=FALSE, col.names = TRUE, ascii= FALSE){
name <- strsplit(file,".",fixed = T)[[1]][1]
type <- tolower(strsplit(file,".",fixed = T)[[1]][2])
if (type == "csv"){
write.csv(data, file, row.names=row.names)}
else if(type == "rdata"){
save(data, file = file, ascii=ascii) #! save(data, data1, file="data.RData")
}
else if(type == "rds"){
saveRDS(data, file, ascii=ascii)
}
else if(type == "rdmpd"){
dump(data,file)
}
}

WRITE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
> path <- "test"
> dir.create(path) ## 创建一个目录
> setwd(path) ## 变更工作目录
> setwd("..") ## 变更目录至上一级
> unlink(path, recursive = TRUE) ## 删除目录
> dir() ##查看当前目录
> ## 保存当前对话中的mat1,使用最大的压缩比
> save("mat1", file = "mat1.rda", compress = "bzip2", compression_level = 9)
> file.info("mat1.rda")$size ##文件的大小
> object.size(mat1)
dir
basename, dirname, tools::file_ext
file.path
path.expand, normalizePath
file.copy, file.create, file.remove, file.rename, dir.create
file.exists, file.info
tempdir, tempfile
download.file, library(downloader)

PDF

PDF is a vector file format. Vector files are generally preferred for print output because the resulting output can be scaled to any size without pixelation. The size of a vector file is usually smaller than the corresponding bitmap file, except in cases where there are many objects. (For example, a scatter plot with thousands of points may result in a very large vector file, but a smaller bitmap file.)

1
2
3
4
5
pdf("plots.pdf")
plot(...)
plot(...)
dev.off()

PDF’s are 7x7 inches by default, and each new plot is on a new page. The size can be changed:

1
2
3
4
5
6
# 6x3 inches
pdf("plots.pdf", width=6, height=3)

# 10x6 cm
pdf("plots.pdf", width=10/2.54, height=6/2.54)

If you want to edit your file in a vector editor like Inkscape or Illustrator, some of the plotting point objects might look like letters instead of circles, squares, etc. To avoid this problem:

1
2
pdf("plots.pdf", useDingbats=FALSE)

SVG

SVG is another vector format. The default settings for svg() doesn’t allow for multiple pages in a single file, since most SVG viewers can’t handle multi-page SVG files. See the PNG section below for outputting to multiple files.

1
2
3
4
svg("plots.svg")
plot(...)
dev.off()

SVG files may work better with vector-editing programs than PDF files.

PNG/TIFF

PNG and TIFF are bitmap (or raster) formats. If they are magnified, the pixels may be visible.

1
2
3
4
5
png("plot.png")
# or tiff("plot.tiff")
plot(...)
dev.off()

By default, the graphs are 480x480 pixels in size, at a resolution of 72 dpi (6.66x6.66 inches).

Increasing the resolution will increase the size (in pixels) of the text and graph elements. This occurs because the size of these elements is relative to the physical dimension of the graph (e.g., 4x4 inches), not the pixel dimension of the graph. For example, a 12 point font is 12/72 = 1/6 inch tall; at 72 dpi, this is 12 pixels, but at 120dpi, it is 20 pixels.

This would create a graph that is 480x240 pixels at 120dpi, equivalent to 4x2 inches.

1
2
3
4
5
6
7
8
png("plot.png", width=480, height=240, res=120)
# jpeg("output.jpg")
# bmp("output.bmp")
# pdf("output.pdf")
# win.metafile("output.wmf")
# postscript("output.ps")
plot(...)
dev.off()

If you want to make more than one graph, you must either execute a new png() command for each one, or put %d in the filename:

1
2
3
4
png("plot-%d.png")
plot(...)
dev.off()

This will generate plot-1.png, plot2.png, and so on.

For import into PDF-incapable programs (MS Office)

Some programs which cannot import PDF files may work with high-resolution PNG or TIFF files. For example, Microsoft Office cannot import PDF files. For print publications, you may be required to use 300dpi images.

1
2
3
4
5
6
# Make a 6x6 inch image at 300dpi
ppi <- 300
png("plot.png", width=6*ppi, height=6*ppi, res=ppi)
plot(...)
dev.off()

ggplot2

If you make plots with ggplot2 in a script or function, you must use the print() command to make the graphs actually get rendered.

1
2
3
4
5
6
7
8
9
10
# This will do nothing
pdf("plots.pdf")
qplot(...)
dev.off()

# This will do the right thing
pdf("plots.pdf")
print(qplot(...))
dev.off()

To save a ggplot2 graph from the screen to a file, you can use ggsave().

1
2
3
4
5
6
7
ggsave("plot.pdf")

ggsave("plot.pdf", width=4, height=4)

# This will save a 400x400 file at 100 ppi
ggsave("plot.png", width=4, height=4, dpi=100)

Saving a graph from the screen

If you have a graph on the screen, you can save it to a bitmap file.

This will save an exact pixel-for-pixel copy of what’s on screen, but it will probably only work in Linux and on Macs that use X11 for R graphing:

1
2
3
4
5
# Make a plot on screen
plot(...)

savePlot("myplot.png")

This will save the current graph from the screen, but it re-renders it for the device, which may have different dimensions, so it won’t look exactly the same unless you specify the exact same size in pixels.

1
2
3
4
5
6
7
8
9
10
11
12
# Make a plot on screen
plot(...)

dev.copy(pdf,"myplot.pdf", width=4, height=4)
dev.off()
# Same as doing:
# pdf("myplot.pdf", width=4, height=4)
# plot(...)
# dev.off()

dev.copy(png,"myplot.png", width=400, height=400)
dev.off()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
zz <- file("test.dat", "wb")
writeBin(1:10, zz)
writeBin(pi, zz, endian = "swap")
writeBin(pi, zz, size = 4)
writeBin(pi^2, zz, size = 4, endian = "swap")
writeBin(pi+3i, zz)
writeBin("A test of a connection", zz)
close(zz)

fl = file("test.dat", "rb")
readBin(fl, integer(), endian = "little")
# [1] 1
readBin(fl, integer(), n = 4, endian = "little")
# [1] 2 3 4 5
readBin(fl, integer(), n = 2, size = 4, endian = "little")
# [1] 6 7
readBin(fl, numeric(), 1, endian = "swap")
# [1] 3.79e-270

REFERENCES