Using saveWorkbook (from openxlsx) get Error - r

Tested with a simple data frame as input, i think it might be the Excel file itself.
workbook <- loadWorkbook(filepath, xlsxFile = NULL, isUnzipped = FALSE)
writeData(workbook, "Test1", al, startCol = 2, startRow = 5, colNames = FALSE)
saveWorkbook(workbook, filepath, overwrite = T)
But I get this :
Error in comment_list[[i]]$style[[j]] : subscript out of bounds

Apparantly, openxslx cannot handle comments. Deleting all comments in my excel file resolved the problem.

Related

R) Counts.csv.gz file to Seurat object

I usually import filtered feature bc matrix including barcodes.tsv.gz, features.tsv.gz, and matrix.mtx.gz files to R environment by Read10X function, and convert the data to Seurat object by CreateSeuratObject function.
However, I found out that some publicly available processed scRNA-seq data was shared only in the format of counts.csv.gz file.
So, I tried to convert the counts.csv.gz files to Seurat object via following commands;
countsData<-read.delim(file = "~path/TUMOR1_counts.csv.gz", header = TRUE, sep = ",")
Tumor2 <- CreateSeuratObject(counts = countsData, project = "Tumor2", min.cells = 3, min.features = 200)
However, the following error occured.
Error in CreateAssayObject(counts = counts, min.cells = min.cells, min.features = min.features) :
No feature names (rownames) names present in the input matrix
Here is the counts.csv file that looks like this.
How can I solve this problem?
At first, count matrix as an input for CreateSeuratObject() should have the cells in column and features in row. It seems like that you should use t() to convert your imported counts with the rownames.
I recommend you do like this:
countsData <- read.csv(file = "~path/TUMOR1_counts.csv", header = TRUE, row.names = 1)
Tumor2 <- CreateSeuratObject(counts = t(countsData), project = "Tumor2", min.cells = 3, min.features = 200)
I think you have empty cells. You should fill them with zeros.

With R package openxlsx i am trying to merge columns and rows. though script works it does not allow me to overwrite

