cleaning data fails when using SIT - r

I am trying to format the data using the bt.prep() function. Can someone explain why this fails, and hopefully how to fix it? Link to github repo
library(quantmod)
#Systematic Investor Toolbox
sit = getURLContent('https://github.com/systematicinvestor/SIT/raw/master/sit.gz', binary=TRUE, followlocation = TRUE, ssl.verifypeer = FALSE)
con = gzcon(rawConnection(sit, 'rb'))
source(con)
close(con)
data <- getSymbols("USD/EUR",src="oanda",env=NULL)
bt.prep(data, align='remove.na')
Error is:
Error in b[[i]] : attempt to select more than one element In addition: Warning message:
In merge.xts(..., all = all, fill = fill, suffixes = suffixes) :
NAs introduced by coercion

#Rilcon42,
I think you need to create a new.env() before calling getSymbols...
library(quantmod)
library(RCurl)
#Systematic Investor Toolbox
sit = getURLContent('https://github.com/systematicinvestor/SIT/raw/master/sit.gz', binary=TRUE, followlocation = TRUE, ssl.verifypeer = FALSE)
con = gzcon(rawConnection(sit, 'rb'))
source(con)
close(con)
tickers = spl('USD/EUR')
dataRepo <- new.env()
getSymbols(tickers, src = "oanda", env = dataRepo, auto.assign = T)
bt.prep(dataRepo, align='remove.na')
head(dataRepo$USDEUR,6)
results:
> library(quantmod)
> library(RCurl)
>
> #Systematic Investor Toolbox
> sit = getURLContent('https://github.com/systematicinvestor/SIT/raw/master/sit.gz', binary=TRUE, followlocation = TRUE, ssl.verifypeer = FALSE)
> con = gzcon(rawConnection(sit, 'rb'))
> source(con)
> close(con)
>
> tickers = spl('USD/EUR')
> dataRepo <- new.env()
> getSymbols(tickers, src = "oanda", env = dataRepo, auto.assign = T)
[1] "USDEUR"
> bt.prep(dataRepo, align='remove.na')
>
> head(dataRepo$USDEUR,6)
USD.EUR
2015-01-11 0.8445
2015-01-12 0.8448
2015-01-13 0.8467
2015-01-14 0.8489
2015-01-15 0.8535
2015-01-16 0.8621

Related

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.

curl_fetch_disk shutting down with no error message

The code below keeps shutting down after a minute with no error messages. Does anybody know why ? I think it has to do with the function curl_fetch_disk...
library(dplyr)
library(curl)
main.dir <- "C:/Users/blue/Desktop/Arc"
liste.urls <- read.csv('C:/Users/blue/Desktop/Arc/test2.csv', header = FALSE)
subsub.dir <- 'hydro_l'
liste.urls$hydro_l <- paste0(liste.urls$V1, "/hydro_l/")
liste.urls$rep <- sapply(liste.urls, substring, 94, 100)
for (i in 1:length(liste.urls)) {
url.download <- as.character(liste.urls$hydro_l[i+6])
handle = new_handle(dirlistonly = TRUE)
con = curl(url.download, "r", handle)
tbl = read.table(con, stringsAsFactors = TRUE, fill = TRUE)
close(con)
head(tbl)
liste.fichiers.urls <- paste0(url.download, tbl[, 1])
fichiers = basename(liste.fichiers.urls)
sub.dir <- liste.urls$rep[i+6]
dir.create(file.path(main.dir, sub.dir))
dir.create(file.path(main.dir, sub.dir, subsub.dir))
setwd(file.path(main.dir, sub.dir, subsub.dir))
for (j in 1:length(liste.fichiers.urls)) {
curl_fetch_disk(liste.fichiers.urls[j], fichiers[j])
}
}

RPackage library exception (error 1000)

I have the following code in an Execute R Module.
# Input
data1 <- maml.mapInputPort(1) # Qualitative with 8 variables
install.packages("src/graphics.zip", lib.loc = ".", repos = NULL, verbose =
TRUE)
install.packages("src/grDevices.zip", lib.loc = ".", repos = NULL, verbose =
TRUE)
install.packages("src/stats.zip", lib.loc = ".", repos = NULL, verbose =
TRUE)
install.packages("src/utils.zip", lib.loc = ".", repos = NULL, verbose =
TRUE)
install.packages("src/MASS.zip", lib.loc = ".", repos = NULL, verbose =
TRUE)
success <- library("MASS", lib.loc = ".", logical.return = TRUE, verbose =
TRUE)
library(MASS)
mca <- mca(data1, nf = 10)
mca1 <- data.frame(mca$rs)
# Output
maml.mapOutputPort("mca1");
When I execute I am getting the following error:
RPackage library exception: Attempting to obtain R output before invoking execution process. (Error 1000)
But it is working fine in RStudio.
I also have a node that does the same process and it works without errors. I have executed it several times, sometimes it has worked for me and then it has returned error.
Please let me know what the issue is.
With regards,
Celia

