R/View.excel.r

View.excel = function (x) {

  if(nrow(x)>200*10^3){stop("This data.frame is too big for Excel")}
  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", dec = ","),
    silent = TRUE)
  if ("try-error" %in% class(WriteAttempt)) {
    write.table(x, file=tempFilePath, , quote=TRUE, sep=";", na="",
                row.names=FALSE, qmethod="double", dec = ",")
    shell.exec(tempFilePath)
  } else {
    shell.exec(preferredFilePath)
  }
}
nicolassoarespinto/rcode documentation built on May 23, 2019, 5:09 p.m.