I use this script
library(openxlsx)
Output <- read.xlsx(xlsxFile = "Excel_file.xlsx", fillMergedCells =
TRUE,colNames = TRUE)
When i do this it works and does what it is supposed to do. The file contains a lot of merged rows and columns. However it doesn't always work. Is there another package or another script i can do that maybe can do overwrite? I'm wondering if that is the issue.
Otherwise when i try to run the script above again it tells me.
Error in read.xlsx(xlsxFile = "Excel_file.xlsx", fillMergedCells = TRUE, :
Please provide a sheet name OR a sheet index.
when i do and acknowledge adding sheet name or sheet index.....
library(openxlsx)
Output <- read.xlsx(xlsxFile = "Excel_file.xlsx", fillMergedCells = TRUE,colNames = TRUE,sheetIndex = 1)
or
library(openxlsx)
Output <- read.xlsx(xlsxFile = "Excel_file.xlsx", fillMergedCells = TRUE,colNames = TRUE,sheetName = "sheet 1")
i get another error message
Error in loadWorkbook(file, password = password) :
argument "file" is missing, with no default

Issue downloading and opening xlsx-file from within R

I would like to download and open the following Excel-file with monthly and annual consumer price indices directly from within R.
https://www.bfs.admin.ch/bfsstatic/dam/assets/7066959/master
(the link can be found on this site: https://www.bfs.admin.ch/bfs/de/home/statistiken/preise/landesindex-konsumentenpreise/lik-resultate.assetdetail.7066959.html)
I used to download this file manually using the browser, save it locally on my computer, then open the xlsx-file with R and work with the data without any problems.
I have now tried to read the file directly from within R, but without luck so far. As you can see from the URL above, there is no .xlsx extension or the like, so I figured the file is zipped somehow. Here is what I've tried so far and where I am stuck.
library(foreign)
library(xlsx)
# in a browser, this links opens or dowloads an xlsx file
likurl <- "https://www.bfs.admin.ch/bfsstatic/dam/assets/7066959/master"
temp <- tempfile()
download.file(likurl, temp)
list.files <- unzip(temp,list=TRUE)
data <- read.xlsx(unz(temp,
+ list.files$Name[8]), sheetIndex=2)
The result from the last step is
Error in +list.files$Name[8] : invalid argument to unary operator
I do not really understand the unz function, but can see this is somehow wrong when reading the help file for unz (I found this suggested solution somewhere online).
I also tried the following, different approach:
library(XLConnect)
likurl <- "https://www.bfs.admin.ch/bfsstatic/dam/assets/7066959/master"
tmp = tempfile(fileext = ".xlsx")
download.file(likurl, tmp)
readWorksheetFromFile(tmp, sheet = 2, startRow = 4,
colNames = TRUE, rowNames = FALSE)
with the last line returning as result:
Error: ZipException (Java): invalid entry size (expected 1644 but got 1668 bytes)
I would greatly appreciate any help on how I can open this data and work with it as usual when reading in data from excel into R.
Thanks a lot in advance!
Here's my solution thanks to the hint by #Johnny. Reading the data from excel worked better with read.xlsx from the xlsx-package (instead of read_excel as suggested in the link above).
Some ugly details still remain with how the columns are named (colNames are not passed on correctly, except for the first and 11th column) and how strangely new columns are created from the options passed to read.xlsx (e.g., a column named colNames, with all entries == TRUE; for details, see the output structure with str(LIK.m)). However, these would be for another question and for the moment, they can be fixed in the quick and dirty way :-).
library(httr)
library(foreign)
library(xlsx)
# in a browser, this links opens or dowloads an xlsx file
likurl<-'https://www.bfs.admin.ch/bfsstatic/dam/assets/7066959/master'
p1f <- tempfile()
download.file(likurl, p1f, mode="wb")
GET(likurl, write_disk(tf <- tempfile(fileext = ".xlsx")))
# annual CPI
LIK.y <- read.xlsx(tf,
sheetIndex = 2, startRow = 4,
colNames = TRUE, rowNames = FALSE, stringsAsFactors = FALSE,
detectDates = FALSE, skipEmptyRows = TRUE, skipEmptyCols = TRUE ,
na.strings = "NA", check.names = TRUE, fillMergedCells = FALSE)
LIK.y$X. <- as.numeric(LIK.y$X.)
str(LIK.y)
# monthly CPI
LIK.m <- read.xlsx(tf,
sheetIndex = 1, startRow = 4,
colNames = TRUE, rowNames = FALSE, stringsAsFactors = FALSE,
detectDates = FALSE, skipEmptyRows = TRUE, skipEmptyCols = TRUE ,
na.strings = "NA", check.names = TRUE, fillMergedCells = FALSE)
LIK.m$X. <- as.numeric(LIK.m$X.)
str(LIK.m)

openxlsx not able to read from .xlsx file in R

I am trying to read value from a .xlsx file using openxlsx package in R. In simple words, I need to write a row of data, which then populates some output cell that has to be read back in R. I will share an example to better explain the problem.
Initial state of the .xlsx file:
I'm now trying to write new values to the cell : A2:A3 = c("c", 5). So ideally, I'm expecting A6 = 15
Below is the code used :
require(openxlsx)
path <- "C:/path_to_file/for_SO1.xlsx"
input_row <- c("c", 5)
# Load workbook; create if not existing
wb <- loadWorkbook(path)
# createSheet(wb, name = "1")
writeData(wb,
sheet = "Sheet1",
x = data.frame(input_row),
startCol=1,
startRow=1
)
data_IM <- read.xlsx(wb,
sheet = "Sheet1",
rows = c(5,6),
cols = c(1))
# Save workbook
saveWorkbook(wb, file = path, overwrite = TRUE)
#> data_IM
# output_row
#1 3
But I get the inital value(3). However, If i open the .xlsx file, I can see the 15 residing there:
What could be the reason for not able to read this cell? I tried saving it after writing to the file and again reading it but even that failed. openxlsx is the only option I have due to JAVA errors from XLConnect etc.
?read.xlsx
Formulae written using writeFormula to a Workbook object will not get
picked up by read.xlsx(). This is because only the formula is written
and left to be evaluated when the file is opened in Excel. Opening,
saving and closing the file with Excel will resolve this.
So the file needs to be opened in Excel and then saved, I can verify that this does work. However this may not be suitable for you.
XLConnect seems to have the desired functionality
# rjava can run out of memory sometimes, this can help.
options(java.parameters = "-Xmx1G")
library(XLConnect)
file_path = "test.xlsx"
input_row <- c("c", 5)
wb <- loadWorkbook(file_path, create=F)
writeWorksheet(wb, 1, startRow = 1, startCol = 1, data = data.frame(input_row))
setForceFormulaRecalculation(wb, 1, TRUE)
saveWorkbook(wb)
# checking
wb <- loadWorkbook(file_path, create=F)
readWorksheet(wb, 1)
The file https://cran.r-project.org/web/packages/openxlsx/openxlsx.pdf says
Workbook object will not get picked up by read.xlsx().
This is because only the formula is written and left to be evaluated when the file is opened in Excel.
Opening, saving and closing the file with Excel will resolve this.
So if you are using windows then
save following file vbs file to for example opensaveexcel.vbs
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("D:\Book2.xlsx")
objWorkbook.Save
objWorkbook.Close
objExcel.Quit
Set objExcel = Nothing
Set objWorkbook = Nothing
and then you can write R code as cell A4 has formula in Book1.xlsx as =A3*5
mywritexlsx(fname="d:/Book1.xlsx",data = 20,startCol = 1,startRow = 3)
system("cp d:\\Book1.xlsx d:\\Book2.xlsx")
system("cscript //nologo d:\\opensaveexcel.vbs")
tdt1=read.xlsx(xlsxFile = "d:/Book1.xlsx",sheet = "Sheet1",colNames = FALSE)
tdt2=read.xlsx(xlsxFile = "d:/Book2.xlsx",sheet = "Sheet1",colNames = FALSE)
Works for me by the way mywritexlsx is as
mywritexlsx<-function(fname="temp.xlsx",sheetname="Sheet1",data,
startCol = 1, startRow = 1, colNames = TRUE, rowNames = FALSE)
{
if(!file.exists(fname))
{
wb = openxlsx::createWorkbook()
sheet = openxlsx::addWorksheet(wb, sheetname)
}
else
{
wb <- openxlsx::loadWorkbook(file =fname)
if(!(sum(openxlsx::getSheetNames(fname)==sheetname)))
sheet = openxlsx::addWorksheet(wb, sheetname)
else
sheet=sheetname
}
openxlsx::writeData(wb,sheet,data,startCol = startCol, startRow = startRow,
colNames = colNames, rowNames = rowNames)
openxlsx::saveWorkbook(wb, fname,overwrite = TRUE)
}

Error in file.exists(path) : invalid 'file' argument in R

Please accept my apologies as I am new to R. The following code is used to process multiple files in one go and placing the output in a separate folder.
require(highfrequency)
require(quantmod)
require(readxl)
input_files1=list("file_path1.xlsx","file_path2.xlsx","file_path3.xlsx") #making list of file paths
for(i in length(input_files1))
{
bid_df<-read_excel(input_files1[i], sheet = 1, col_names = TRUE, col_types = NULL, na = "", skip = 0)
#read_excel takes file path as first argument
ask_df<-read_excel(input_files1[i], sheet = 2, col_names = TRUE, col_types = NULL, na = "", skip = 0)
trade_df<-read_excel(input_files1[i], sheet = 3, col_names = TRUE, col_types = NULL, na = "", skip = 0)
qdata_df <- merge(ask_df, bid_df, by = "TIMESTAMP")
qdata_xts_raw<-xts(qdata_df[,-1], order.by=qdata_df[,1])
qdata_xts_m<-mergeQuotesSameTimestamp(qdata_xts_raw, selection = "median")
trade_xts_raw <- xts(trade_df[,-1], order.by=trade_df[,1])
trade_xts_m<-mergeTradesSameTimestamp(trade_xts_raw, selection = "median")
tqdata=matchTradesQuotes(trade_xts_m,qdata_xts_m)
quoted_spread<-tqLiquidity(tqdata,trade_xts_m,qdata_xts_m,type="qs")
qs_30<-aggregatets(quoted_spread,FUN="mean",on="minutes",k=30)
indexTZ(qs_30) <- "UTC"
write.csv(qs_30, file = file.path("output_file_path", paste0("CAN_out", i)))
}
When the code is run, it gives the following error
Error in file.exists(path) : invalid 'file' argument
Please help in removing the error and running the code.
Access elements of the list in the path as input_files1[[i]]

Resources