Error with getSymbols - RStudio

I don't know why but I am facing errors when trying to download data from "Oanda" using getSymbols function:
library(quantmod)
require(quantmod)
tickers <- c("USD/EUR","EUR/BRL","USD/ARS","USD/BRL","USD/CLP","USD/COP")
tickers2 <- c("USDEUR","EURBRL","USDARS","USDBRL","USDBRL","USDCLP","USDCOP")
getSymbols(tickers, src = "oanda", env = parent.frame(n=1),
from = "2014-01-01", to = Sys.Date(), auto.assign = TRUE,
warning = FALSE)
Closeprices <- do.call(cbind, lapply(tickers2, function(x) get(x)))
head(Closeprices)
dat1 <- as.data.frame(Closeprices)
dat1$date <- time(Closeprices)
head(dat1)
and it returns the following error:
> getSymbols("USD/EUR", src = "oanda", env = parent.frame(n=1),
+ to = Sys.Date(), auto.assign = TRUE,
+ warning = FALSE)
Error in charToDate(x) :
character string is not in a standard unambiguous format
It used to work well, but now it is crashing.
Can you help me out?

R asks for a list which seems to be a list according to is.list (=TRUE)

I am using the RAM package.
The function I use is very simple for diversity index, adding up a column in my metadata ;
outname <-OTU.diversity(data=OTUtables, meta=metatables)
(Arguments: data a list of OTU tables.
meta the metadata to append the outputs)
I am looping it but I get this error:
please provide otu tables as list; see ?RAM.input.formatting
So I go to that help menu and read this:
one data set:
data=list(data=otu)
multiple data sets:
data=list(data1=otu1, data2=otu2, data3=otu3)
here is my code:
i <- 1
for(i in 1:nrow(metadataMasterTax)){
temp <- read.table(paste(metadataMasterTax$DataAnFilePath[i], metadataMasterTax$meta[i], sep = ""),
sep = "\t", header = TRUE, dec = ".", comment.char = "", quote = "", stringsAsFactors = TRUE,
as.is = TRUE)
temp2 <- temp
temp2$row.names <- NULL #to unactivate numbers generated in the margin
trans <- read.table(paste(metadataMasterTax$taxPath[i], metadataMasterTax$taxName[i], sep = ""),
sep = "\t", header = TRUE, dec = ".", comment.char = "", quote = "", stringsAsFactors = TRUE,
as.is = TRUE, check.names = FALSE)
trans2 <- trans
trans2$row.names <- NULL #to unactivate numbers generated in the margin
data=list(data=trans2[i])
temp2[i] <- OTU.diversity(data=trans2[i], meta=temp2[i])
# Error in OTU.diversity(trans2, temp2) :
# please provide otu tables as list; see ?RAM.input.formatting
# is.list(trans2)
# [1] TRUE
# is.list(data)
# [1] TRUE
temp$taxonomy <- temp2$taxonomy
write.table(temp, file=paste(pathDataAn, "diversityDir/", metadataMasterTax$ShortName[i], ".meta.div.tsv", sep = ""),
append = FALSE,
sep = "\t",
row.names = FALSE)
}
Can anyone help me please....
thanks a lot
Because the main problem appears to be getting the OTU.diversity function to work, I focus on this issue. The code snippet below runs OTU.diversity without any problems, using the Google sheets data provided by OP.
library(gsheet)
library(RAM)
for (i in 1:2) {
# Meta data
temp <- as.data.frame(gsheet2tbl("https://drive.google.com/open?id=1hF47MbYZ1MG6RzGW-fF6tbMT3z4AxbGN5sAOxL4E8xM"))
temp$row.names <- NULL
# OTU
trans <- as.data.frame(gsheet2tbl("https://drive.google.com/open?id=1gOaEjDcs58T8v1GA-OKhnUsyRDU8Jxt2lQZuPWo6XWU"))
trans$row.names <- NULL
rownames(temp) <- colnames(trans)[-ncol(trans)]
temp2 <- OTU.diversity(data = list(data = trans), meta = temp)
write.table(temp2,
file = paste0("file", i, ".meta.div.tsv"), # replace
append = FALSE,
sep = "\t",
row.names = FALSE)
}
Replace for (i in 1:2) with for(i in 1:nrow(metadataMasterTax)), as.data.frame(gsheet2tbl(...)) with read.table(...), and the file argument in write.table with the appropriate string.

Resources