2012-08-28 17 views
13

में आर डेटा.फ्रेम, वेक्टर, या डेटाटेबल को त्वरित रूप से देखें Excel में आप जल्दी से छोटी आर तालिका/वेक्टर ऑब्जेक्ट्स कैसे खोलते हैं? उदाहरण के लिएएक्सेल

मान लीजिए आप निम्नलिखित तीन वस्तुओं है कि आप Excel में देखना चाहते हैं:

## A data frame with commas and quotes 
df = data.frame(
    area = unname(state.x77[,'Area']), 
    frost = unname(state.x77[,'Frost']), 
    comments = "Ok for a visit, but don't want to live there", 
    challengeComments = c('"', '""')) 
row.names(df) = state.name 
df = df[1:10, ] 
df['California', 'comments'] = "Would like to live here" 

## A Matrix 
mat = matrix(rnorm(100), 10) 

## A Vector 
v = 1:10 
+0

मैंने प्रश्न और उत्तर को संदर्भ के रूप में लिखा था। मुझे यकीन नहीं है कि मेरे अपने प्रश्न का उत्तर देने से मुझे उदासीन या निस्संदेह बना दिया जाता है। – geneorama

+0

यदि आप एक्सेल खोलने के बजाय फ़ॉर्म जैसे स्प्रैडशीट में डेटा देखना चाहते हैं तो आप 'फिक्स',' एडिट ', या' data.entry' – Dason

+0

या 'व्यू' का उपयोग कर सकते हैं – geneorama

उत्तर

13

मुझे लगता है कि कार्य को पूरा करने के लिए इस समारोह में लिखा था। मैं इसे "temp फ़ाइल लिखें", या "wtf" कहता हूं। यह केवल विंडोज़ पर काम करता है यदि आपके पास एक्सेल से जुड़े सीएसवी फाइलें हैं।

आप विभिन्न ऑपरेटिंग सिस्टमों को अपनाने के तरीके को देखने के लिए पीबीएसमोडलिंग :: ओपनफाइल में कोड देख सकते हैं।

wtf = function (x) { 
    tempFilePath = paste(tempfile(), ".csv") 
    tempPath = dirname(tempFilePath) 
    preferredFile = paste(deparse(substitute(x)), ".csv", sep = "") 
    preferredFilePath = file.path(tempPath, preferredFile) 

    if(length(dim(x))>2){ 
    stop('Too many dimensions') 
    } 
    if(is.null(dim(x))){ 
    x = as.data.frame(x) 
    } 
    if (is.null(rownames(x))) { 
    tmp = 1:nrow(x) 
    }else { 
    tmp = rownames(x) 
    } 
    rownames(x) = NULL 
    x = data.frame(RowLabels = tmp, x) 
    WriteAttempt = try(
    write.table(x, file=preferredFilePath, quote=TRUE, sep=",", na="", 
       row.names=FALSE, qmethod="double"), 
    silent = TRUE) 
    if ("try-error" %in% class(WriteAttempt)) { 
    write.table(x, file=tempFilePath, , quote=TRUE, sep=",", na="", 
       row.names=FALSE, qmethod="double") 
    shell.exec(tempFilePath) 
    } else { 
    shell.exec(preferredFilePath) 
    } 
} 


wtf(df) 
wtf(mat) 
wtf(v) 

यदि आप एक ही वस्तु को कई बार खोलते हैं, तो यह अभी भी त्रुटि से निपटने के लिए धन्यवाद काम करेंगे, लेकिन यह एक गंदा अस्थायी नाम होगा।

wtf(df) 
df$MoreData = pi 
wtf(df) 
+12

फ़ंक्शन के नाम के लिए +1 –

+0

क्या आपने इसका परीक्षण विंडोज के अलावा अन्य ओएस पर किया है? इसके अलावा: एक्सेल के एक उदाहरण के एकाधिक विंडोज़ में .csv फ़ाइलों को खोलने के बजाय, एक्सेल के कौन से संस्करण और एक्सेल के संस्करण के आधार पर यह एक्सेल ऐप के कई उदाहरण शुरू कर सकता है। यह आपकी गलती नहीं है (यह रेडमंड है), लेकिन मुझे यकीन है कि कुछ चिकना सिस्टम कॉल है जो उन समस्याओं के आसपास हो सकती है। –

+0

पीबीएसमोडलिंग से "ओपनफाइल" में आपके द्वारा उपयोग किए जा रहे ओएस के परीक्षण के कुछ चालाक तरीके हैं। मुझे लगा कि आर-एक्सेल लिंक की इच्छा ज्यादातर चीजें हैं जो विंडोज उपयोगकर्ता चाहते हैं। – geneorama

0

write.csv(x, "clipboard") का उपयोग करें और एक्सेल में पेस्ट करें।

+0

