I'm using the Here JavaScript web SDK and everything is working fine on every device and browser combination on BrowserStack. However, on a physical OnePlus 5T device running Chrome 77.0.3865.116 the map fails to load with the stack trace below in the console.
What I see is that the container for the map including map controls is displayed, the two markers that are added to the map are also briefly shown. Then the screen appears to redraw and the markers disappear leaving a grey, empty map with just the UI controls.
On the same device running Firefox, there are no issues. The stack trace is:
WebGL: CONTEXT_LOST_WEBGL: loseContext: context lost
mapsjs-core.js:377 Tangram [error]: Style: error compiling program for style '0_icon_library' (program key 'program') {texture: "icon_library", blend_order: 1, draw: {…}, mix: Array(1), name: "0_icon_library", …} TypeError: Failed to execute 'shaderSource' on 'WebGLRenderingContext': parameter 1 is not of type 'WebGLShader'.
at Function.Zl.disabled.Wl.createShader (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:215834)
at Function.Wl.updateProgram (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:214896)
at f.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:208515)
at Object.getProgram (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:255275)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:507122)
at t (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:506861)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:507018)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:506274)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:504451)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:504035)
Jr # mapsjs-core.js:377
getProgram # mapsjs-core.js:377
value # mapsjs-core.js:377
t # mapsjs-core.js:377
value # mapsjs-core.js:377
value # mapsjs-core.js:377
value # mapsjs-core.js:377
value # mapsjs-core.js:377
value # mapsjs-core.js:377
$k.V # mapsjs-core.js:286
n.Jh # mapsjs-core.js:333
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
mapsjs-core.js:377 Tangram [error]: Style: error compiling program for style '0_shields' (program key 'program') {blend_order: 1, draw: {…}, mix: Array(1), name: "0_shields", mixed: {…}, …} TypeError: Failed to execute 'shaderSource' on 'WebGLRenderingContext': parameter 1 is not of type 'WebGLShader'.
at Function.Zl.disabled.Wl.createShader (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:215834)
at Function.Wl.updateProgram (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:214896)
at f.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:208515)
at Object.getProgram (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:255275)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:507122)
at t (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:506861)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:507018)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:506274)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:504451)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:504035)
Jr # mapsjs-core.js:377
getProgram # mapsjs-core.js:377
value # mapsjs-core.js:377
t # mapsjs-core.js:377
value # mapsjs-core.js:377
value # mapsjs-core.js:377
value # mapsjs-core.js:377
value # mapsjs-core.js:377
value # mapsjs-core.js:377
$k.V # mapsjs-core.js:286
n.Jh # mapsjs-core.js:333
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
requestAnimationFrame (async)
Xj # mapsjs-core.js:234
zm # mapsjs-core.js:332
n.Jh # mapsjs-core.js:334
mapsjs-core.js:377 Tangram [error]: Style: error compiling program for style '0_text-blend-order' (program key 'program') {blend_order: 1, mix: Array(1), name: "0_text-blend-order", mixed: {…}, animated: false, …} TypeError: Failed to execute 'shaderSource' on 'WebGLRenderingContext': parameter 1 is not of type 'WebGLShader'.
at Function.Zl.disabled.Wl.createShader (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:215834)
at Function.Wl.updateProgram (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:214896)
at f.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:208515)
at Object.getProgram (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:255275)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:507122)
at t (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:506861)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:507018)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:506274)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:504451)
at l.value (https://js.api.here.com/v3/3.1/mapsjs-core.js:377:504035)
I'm not sure of the best way to report this, hence posting here. Happy to raise as a proper issue if someone provides me with the way to do so.
EDIT
Having looked into this error message with MapBox one cause is too many browser tabs open running WebGL but this occurs with only a single tab open
After improvements of JS API the issue "WebGL: CONTEXT_LOST_WEBGL: loseContext: context lost" is not reproducible any more.
Related
I'm trying to get all the price data of DJIA components and combine them into a single dataframe, then export it to a .csv file.
This is what I have currently, but I'm not sure how to proceed.
# dates
fr <- '2010-01-01'
to <- '2022-01-01'
DJIA_list <- c("MMM", "AXP", "AAPL", "BA", "CAT", "CVX", "CSCO", "KO", "DOW", "XOM",
"GS", "HD", "INTC", "IBM", "JNJ", "JPM", "MCD", "MRK", "MSFT","NKE",
"PFE", "PG", "RTX", "TSLA", "TRV", "UNH", "VZ", "V", "WBA", "WMT")
count <- 1
# Getting adjusted prices of assets
for (i in DJIA_list){
asset <- getSymbols(i, src = 'yahoo', from = as.Date(fr), to = as.Date(to), auto.assign = FALSE)
assign(paste0("var_", i), asset[,6])
count <- count + 1
}
I now have a bunch of xts objects named like var_MMM, var_AAPL etc... Not sure how best to deal with them next. I tried to install the qmao add-on but it didn't work for me either.
This is how you would do it with quantmod working with just xts objects
library(quantmod)
fr <- '2010-01-01'
to <- '2022-01-01'
DJIA_list <- c("MMM", "AXP", "AAPL") # extend after testing to the symbols you desire
mktdata <- new.env()
getSymbols(DJIA_list, env = mktdata, from = fr, to = to)
out <- eapply(mktdata, Ad)
out <- do.call(merge, out) # can combine with the above line if you understand what eapply() is doing)
#xts is a zoo object, so can use the write.zoo handler to write to .csv in the format of a data.frame
write.zoo(out, "dj.csv")
You can get the price data easily with the tidyquant library.
library(tidyquant)
#> Loading required package: lubridate
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
#> Loading required package: PerformanceAnalytics
#> Loading required package: xts
#> Loading required package: zoo
#>
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#>
#> as.Date, as.Date.numeric
#>
#> Attaching package: 'PerformanceAnalytics'
#> The following object is masked from 'package:graphics':
#>
#> legend
#> Loading required package: quantmod
#> Loading required package: TTR
#> Registered S3 method overwritten by 'quantmod':
#> method from
#> as.zoo.data.frame zoo
#> == Need to Learn tidyquant? ====================================================
#> Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
#> </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
library(tidyverse)
DJIA_list <- c("MMM", "AXP", "AAPL", "BA", "CAT", "CVX", "CSCO", "KO", "DOW", "XOM",
"GS", "HD", "INTC", "IBM", "JNJ", "JPM", "MCD", "MRK", "MSFT","NKE",
"PFE", "PG", "RTX", "TSLA", "TRV", "UNH", "VZ", "V", "WBA", "WMT")
stock_prices <- tq_get(DJIA_list,
get = "stock.prices",
from = "2010-01-01",
to = "2022-01-01")
#> Registered S3 method overwritten by 'tune':
#> method from
#> required_pkgs.model_spec parsnip
stock_prices %>%
select(date, symbol, adjusted) %>%
pivot_wider(names_from = symbol,
values_from = adjusted) %>%
tail()
#> # A tibble: 6 x 31
#> date MMM AXP AAPL BA CAT CVX CSCO KO DOW XOM GS
#> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 2021-12-23 175. 164. 176. 204. 206. 116. 61.9 58.2 55.1 61.0 385.
#> 2 2021-12-27 177. 164. 180. 203. 206. 119. 63.0 58.7 55.9 61.9 388.
#> 3 2021-12-28 178. 164. 179. 206. 207. 119. 63.2 58.9 56.6 61.7 388.
#> 4 2021-12-29 178. 163. 179. 204. 207. 118. 63.6 59.0 57.1 61.2 386.
#> 5 2021-12-30 178. 164. 178. 203. 206. 117. 63.2 58.8 56.8 60.8 386.
#> 6 2021-12-31 178. 163. 178. 201. 207. 117. 63.0 59.2 56.7 61.2 383.
#> # ... with 19 more variables: HD <dbl>, INTC <dbl>, IBM <dbl>, JNJ <dbl>,
#> # JPM <dbl>, MCD <dbl>, MRK <dbl>, MSFT <dbl>, NKE <dbl>, PFE <dbl>,
#> # PG <dbl>, RTX <dbl>, TSLA <dbl>, TRV <dbl>, UNH <dbl>, VZ <dbl>, V <dbl>,
#> # WBA <dbl>, WMT <dbl>
Created on 2022-01-14 by the reprex package (v2.0.1)
These are the datasets I'm working on.
Since the P-ones have the following structure
# A tibble: 25 x 5
ID GR SES COND `LPP2(1000-1500).POz`
<chr> <chr> <chr> <chr> <dbl>
1 01 RP V NEG-CTR 7.91
2 04 RP V NEG-CTR 1.07
3 06 RP V NEG-CTR 0.742
4 07 RP V NEG-CTR 4.49
5 08 RP V NEG-CTR -2.43
6 09 RP V NEG-CTR 0.649
7 10 RP V NEG-CTR 1.10
8 11 RP V NEG-CTR -2.07
9 12 RP V NEG-CTR 10.9
10 13 RP V NEG-CTR 5.17
While the V-ones have this following ones, with a more extended number of coulmns:
> `RP-V-NEG-CTR`
# A tibble: 25 x 16
ID GR SES COND `P3(400-450).FCz` `P3(400-450).Cz` `P3(400-450).Pz` `LPPearly(500-700)~
<chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 01 RP V NEG-CTR -11.6 -5.17 11.9 -11.8
2 04 RP V NEG-CTR -0.314 2.23 15.1 -4.02
3 06 RP V NEG-CTR -0.214 -1.30 3.14 4.47
4 07 RP V NEG-CTR -2.83 2.19 13.7 -0.884
5 08 RP V NEG-CTR 4.24 2.45 7.48 1.15
6 09 RP V NEG-CTR 9.57 13.7 23.4 13.3
7 10 RP V NEG-CTR -6.13 -4.13 6.27 0.0229
8 11 RP V NEG-CTR 0.529 5.43 13.0 1.90
9 12 RP V NEG-CTR -7.74 -2.41 15.8 -8.08
10 13 RP V NEG-CTR 1.27 3.48 9.94 1.75
# ... with 15 more rows, and 8 more variables: LPPearly(500-700).Cz <dbl>,
# LPPearly(500-700).Pz <dbl>, LPP1(500-1000).FCz <dbl>, LPP1(500-1000).Cz <dbl>,
# LPP1(500-1000).Pz <dbl>, LPP2(1000-1500).FCz <dbl>, LPP2(1000-1500).Cz <dbl>,
# LPP2(1000-1500).Pz <dbl>
>
I've used the followinf procedure for importing and merging them together:
files <- list.files(pattern = "\\.xls")
for (i in 1:length(files)) {
assign(gsub("\\.xls", "", files[i]), readxl::read_xls(files[i]))
}
data <- `RP-V-NEG-CTR` %>%
add_row(`RP-V-NEG-NOC`) %>%
add_row(`RP-V-NEU-NOC`)%>%
arrange(ID)
data1 <- `RP-POz-NEG-CTR` %>%
add_row(`RP-POz-NEG-NOC`) %>%
add_row(`RP-POz-NEU-NOC`)%>%
arrange(ID)
data <- merge(data, data1, by = c('ID', 'GR', 'SES', 'COND'))
Since trying by this following code I've obtain a non-matched merged dataset:
filenames <- list.files(pattern = '^RP.*\\.xls$')
> data <- purrr::map_df(filenames, readxl::read_excel)
Like this:
In the case I would ike to import already merged in a proper way, what am I suppsed to do/adjust?
Thanks in advance
Maybe we need map2
library(dplyr)
library(purrr)
library(stringr)
filesv <- list.files(pattern = 'RP-V-.*\\.xls', full.names = TRUE)
filesp <- list.files(pattern = 'RP-P-.*\\.xls', full.names = TRUE)
nm1 <- str_c(str_remove(basename(filesv), "\\.xls"),
str_remove(basename(filesp), "\\.xls"), sep="_")
out <- map2(filesv, filesp, ~ {
vdat <- readxl::read_excel(.x)
pdat <- readxl::read_excel(.y)
inner_join(vdat, pdat, by = c('ID', 'GR', 'SES', 'COND'))
}) %>%
setNames(nm1) %>%
bind_rows(.id = 'grp')
Or if we don't need the 'grp' column
out <- map2_dfr(filesv, filesp, ~ {
vdat <- readxl::read_excel(.x)
pdat <- readxl::read_excel(.y)
inner_join(vdat, pdat, by = c('ID', 'GR', 'SES', 'COND'))
})
I have a time series and want to use period.apply() function xts library to estimate the mean for 377 days
The reproducible example is as following
zoo.data <- zoo(rnorm(5031)+10,as.Date(13514:17744,origin="1970-01-01"))
ep <- endpoints(zoo.data,'days', k =377)
period.apply(zoo.data, INDEX=ep, FUN=function(x) mean(x))
The output generated is
2007-05-28 2007-12-31 2008-10-05 2008-12-31 2009-02-02 2009-12-31
9.905663 9.800760 10.006344 10.052163 10.152453 10.032073
2010-06-13 2010-12-31 2011-10-22 2011-12-31 2012-02-18 2012-12-31
9.879439 10.038644 9.957582 9.977026 9.959094 10.004348
2013-06-29 2013-12-31 2014-11-07 2014-12-31 2015-03-06 2015-12-31
10.004620 10.086071 9.902875 9.843695 9.851306 10.072610
2016-07-14 2016-12-31 2017-11-23 2017-12-31 2018-03-22 2018-08-01
9.966911 10.199251 10.001628 10.263590 10.181235 10.059080
The output is unexpected as the difference in each date is not 377. The output shows that its stops at year end 20xx-12-31 before moving on to next endpoints
I am not sure that you could solve this using endpoints function directly.
Here is one way to solve it using built-in functions. It is a slightly
general solution.
In the code below, you can uncomment the commented lines to print the number of observations in the last interval.
library(xts)
apply.fun <- function(data, variable=1, fun=mean, k=377) { # variable: variable name or column index
data <- as.xts(data)
variable <- data[, variable, drop=TRUE]
idx <- index(data)
byindex <- as.integer(idx - first(idx)) %/% k # intervals idendifiers
endates <- idx[!duplicated(byindex, fromLast=TRUE)]
ans <- setNames(tapply(variable, byindex, fun), endates)
#inter.end <- sum(byindex==last(byindex))
#if(inter.end < k) cat(sprintf("Last internal has fewer observations: %d<k=%d\n\n", inter.end, k))
return(as.xts(as.matrix(ans)))
}
set.seed(147)
zoo.data <- zoo(rnorm(5031)+10,as.Date(13514:17744,origin="1970-01-01"))
apply.fun(zoo.data, 1, mean)
# [,1]
# 2008-01-12 10.043735
# 2009-01-23 10.042741
# 2010-02-04 9.957842
# 2011-02-16 10.016998
# 2012-02-28 9.932871
# 2013-03-11 9.932731
# 2014-03-23 10.045344
# 2015-04-04 10.015821
# 2016-04-15 10.015023
# 2017-04-27 10.038887
# 2018-05-09 9.978744
# 2018-08-01 10.004074
I wish to plot some maps of Denmark using the new sf package and geom_sf() from ggplot2. I have a shapefile from a public map service, with a total of 4 files (DK.shp, DK.dbf, DK.shf and DK.prj). I have made the data available here in a zip-file (let me know if there is a better way to share files on Stack Overflow, I was not sure).
In R I try to load the file:
library(sf)
devtools::dev_mode(on = TRUE)
#devtools::install_github('tidyverse/ggplot2')
library(ggplot2)
d <- st_read('DK/DK.shp')
ggplot(d) +
geom_sf()
nc <- st_read(system.file("shape/nc.shp", package="sf"))
ggplot(nc) +
geom_sf()
geom_sf() plots the buildin nc.shp file just as expected, but DK.shp makes it return:
Error in x %*% Mat : non-conformable arguments
In addition: There were 50 or more warnings (use warnings() to see the first 50)
The 50 warnings mentioned are all of the type:
1: In matrix(vec, ncol(m), nrow(m)) :
data length [2] is not a sub-multiple or multiple of the number of rows [3]
I am not sure whether this is a problem with the data-file I supply, or a bug in either sf or ggplot2. Any suggestions on how to resolve this?
Update
Looking into the data returned by st_geometry() i found the following:
x <- st_geometry(d[1,])
unlist(x)
# [1] 695222.0 695157.0 694574.5 693521.3 693590.6 693091.5 694813.6 694669.2 694190.6
# [10] 693295.8 692494.8 692655.9 693205.8 693998.4 695060.1 696658.0 697494.1 697847.6
# [19] 699947.6 700876.2 700936.4 702302.0 702727.3 702762.6 703223.3 703771.5 704180.7
# [28] 704991.5 704650.9 706955.0 708027.2 710597.9 710211.5 710128.9 710127.7 709773.2
# [37] 709965.2 708919.0 710098.1 710272.9 710586.6 712156.8 710876.0 710567.9 710041.6
# [46] 709731.5 708333.8 707001.3 707546.4 707193.8 706598.1 706370.5 704816.7 705153.2
# [55] 704208.8 704209.4 702438.4 702290.3 702017.5 701601.2 700813.9 700244.4 698705.2
# [64] 697584.8 695462.3 695222.0 6195729.1 6196153.6 6197058.5 6197735.0 6198228.8 6198687.0
# [73] 6199968.8 6200923.1 6201422.3 6201743.9 6203045.2 6204716.4 6205660.8 6205964.5 6207435.3
# [82] 6207247.1 6207387.4 6207111.0 6208095.2 6208737.5 6209758.0 6209820.5 6209795.2 6209941.6
# [91] 6209663.9 6209741.5 6209669.6 6210534.0 6211238.2 6211301.4 6211441.3 6211409.8 6210281.9
# [100] 6209192.3 6208759.1 6207688.6 6206301.1 6205454.5 6204600.1 6204566.5 6203154.7 6201390.7
# [109] 6199999.8 6199645.5 6199286.7 6199607.3 6199116.3 6199058.5 6198042.2 6197785.1 6198202.3
# [118] 6198169.9 6197827.8 6196050.1 6194848.5 6194665.8 6194474.5 6193641.8 6193609.7 6193729.3
# [127] 6193661.6 6196020.7 6197222.2 6196682.2 6195683.5 6195729.1 -999.0 -999.0 -999.0
# [136] -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0
# [145] -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0
# [154] -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0
# [163] -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0
# [172] -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0
# [181] -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0
# [190] -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0 -999.0
nx <- st_geometry(nc[1,])
unlist(nx)
# [1] -81.47276 -81.54084 -81.56198 -81.63306 -81.74107 -81.69828 -81.70280 -81.67000 -81.34530
# [10] -81.34754 -81.32478 -81.31332 -81.26624 -81.26284 -81.24069 -81.23989 -81.26424 -81.32899
# [19] -81.36137 -81.36569 -81.35413 -81.36745 -81.40639 -81.41233 -81.43104 -81.45289 -81.47276
# [28] 36.23436 36.27251 36.27359 36.34069 36.39178 36.47178 36.51934 36.58965 36.57286
# [37] 36.53791 36.51368 36.48070 36.43721 36.40504 36.37942 36.36536 36.35241 36.36350
# [46] 36.35316 36.33905 36.29972 36.27870 36.28505 36.26729 36.26072 36.23959 36.23436
So the shapefile I am trying to read contains the values in a different format or something. Also, what is going on with all the -999 values? How to I deal with this?
I'll file this as an issue; your geometry contains Z coordinates, and right now geom_sf doesn't like that. Try
ggplot(st_zm(d)) + geom_sf()
to drop the Z coordinate from your polygon nodes. (Issue raised here)
I'm new to R, loops and quantmod. I'm trying to convince quantmod to skip any ticker it's unable to process and continue on to the next ticker symbol, instead of stopping. I thought I'd found my answer here how do I loop through all the stocks with quantmod and ttr? but I'm not able to get Rime's solution to work:
If the loop breaks, say on the 50th iteration, then just re run the last block of code by changing the following
# Actual loop:
# IF IT BREAKS ON THE 50th ITERATION, it must be skipped, therefore change it to 51
for(i in 51:length(symbols)) {
symbols[i]-> symbol
...
Below is my original code, which only returns 8 of the many values(so I assume that 9 is the trouble spot).
library(gdata)
d = read.xls("~/Documents/TEST.xlsx", sheet = 1, stringsAsFactors=F)
library(quantmod)
sym <- as.character(d[,1])
results <- NULL
for (ii in sym){
data1 <- getSymbols(Symbols = ii,
src = "yahoo",
from = Sys.Date() - 100,
auto.assign = FALSE)
de = head(data1,150)
colnames(de) <- c("open","high","low","close","volume","adj.")
overnightRtn <- (as.numeric(de[2:nrow(de),"open"])/as.numeric(de[1:(nrow(de)-1),"close"])) - 1
results <- rbind(results,cbind(
paste(round(min(overnightRtn,na.rm=T),5),"%",sep="")))
}
colnames(results) <- c("overnightRtn2")
rownames(results) <- sym
View(results)
When I change for(ii in sym) to for(ii in 9:length(sym)) I get an error:
could not find function "getSymbols.9"
Here is the start of d[,1] :
[1] "ABX" "ACC" "ACCO" "ACE" "ACG" "ACH" "ACI" "ACM" "ACMP" "ACN"
There are some workarounds for errors when looping in R, one way to do this will be using the tryCatchfunction, juba showed here how to do it. I also made sure that the for loop will only continue when the data1variable is assigned some value.
Change your for loop for the following code and it should work for what you are asking.
for (ii in sym){
data1 <- NULL # NULL data1
data1 <- tryCatch(getSymbols(Symbols = ii,
src = "yahoo",
from = Sys.Date() - 100,
auto.assign = FALSE),
error=function(e){}) # empty function for error handling
if(is.null(data1)) next() # if data1 is still NULL go to next ticker
de = head(data1,150)
colnames(de) <- c("open","high","low","close","volume","adj.")
overnightRtn <- (as.numeric(de[2:nrow(de),"open"])/as.numeric(de[1:(nrow(de)-1),"close"])) - 1
results <- rbind(results,cbind(
paste(round(min(overnightRtn,na.rm=T),5),"%",sep="")))
}
You might try the tidyquant package which takes care of error handling internally. It also doesn't require for-loops so it will save you a significant amount of code. The tq_get() function is responsible for getting stock prices. You can use the complete_cases argument to adjust how errors are handled.
Example with complete_cases = TRUE: Automatically removes "bad apples"
library(tidyquant)
# get data with complete_cases = TRUE automatically removes bad apples
c("AAPL", "GOOG", "BAD APPLE", "NFLX") %>%
tq_get(get = "stock.prices", complete_cases = TRUE)
#> Warning in value[[3L]](cond): Error at BAD APPLE during call to get =
#> 'stock.prices'. Removing BAD APPLE.
#> # A tibble: 7,680 × 8
#> symbol date open high low close volume adjusted
#> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 AAPL 2007-01-03 86.29 86.58 81.90 83.80 309579900 10.85709
#> 2 AAPL 2007-01-04 84.05 85.95 83.82 85.66 211815100 11.09807
#> 3 AAPL 2007-01-05 85.77 86.20 84.40 85.05 208685400 11.01904
#> 4 AAPL 2007-01-08 85.96 86.53 85.28 85.47 199276700 11.07345
#> 5 AAPL 2007-01-09 86.45 92.98 85.15 92.57 837324600 11.99333
#> 6 AAPL 2007-01-10 94.75 97.80 93.45 97.00 738220000 12.56728
#> 7 AAPL 2007-01-11 95.94 96.78 95.10 95.80 360063200 12.41180
#> 8 AAPL 2007-01-12 94.59 95.06 93.23 94.62 328172600 12.25892
#> 9 AAPL 2007-01-16 95.68 97.25 95.45 97.10 311019100 12.58023
#> 10 AAPL 2007-01-17 97.56 97.60 94.82 94.95 411565000 12.30168
#> # ... with 7,670 more rows
Example with complete_cases = FALSE: Returns nested data frame.
library(tidyquant)
# get data with complete_cases = FALSE returns a nested data frame
c("AAPL", "GOOG", "BAD APPLE", "NFLX") %>%
tq_get(get = "stock.prices", complete_cases = FALSE)
#> Warning in value[[3L]](cond): Error at BAD APPLE during call to get =
#> 'stock.prices'.
#> Warning in value[[3L]](cond): Returning as nested data frame.
#> # A tibble: 4 × 2
#> symbol stock.prices
#> <chr> <list>
#> 1 AAPL <tibble [2,560 × 7]>
#> 2 GOOG <tibble [2,560 × 7]>
#> 3 BAD APPLE <lgl [1]>
#> 4 NFLX <tibble [2,560 × 7]>
In both cases the user gets a WARNING message. The prudent user will read them and try to determine what the issue is. Most important, the long running script will not fail.