Unable to read Excel file in R-studio - r

install.packages('read_excel')
library(xlsx)
read.xlsx(path, sheet = "C:/Users/Vikas Singh/Desktop/Data KNN.xlsx",sheet=2 , range = NULL, col_names = TRUE, col_types = NULL, na = "", trim_ws = TRUE, skip = 0, n_max = Inf)
Even after using the above code I'm getting an error message no function read.xlsx

The read.xlsx is a function from the openxlsx package.
You need install and load the readxl package.
install.packages("readxl")
library(readxl)
Then try the following code:
read_excel(path = "C:/Users/Vikas Singh/Desktop/Data KNN.xlsx", sheet = 2, col_names = TRUE, col_types = NULL, na = "", skip = 0)

You have to install the openxlsx package. Please read the according documentation here. Try out the following code:
install.packages("openxlsx")
library(openxlsx)
read.xlsx(xlsxFile = "C:/Users/Vikas Singh/Desktop/DataKNN.xlsx", sheet= <sheet_index> , <... all_your_other_arguments>)

Related

Why R reads CSV file differently

I am using
myCounts<-read.csv("myCounts.csv", header = TRUE, row.names = 1, sep = ",")
and
Book4 <- read_delim("Book4.csv", delim = ";",
escape_double = FALSE, trim_ws = TRUE)
to read two csv files. But read.csv and read.delim is pressing them differently.
Could you please explane how to read in book4 data in the same structure of myCounts data?
I tried following, it works.
df<-read.delim("~/Documents/sample.csv" ,sep = ";",row.names = 1)

data.table::fread error when converting MAF files to data table

I want to merge the 50 MAF files with the sample information so that I can read it as a data.table and subset it.
library(maftools)
# Load MAF files
maf = system.file("extdata", list.files(path="mafs/"), package="maftools")
# Load sample information
si <- system.file("extdata", "sample-information.tsv", package="maftools")
d = read.maf(maf=maf, clinicalData=si)
Traceback:
Error in data.table::fread(file = maf, sep = "\t", stringsAsFactors = FALSE, :
File '' does not exist or is non-readable. getwd()=='C:/Users/User/Documents/VanAllen'
> traceback()
3: stop("File '", file, "' does not exist or is non-readable. getwd()=='",
getwd(), "'")
2: data.table::fread(file = maf, sep = "\t", stringsAsFactors = FALSE,
verbose = FALSE, data.table = TRUE, showProgress = TRUE,
header = TRUE, fill = TRUE, skip = "Hugo_Symbol", quote = "")
1: read.maf(maf = maf, clinicalData = si)
1: data.table::fread(input = maf)
Maftools documentation:
https://www.bioconductor.org/packages/release/bioc/manuals/maftools/man/maftools.pdf
When I run your code, maf indeed points to no character ( "" ), which of course cannot be read by fread. However when I try
fread("R/x86_64-pc-linux-gnu-library/3.6/maftools/extdata/brca.maf.gz")
it works as expected.

Saving .txt file with write_delim and here package into subdirectory in Rmarkdown

The goal is to save a .txt file into a subdirectory (~/output/data/file.txt) within my project folder by using the here package in rmarkdown. It should be tab-delimited and have no col names.
This is my code that fails:
readr::write_delim(here::here(file, "output", "data", "file.txt",
delim = "\t", col_names = FALSE))
Here is a reproducible example:
file <- data.frame(chromosome = c("chr1", "chr2", "chr3"),
start = c(10, 20, 30),
stop = c(100, 200, 300),
gene = c("geneA", "geneB", "geneC"))
This is the error I get:
Error in readr::write_delim(here::here(file, "output", "data", "file.txt", : is.data.frame(x) is not TRUE
I get it to work with this code, where I put in the whole path, but I want to avoid that:
readr::write_delim(file, "/whole/path/to/project/folder/output/data/file.txt",
delim = "\t", col_names = FALSE)
Any ideas on what is going wrong here?
Looks like I've figured it out myself...
write_delim(file, here("output", "data", "file.txt"),
col_names = FALSE, delim = "\t")
my order was wrong.

R readr function for previewing csv

I am looking for a function or workaround in readr or R base to "preview" the column types that read_csv will guess before actually importing the data.
I am working with several files about 60Mb size containing 51 columns and 160k rows so that would make it much easier to build the col_types specification for read_csv.
My excuses if it sounds like an obvious question. I found no answers in the forum to this specific issue and have only recently starting using dplyr. Thanks.
Went into the readr code and tried to do some surgery to use the read_csv function code but only as far as the spec is guessed.
getReaderSpec <- function (file, col_names = TRUE, col_types = NULL, locale = default_locale(),
na = c("", "NA"), quoted_na = TRUE, quote = "\"",
comment = "", trim_ws = TRUE, skip = 0, n_max = Inf,
guess_max = min(1000, n_max), progress = show_progress(),
skip_empty_rows = TRUE)
{
tokenizer <- readr:::tokenizer_csv(na = na, quoted_na = quoted_na,
quote = quote, comment = comment, trim_ws = trim_ws,
skip_empty_rows = skip_empty_rows)
name <- readr:::source_name(file)
file <- readr:::standardise_path(file)
if (readr:::is.connection(file)) {
data <- readr:::datasource_connection(file, skip, skip_empty_rows,
comment)
if (readr:::empty_file(data[[1]])) {
return(tibble::tibble())
}
}
else {
if (!isTRUE(grepl("\n", file)[[1]]) && readr:::empty_file(file)) {
return(tibble::tibble())
}
if (is.character(file) && identical(locale$encoding,
"UTF-8")) {
data <- enc2utf8(file)
}
else {
data <- file
}
}
spec <- readr:::col_spec_standardise(data, skip = skip, skip_empty_rows = skip_empty_rows,
comment = comment, guess_max = guess_max, col_names = col_names,
col_types = col_types, tokenizer = tokenizer, locale = locale)
readr:::show_cols_spec(spec)
invisible(spec)
}
myspec <- getReaderSpec("someexample.csv")

hello i want create tsne plot by my dataset in R . but when i write commands i get this error

library(Rtsne)
setwd("n/g")
expression_data <- read.table(file = "zdata.matrix.xlsx", row.names = 1, sep=',', header = T)
meta_data <- read.table(file = "atac_v1_pbmc_10k_singlecell.xlsx", row.names = 1, sep=',', header = T)
tsne_realData <- Rtsne(expression_data, perplexity=10, check_duplicates = FALSE)
# Error in terms.formula(object, data = data) :
# '.' in formula and no 'data' argument
I briefly looked at the Rtsne package documentation and the Rtsne() function requires the data format to be a matrix. Try converting expression_data to a matrix before passing it to the Rtsne function. You can do so like this:
library(Rtsne)
setwd("n/g")
expression_data <- read.table(file = "zdata.matrix.xlsx", row.names = 1, sep=',', header = T)
meta_data <- read.table(file = "atac_v1_pbmc_10k_singlecell.xlsx", row.names = 1, sep=',', header = T)
expression_matrix <- as.matrix(expression_data)
tsne_realData <- Rtsne(expression_matrix, perplexity=10, check_duplicates = FALSE)

Resources