जब तक मैंने कोशिश नहीं की तब तक मुझे इस विचार से प्यार था। दुर्भाग्य से, यह प्रत्येक पंक्ति के पहले सेल में पूरी रेखा चिपकाता है। यह स्वचालित रूप से प्रत्येक मान को अपने सेल में नहीं डालता है। – Farrel

+0

मुझे पहले पेस्ट के लिए "टेक्स्ट टू कॉलम" का उपयोग करना था, और सही विभाजक सेट करना था। उसके बाद, यह मेरे लिए ठीक काम करता है (एक्सेल 2007)। बाद के पेस्ट को "कॉलम टू टेक्स्ट" की आवश्यकता नहीं है। –

+0

मेरे लिए काम करता है, हालांकि मैं ओओ कैल्क का उपयोग करता हूं। –

1

बेशर्म विज्ञापन के लिए क्षमा करें ... तुम मेरे पैकेज की कोशिश कर सकते http://cran.r-project.org/web/packages/excel.link/index.html ऐसा लगता है कि:

library(excel.link) 
xlrc[a1]=df 

यह Omegahat RDCOMClient पैकेज पर निर्भर करता है, तो यह जरूरी है कि यह स्रोत से स्थापित:

install.packages("RDCOMClient", repos = "http://www.omegahat.org/R") 
install.packages("excel.link", repos = "http://cran.at.r-project.org",type="source") 
3

मैंने लिबर ऑफिस कैल्क या एक्सेल में फ़ाइलों को खोलने के लिए एक फ़ंक्शन लिखा था। See here for details

view <- function(data, autofilter=TRUE) { 
    # data: data frame 
    # autofilter: whether to apply a filter to make sorting and filtering easier 
    open_command <- switch(Sys.info()[['sysname']], 
          Windows= 'open', 
          Linux = 'xdg-open', 
          Darwin = 'open') 
    require(XLConnect) 
    temp_file <- paste0(tempfile(), '.xlsx') 
    wb <- loadWorkbook(temp_file, create = TRUE) 
    createSheet(wb, name = "temp") 
    writeWorksheet(wb, data, sheet = "temp", startRow = 1, startCol = 1) 
    if (autofilter) setAutoFilter(wb, 'temp', aref('A1', dim(data))) 
    saveWorkbook(wb,) 
    system(paste(open_command, temp_file)) 
} 
+0

मेरे पास इस बहुत ही आसान फ़ंक्शन के लिए दो फ़िक्स हैं, यह एक dplyr-datasets को संभालने में सक्षम बनाता है, और दूसरा निर्दिष्ट करता है कि इस फ़ंक्शन के भीतर XLConnect फ़ंक्शन का उपयोग किया जाना चाहिए XLConnect पैकेज से "::" -ऑपरेटर। ऐसा इसलिए है क्योंकि मुझे विवादित पैकेजों में समस्याएं आई हैं। यदि आपको इस फ़ंक्शन के साथ समस्याएं हैं, तो जेरोमी suplies के लिंक में टिप्पणियां-फ़ील्ड देखें। – emilBeBri

1

मैं इस अक्सर उपयोग एक्सेल में डेटा की एक तालिका चिपकाने के लिए:

write.table(x, "clipboard", row.names=F, sep='\t') 

और आर में एक्सेल से एक (उप) तालिका कॉपी करने के लिए, यह करने के (टेबल संभालने में शीर्षलेख पंक्ति है):

read.csv('clipboard', sep='\t') 
+0

हां, मुझे यह चाल पसंद है और टैब डिलीमीटर सही एक्सेल में पेस्ट करते हैं। समस्या स्मृति सीमा है, और लिनक्स समर्थन की कमी है। मेरे पैकेज (जेनोरामा) में क्लिपर है और एक्सेल से चिपकने के लिए क्लिप किया गया है (एक्सेल से आर में वर्चुअल रूप से कोई मेमोरी सीमा बीटीडब्लू नहीं है)। https://github.com/geneorama/geneorama/blob/master/R/clipper.R https://github.com/geneorama/geneorama/blob/master/R/clipped.R – geneorama

0

मैंने विंडोज़ के लिए एक फ़ंक्शन लिखा था। लेकिन शायद यह अन्य ऑपरेटिंग सिस्टम पर भी काम करता है।

यह सी: \ उपयोगकर्ता \ ... \ दस्तावेज़ \ Rview में अस्थायी फ़ाइलें बनाता है और उन्हें browsURL() के साथ खुलता है। आप समानांतर में 99 फाइलें खोल सकते हैं। आप आसानी से चुन सकते हैं कि तर्क "नाम" द्वारा कौन से dimnames प्रदर्शित किए जाने चाहिए। फ़ंक्शन '+, -, = को कॉल/राउनम्स में पहले जोड़ देगा ताकि यह 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) 
    } 
}