Windows用の関数を書きました。しかし、おそらく他のオペレーティング システムでも動作します。
C:\Users\...\Documents\Rview に一時ファイルを作成し、browseURL() で開きます。最大 99 個のファイルを同時に開くことができます。引数 "names" によって、どのディムネームを表示するかを簡単に選択できます。この関数は、col/rownames の +、-、= の前に ' を追加するため、Excel で適切に表示されます。
個人的には、 tempfile() を使用するよりも Sys.getenv("TMP") を使用するソリューションを好みます。これは、tempfile() が一定期間後に一時ファイル フォルダーを台無しにするためです。
詳細については、コードの上部にある引数の説明を参照してください。
# This function creates a CSV file from a data.frame/matrix and opens it with the default CSV-opening-program
# of the computer.
#
# x = data.frame/matrix
# names = dimnames to be saved in the file. "col"=colnames, "rowcol"=rownames&colnames, "row"=rownames, "no"=no dimnames
# nrows = maximum number of rows to be saved (for higher speed with large datasets)
# if n=-1, all rows will be displayed.-> see also the help for read.table()
# ncols = maximum number of columns to be saved (for higher speed with large datasets)
# folder = directory, where the temporary file should be saved.
# If NULL an accessible folder in C:/Users/.../Documents will be created automatically.
# quote = should quotes be written into the csv File? -> see also the help for write.table()
# na = how should NA values be displayed in the csv File? -> see also the help for write.table()
# openfolder = Should the folder with all temporary files be opened after having created the file?
view <- function(x, names=c("col","rowcol","row","no"), nrows=10000, ncols=1000, folder=NULL, quote=FALSE, na="NA", openfolder=FALSE, ...){
names <- match.arg(names)
if(is.null(dim(x))) {
x <- as.matrix(x)
}
if(is.null(colnames(x))) colnames(x) <- paste0("V",1:ncol(x))
if(nrows<0) nrows <- nrow(x)
if(ncols<0) ncols <- ncol(x)
# Shrink data.frame such that it can be saved & viewed faster.
nrows <- min(nrow(x), nrows)
if(nrows!=nrow(x)) x <- x[1:nrows,,drop=FALSE]
ncols <- min(ncol(x), ncols)
if(ncols!=ncol(x)) x <- x[,1:ncols,drop=FALSE]
# Define paths
# If is.null(folder), wird ein temporaerer Ordner im Windows-Dateisystem angelegt.
if(is.null(folder)) {
folder <- paste0(Sys.getenv("TMP"), "\\Rview")
suppressWarnings( dir.create(folder) )
}
# Wenn am Schluss des Pfades kein "/" angefuegt wurde, wird dies gemacht:
if( !substr(folder,nchar(folder),nchar(folder))%in%c("/","\\") ) folder <- paste0(folder, "\\")
pfad0 <- folder
name <- "Rview_tmp"
nr <- "01"
csv <- ".csv"
# Check if there are existing files in the folder
fil <- list.files(pfad0)
# If there are no files in the folder, use the default save path.
if(length(fil)==0){
pfad1 <- paste0(pfad0, name, nr, csv)
} else {
# Remove all files in the folder (if possible)
fil <- paste0(pfad0, fil)
suppressWarnings( try( file.remove( fil ) , silent=TRUE) )
fil <- list.files(pfad0)
# If there are no files anymore use the default save path.
if( length(fil)==0 ) {
pfad1 <- paste0(pfad0, name, nr, csv)
} else {
# If there are sill files, read out the number of the newest file (with the highest number)
ncharfil <- nchar(fil)
mx <- max( as.numeric( substr(fil,ncharfil-5,ncharfil-4) ) )
# Add 1 to the number of the file
mxpl1 <- as.character( mx+1 )
if(nchar(mxpl1)==1) mxpl1 <- paste0("0",mxpl1)
# Create a new path
pfad1 <- paste0(pfad0, name, mxpl1, csv)
}
}
# Rownames und colnames, die mit +, - oder = anfangen, mit ' am Anfang versehen, dass es von Excel richtig dargestellt wird
rn1 <- rownames(x)
cn1 <- colnames(x)
ind <- substr(rn1,1,1)%in%c("+","-","=")
if(any(ind)) rownames(x)[ind] <- paste0(" ",rn1[ind])
ind <- substr(cn1,1,1)%in%c("+","-","=")
if(any(ind)) colnames(x)[ind] <- paste0(" ",cn1[ind])
# Write CSV file & open.
if(names=="row") {
# If the first cell of the file is named "ID" Microsoft Excel warns that a SYLK file is opened. Therefore it is renamed.
if(rownames(x)[1]=="ID") rownames(x)[1] <- "lD"
write.table(x, file=pfad1, sep = ";", col.names=FALSE, row.names=TRUE, quote=quote, na=na, ...)
} else if (names=="col") {
# If the first cell of the file is named "ID" Microsoft Excel warns that a SYLK file is opened. Therefore it is renamed.
if(colnames(x)[1]=="ID") colnames(x)[1] <- "lD"
write.table(x, file=pfad1, sep = ";", col.names=TRUE, row.names=FALSE, quote=quote, na=na, ...)
} else if (names=="rowcol") {
write.table(x, file=pfad1, sep = ";", col.names=NA) # Colnames & Rownames
} else {
write.table(x, file=pfad1, sep = ";", col.names=FALSE, row.names=FALSE, quote=quote, na=na, ...)
}
browseURL(pfad1)
if(openfolder) {
Sys.sleep(1)
browseURL(folder)
}
}