Error with getSymbols - RStudio - r

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?

Related

Again reactiveFileReader

Let's say get_line() is reactive, returns an integer, e.g.:
get_line <- reactive({
i=1
getline=0
while (getline==0 & i<=100) {
x <- readLines(input$StoreDestination , n =100)[i]
x <- stringr::str_extract(x, pattern = "t1=[0-9]")
if(!is.na(x)) {getline=i}
i=i+1
}
return(getline)
})
Next calling reactiveFileReader such that skip parameter of the read.table function is reactive, e.g.:
df_source <- reactiveFileReader(
intervalMillis = 10000,
session=session,
filePath = reactive({ input$StoreDestination }),
readFunc = read.table,
skip = get_line(), ### with () as we see :)
check.names = F,
sep = ";")
Getting Error:
Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context.
• You tried to do something that can only be done from inside a reactive consumer.
tried to sort of explicitly state that it is a reactive:
df_source <- reactiveFileReader(
intervalMillis = 10000,
session=session,
filePath = reactive({ input$StoreDestination }),
readFunc = read.table,
skip = reactive({ get_line() }),
check.names = F,
sep = ";")
Getting Error:
Warning: Error in >: comparison (6) is possible only for atomic and list types
any try such that df_source <- reactive({ reactiveFileReader( ..., skip = get_line(),...)})
Getting Error:
Warning: Error in as.data.frame.default: cannot coerce class ‘c("reactiveExpr", "reactive", "function")’ to a data.frame
However if we use, fixed value for the skip, everything is working. e.g.:
df_source <- reactiveFileReader(
intervalMillis = 10000,
session=session,
filePath = reactive({ input$StoreDestination }),
readFunc = read.table,
skip = 4,
check.names = F,
sep = ";")

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])
}
}

I can't get the xts object in rstudio

I use getSymbols to get the return data of sp500 in R,but i can't use the xts data in Ad() function,here is my code:
identifiers <- read.csv("sp500.csv",header = FALSE)
getSymbols("GOOG", src = "yahoo", from = "2013-01-02") #Stock: Google
LogRetGOOG = drop(coredata(na.omit(diff(log(lag(Ad(GOOG)))))))
stocks = matrix(nrow = length(LogRetGOOG),ncol = nrow(identifiers))
for (sedol in identifiers$V1) {
getSymbols(sedol, src = "yahoo", from = "2018-01-02") #Stock: Google
# dealead_data <- drop(coredata(na.omit(diff(log(lag(Ad(sedol)))))))
# stocks[,i] = dealead_data
# i <- i + 1
}
i <- 1
for (sedol in objs) {
x <- get(sedol)
# getSymbols(sedol, src = "yahoo", from = "2018-01-02") #Stock:
Google
dealead_data <-
drop(coredata(na.omit(diff(log(lag(as.name(sedol)))))))
stocks[,i] = dealead_data
i <- i + 1
}
the return result of as.name() is charactor,but I need the xts objcet,I don't know how to get it,thanks.
getSymbols(identifier, src = "yahoo", from = "2007-01-01",to = "2016-12-31",auto.assign = FALSE)
add auto.assign = FALSE,the issue was sovled

cleaning data fails when using SIT

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

r - App failed to start - could not find function "setInternet2"

