Ho scritto una funzione per Windows. Ma probabilmente funziona anche su altri sistemi operativi.
Crea file temporanei in C: \ Users \ ... \ Documents \ Rview e li apre con browseURL(). È possibile aprire fino a 99 file in parallelo. Puoi facilmente scegliere quali dimnames dovrebbero essere visualizzati dall'argomento "nomi". La funzione aggiungerà 'before +, -, = in col/rownames quindi verrà visualizzato correttamente in Excel.
Personalmente preferisco la soluzione utilizzando Sys.getenv ("TMP") sull'uso di tempfile() perché tempfile() farà confondere la cartella dei file temporanei dopo un certo periodo di tempo.
Vedere le descrizioni degli argomenti nella parte superiore del codice per ulteriori informazioni.
# 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)
}
}
Ho scritto la domanda e la risposta come riferimento. Non sono sicuro che rispondere alla mia stessa domanda mi renda egocentrico o altruista. – geneorama
Se si desidera visualizzare i dati in un foglio di calcolo come un modulo invece di aprire Excel, è possibile utilizzare 'fix',' edit', o 'data.entry' – Dason
o' View' funziona anche – geneorama