I met some problems that whenever I tried to deploy my shiny applications, I got this message: Error in value[3L] : could not find function setInternet2
Calls: local...tryCatch ......
Execution halted
setInternet2
code here
### install packages
library(doParallel)
library(devtools)
library(lattice)
library(shiny)
library(stringr)
library(lubridate)
library(shinyBS)
library(doSNOW)
library(XML)
library(httr)
library(RCurl)
library(wordcloud)
library(tm)
library(rJava)
library(qdap)
library(slam)
runApp(list(
server = shinyServer(function(input, output, session){
observeEvent(input$getNews, {
output$News <- renderDataTable({
# Create a Progress object
progress <- shiny::Progress$new(session, min=1, max=15)
progress$set(message = "(╯ ̄▽ ̄)╯ Loading...", value = 0)
# Close the progress when this reactive exits (even if there's an error)
on.exit(progress$close())
data <- list()
tmp <- paste('.html', sep='')
url <- paste('https://www.ptt.cc/bbs/Stock/index', tmp, sep='')
html <- httr:::content(GET(url), encoding = "UTF-8") # xml2
html <- XML::xmlParse(html) # parse from xml2 to xml
url.list <- xpathSApply(html, "//div[#class='title']/a[#href]", xmlAttrs)
data <- rbind(data, paste('https://www.ptt.cc', url.list, sep=''))
data <- unlist(data)
# cl = makeCluster(rep('localhost', 8), 'SOCK')
# clusterSetupRNG(cl)
# clusterEvalQ(cl, source('R/R/GET.R'))
getDoc <- function(line){
start <- regexpr('www', line)[1]
end <- regexpr('html', line)[1]
if(start != -1 & end != -1){
url <- substr(line, start, end + 3)
name <- strsplit(url, '/')[[1]][4]
txtName <- gsub('html', 'txt', name)
if(!file.exists(paste0("document/news/", txtName))){
# html <- httr:::content(GET(url, config = set_cookies("over18"="1")), encoding="UTF-8")
html <- httr:::content(GET(url), encoding = "UTF-8")
html <- XML::xmlParse(html)
doc <- xpathSApply(html, "//div[#id='main-content']", xmlValue)
#write(doc, paste0("document/news/", gsub('html', 'txt', name)),
# encoding = "UTF-8")
writeLines(as.character(doc), paste0("document/news/", gsub('html', 'txt', name)),
useBytes=T)
}
}
}
# parSapply(cl, data, getDoc)
# stopCluster(cl)
sapply(data, getDoc)
cl <- makeCluster(4, type = "SOCK")
doSNOW:::registerDoSNOW(cl)
articles <-
foreach(i = 1:length(list.files("document/news/")), .combine = 'c') %dopar% {
readLines(paste0("document/news/", list.files("document/news/")[i]), encoding = "UTF-8")[1]
}
stopCluster(cl)
start = regexpr("新聞", articles)
end = regexpr("2016", articles)
news <- substr(articles, start = start - 1, stop = end + 3)[start != -1]
news
news2 <- substr(news, start = 1, stop = regexpr("時間", news) - 1)
# for messages
Sys.setenv(LANG = "Zh_TW")
Sys.setlocale("LC_ALL", "cht")
start = regexpr("時間", news)
end = regexpr("2016", news)
newsDate = substr(news, start = start + 6, stop = end + 3)
newsDate
Sys.setenv(LANG = "en")
Sys.setlocale("LC_ALL", "English")
newsDate = strptime(newsDate, format = "%b %d %H:%M:%S %Y")
newsDate = as.POSIXct(newsDate)
#newsDate = as.Date(newsDate, format = "%b %d %Y")
Sys.setenv(LANG = "Zh_TW")
Sys.setlocale("LC_ALL", "cht")
newsDF <- data.frame(Date = newsDate, Event = news2)
news <- newsDF
news <- news[order(news$Date, decreasing = TRUE), ]
news[as.Date(news$Date, format = "%b %d %Y") >= input$dateRange[1] &
as.Date(news$Date, format = "%b %d %Y") <= input$dateRange[2], ]
})
})
}),
ui = shinyUI(tagList(
tags$head(tags$script(HTML("Shiny.addCustomMessageHandler('closeWindow', function(m) {window.close();});"))),
navbarPage(
"MynavbarPage", inverse = TRUE, id = "navbar",
tabPanel("News",
sidebarLayout(
sidebarPanel(
width = 3,
bsTooltip("stocks", title = "Please enter stock code from yahoo finance.", placement = "bottom", trigger = "hover", options = NULL),
textInput("stocks", label = "Stock Code", value = "2330.TW"),
bsTooltip("dateRange", title = "This is a time period you apply WFA to.", placement = "bottom", trigger = "hover", options = NULL),
dateRangeInput("dateRange", label = "Choose Period", start = "2015-01-01", end = Sys.Date()),
actionButton("getNews", "Start", class="btn-primary btn-lg")
),
mainPanel(
bootstrapPage(
dataTableOutput("News"))
)
))
)))
))
But I can run my App locally. I was wondering if it had anything to do with my local path settings? My app is in a folder, e.g., "MyAPP" where ui.R and server.R are included. I also have another folder named "document" inside MyAPP. I used lots of list.file("document/").
I didn't include setInternet2 function in my app but I was using web APIs such as httr, XML, tm, etc.
Any suggestions?
Many thanks.
It seems that the following command connects to the internet and downloads the data. And internally it is setting the Internet options
html <- httr:::content(GET(url), encoding = "UTF-8") # xml2
Can you try the following which will help you debug
Instead of downloading the HTMl from the internet, read from a file
If it works then we know for sure that code that fetches data from the internet is the issue
Regards,
Anant

Resources