Round maps with R - r

I need to draw a round map in an R object in PowerBI.
Now I use two objects, a transparent circle image and a R visual object (map).
This is the code:
dataset <- data.frame(03. Ciudad origen, lon, lat, 02. Ciudad destino, lon.1, lat.1, zoomMapa)
dataset <- unique(dataset)
par(fg="#f0ecf8")
colnames(dataset) <- c("ciudad_base","lon_base","lat_base","apto_dest","lon_dest","lat_dest","zoomMapa")
library(stringr)
library(maps)
library(geosphere)
library(magrittr)
offset <- unique(dataset$zoomMapa)
plot_my_connection=function( dep_lon, dep_lat, arr_lon, arr_lat, ...){
inter <- gcIntermediate(c(dep_lon, dep_lat), c(arr_lon, arr_lat), n=50, addStartEnd=TRUE, > breakAtDateLine=F)
inter=data.frame(inter)
diff_of_lon=abs(dep_lon) + abs(arr_lon)
if(diff_of_lon > 180){
lines(subset(inter, lon>=0), ...)
lines(subset(inter, lon<0), ...)
}else{
lines(inter, ...)
}
}
dif_lon=function(dep_lon, arr_lon){
ret = 0
if (dep_lon< 0 & arr_lon<0 ){
ret = abs(min(dep_lon, arr_lon)-max(dep_lon, arr_lon))
}
if (dep_lon> 0 & arr_lon>0 ){
ret = abs(max(dep_lon, arr_lon)-min(dep_lon, arr_lon))
}
if (dep_lon> 0 & arr_lon<0 ){
ret = dep_lon - arr_lon
}
if (dep_lon<0 & arr_lon>0 ){
ret = - dep_lon + arr_lon
}
return (ret)
}
dif_lat=function(dep_lat, arr_lat){
ret = 0
if (dep_lat< 0 & arr_lat<0 ){
ret = abs(min(dep_lat, arr_lat)-max(dep_lat, arr_lat))
}
if (dep_lat> 0 & arr_lat>0 ){
ret = abs(max(dep_lat, arr_lat)-min(dep_lat, arr_lat))
}
if (dep_lat> 0 & arr_lat<0 ){
ret = dep_lat - arr_lat
}
if (dep_lat<0 & arr_lat>0 ){
ret = - dep_lat + arr_lat
}
return (ret)
}
lonmin= min(min(dataset$lon_base), min(dataset$lon_dest)) - dataset$zoomMapa
lonmax = max(max(dataset$lon_base), max(dataset$lon_dest)) + dataset$zoomMapa
latmin= min(min(dataset$lat_base), min(dataset$lat_dest)) - dataset$zoomMapa
latmax=max(max(dataset$lat_base), max(dataset$lat_dest)) + dataset$zoomMapa
diflon = dif_lon(lonmin,lonmax)
diflat = dif_lat(latmin, latmax)
off_lon = 0
off_lat = 0
if (diflon > diflat)
off_lat = (diflon-diflat)/2
if (diflon < diflat)
off_lon = (diflat-diflon)/2
lonmin= lonmin - off_lon
lonmax = lonmax + off_lon
latmin= latmin - off_lat
latmax = latmax + off_lat
par(mar=c(0,0,0,0))
map('world', boundary = TRUE, regions = "", interior = TRUE, bg="white", col="#f0ecf8", fill=TRUE, mar = c(0.1, 0.1, 0, 0.1), xlim=c(lonmin,lonmax), ylim=c(latmin,latmax) )
points(x=as.double(dataset$lon_base1), y=as.double(dataset$lat_base1), col="#1a2732", cex=1, pch=1)
text(dataset$ciudad_base1, x=as.double(dataset$lon_base1), y=as.double(dataset$lat_base1), col="#1a2732", cex=1, pos=1)
for(i in 1:nrow(dataset)){
plot_my_connection(as.double(dataset$lon_base[i]), as.double(dataset$lat_base[i]), as.double(dataset$lon_dest[i]),as.double( dataset$lat_dest[i]), col="#1a2732", lwd=1)
points(x=as.double(dataset$lon_dest[i]), y=as.double(dataset$lat_dest[i]), col="#1a2732", cex=1, pch=1)
text(dataset$apto_dest[i], x=as.double(dataset$lon_dest[i]), y=as.double(dataset$lat_dest[i]), col="#1a2732", cex=1, pos=1)
}
The main problem is that for some routes I don't get the map filled, only the rectangle part.
Any tip there?
Thank you very much.

Related

R Incorrect Number of Dimensions Error from data.frame Assignment

When running the code below I get the error:
Error in data[, 4] : incorrect number of dimensions
Both data[,4] and goals have the same length (480) so I don't understand what the issue is. Data is a data.frame with 4 columns and goals is a length 480 vector.
library(glmmTMB)
simulate_games = function(data) {
mod <- glmmTMB(goals ~ home + (1|attack) + (1|defence), poisson, data=data, REML=TRUE)
goals = predict(mod,newdata = data, type = "response")
data[,4] = goals #Error here
res = comp_ranks(goals)[,2] #comp_ranks is a user defined function
for (i in 1:1000) {
data[,4] = rpois(480,goals)
res = cbind(res,comp_ranks(data)[,2])
}
return(res)
}
long <- read.csv("https://www.math.ntnu.no/emner/TMA4315/2020h/eliteserie.csv", colClasses = c("factor","factor","factor","numeric"))
simulate_games(long)
Here is also the comp_ranks function although I don't think its whats causing the error.
comp_ranks = function(data) {
goals = data[,4]
goals = goals[!is.na(goals)]
teams = unique(data[,1])
teams_points = cbind.data.frame(0,teams)
goals_scored = cbind.data.frame(0,teams)
goals_conceded = cbind.data.frame(0,teams)
for (i in 1:length(teams)) {
tfs = data[,1] == teams[i]
tfc = data[,2] == teams[i]
goals_scored[i,1] = sum(na.omit(goals[tfs]))
goals_conceded[i,1] = sum(na.omit(goals[tfc]))
}
for (i in seq(1,length(goals)-1,2)) {
idx_1 = match(data[,1][i],teams)
idx_2 = match(data[,1][i+1],teams)
if (goals[i] - goals[i+1] > 0) {
teams_points[idx_1,1] = teams_points[idx_1,1] + 3
}
else if (goals[i] - goals[i+1] < 0 ) {
teams_points[idx_2,1] = teams_points[idx_2,1] + 3
}
else {
teams_points[idx_1,1] = teams_points[idx_1,1] + 1
teams_points[idx_2,1] = teams_points[idx_2,1] + 1
}
}
#Sort data.frame by ranks
colnames(teams_points) = c("Points","Teams")
teams_points = teams_points[with(teams_points, order(-Points)), ]
diff = goals_scored[,1] - goals_conceded[,1]
goals_diff = cbind.data.frame(diff,teams)
teams_ranked = teams_points[,2]
for (i in 1:length(teams_points)) {
for (j in 1:length(teams_points)) {
if(j != i) {
if (teams_points[i,1] == teams_points[j,1]) {
if (goals_diff[i,1] == goals_diff[j,1]) {
if (goals_scored[i,1] < goals_scored[j,1] ) {
teams_ranked = replace(teams_ranked,c(i,j), teams_ranked[c(j,i)])
teams_points[,2] = teams_ranked
}
else if(goals_diff[i,1] < goals_diff[j,1] ) {
teams_ranked = replace(teams_ranked,c(i,j), teams_ranked[c(j,i)])
teams_points[,2] = teams_ranked
}
}
}
}
}
}
ranks = data.frame("Ranks" = c(1:16), "Teams" = teams_points[,2], "Points" = teams_points[,1])
return(ranks)
}

R studio / R script Error in .getReactiveEnvironment()$currentContext() :

Hi I'm trying to make a server in R shiny, I have the following code:
output$map <- renderTmap( {
cat("renderTmap (initialise map) | ")
if (input$varID == "Temperture") {
tm_basemap(c("Esri.OceanBasemap","CartoDB.DarkMatter","OpenStreetMap.Mapnik"),alpha = 0.7) +
weLayerF(weather,"temp", "feel_like" ,"-RdYlBu", seq(from = 0, to = 45, by = 2))
} else if (input$varID == "humidity") {
tm_basemap(c("Esri.OceanBasemap","CartoDB.DarkMatter","OpenStreetMap.Mapnik"),alpha = 0.7) +
weLayer(weather,"humidity", "YlOrRd", seq(from = 0, to = 100, by = 2))
} else if (input$varID == "Pressure") {
tm_basemap(c("Esri.OceanBasemap","CartoDB.DarkMatter","OpenStreetMap.Mapnik"),alpha = 0.7) +
weLayer(weather,"pressure", "PuBu", seq(from = 980, to = 1030, by = 2))
} else if (input$varID == "Visablity") {
tm_basemap(c("Esri.OceanBasemap","CartoDB.DarkMatter","OpenStreetMap.Mapnik"),alpha = 0.7) +
weLayer(weather,"visib", "-Greys", seq(from = 0, to = 10000, by = 500))
} else if (input$varID == "Wind") {
tm_basemap(c("Esri.OceanBasemap","CartoDB.DarkMatter","OpenStreetMap.Mapnik"),alpha = 0.7) +
weLayerF(weather,"wind_speed","wind_degree", "Greys", seq(from = 0, to = 30, by = 2))
} else {
tm_basemap(c("Esri.OceanBasemap","CartoDB.DarkMatter","OpenStreetMap.Mapnik"),alpha = 0.7) +
weLayerF(weather,"temp", "feel_like" ,"-RdYlBu", seq(from = -10, to = 45, by = 5))
}
})
And I'm getting the error:
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 expression or observer.)
Can someone help please?

how can I use return values from a function?

I use some functions in my main code which they return some values (scalar).
These values will compare in main code.
But when I run the code it has this Error:
"ERROR: MethodError: no method matching isless(::Array{Float64,1}, ::Array{Float64,1})"
Please help me. This is the code:
using JuMP, CPLEX
ZT = [-36 ; -244.5 ];
ZB = [27.149 ; -288.747];
M = 5;
model = CreateModel();
WES = model[1];
f1 = model[2]; f2 = model[3];
rf1 = model[4]; rf2 = model[5];
lf1 = model[6]; lf2 = model[7];
x = WES[:x] ; y = WES[:y] ;
JuMP.setRHS( rf1, ZB[1] ); JuMP.setRHS( lf1, ZT[1] );
JuMP.setRHS( rf2, ZT[2] ); JuMP.setRHS( lf2, ZB[2] );
ZI, ofvInt = Intpoint( ZB, ZT );
Hgap, Vgap, ZG, ofvGap = Gappoint( ZB, ZT );
if ofvInt !== NaN
y = ZI[2];
elseif ofvGap !== NaN
if isless( Hgap, M + Vgap ) # "MethodError: no method matching
# isless(::Array{Float64,1}, ::Array{Float64,1})"
y = ZG[1]
end
y = ZG[2];
else
y = ( ZB[2] + ZT[2] ) / 2;
end

Attempting to call my function n times

My goal is to call my dice roll function n times where n is the amount of turns. This is a monopoly simulated turn and as such the doubles will roll again, and triples will go to jail. I cannot figure out how to set this up so my function will be
Diceroll <- Function ( Turns, Sides)
Diceroll <- function(Turn,sides){
Turn = as.integer(0)
First_roll = as.integer(0)
Second_roll = as.integer(0)
Third_roll = as.integer(0)
Fourth_roll = as.integer(0)
Fifth_roll = as.integer(0)
Sixth_roll = as.integer(0)
Total = as.integer(0)
i = as.integer(1)
for (i in 1:Turn) {
First_roll = sample(1:sides,size = 1)
Second_roll = sample(1:sides,size = 1)
if(First_roll[1] == Second_roll[1]) {
Third_roll = sample(1:sides,size = 1)
Fourth_roll = sample(1:sides,size = 1)
}
if(Third_roll[1] == Fourth_roll[1] & Third_roll[1] + Fourth_roll[1] > 0) {
Fifth_roll= sample(1:sides,size = 1)
Sixth_roll = sample(1:sides,size = 1)
}
if(Fifth_roll[1] == Sixth_roll[1] & Fifth_roll[1] + Sixth_roll[1] > 0) { Total = "Jail"
}
else {
Total = (First_roll[1] + Second_roll[1] + Third_roll[1] +
Fourth_roll[1] + Fifth_roll[1] + Sixth_roll[1]) }
return(Total)
}
}
Here is my attempt but it is only listed the value of one roll.

Oracle R Enterprise: Error ORE object has no unique key

I need to make a function which call Oracle R Enterprise ore.corr function and output result as a data.frame.
My R code here:
f_sts_corelation =
function(dat, target_col="",corr_type="spearman",group_by="")
{
v_target_col = gsub("\n","",target_col, fixed = TRUE);
v_target_col_list = "";
library("gdata");
for (s_name in strsplit(v_target_col,",")[[1]])
{
n_pos = regexpr(".",s_name,fixed = TRUE);
if (n_pos > 0)
{
s_name = substring(s_name,n_pos+1);
}
s_name = gsub("\"","",s_name, fixed = TRUE);
if (is.numeric(dat[,trim(s_name)]))
{
if (nchar(v_target_col_list)== 0)
{
v_target_col_list = trim(s_name)
}
else
{
v_target_col_list =paste(v_target_col_list,",",trim(s_name))
}
}
}
ore.data = ore.push(dat)
v_id = c()
v_group=c()
v_row = c()
v_col = c()
v_statistic = c()
v_pvalue = c()
v_df = c()
#group_by = ""
s_group_by = trim(gsub("\n","",group_by, fixed = TRUE));
if (nchar(s_group_by) > 0)
{
n_pos = regexpr(".",s_group_by,fixed = TRUE);
if (n_pos > 0)
{
s_group_by = substring(s_group_by,n_pos+1);
}
s_group_by = trim(gsub("\"","",s_group_by, fixed = TRUE));
ore.corr.res = ore.corr(ore.data,var = v_target_col_list, group.by = s_group_by)
for (i in 1:length(ore.corr.res))
{
if (i == 1)
{
v_group = rep(names(ore.corr.res[i]),length(ore.corr.res[[i]]$ROW))
v_row = as.vector(ore.corr.res[[i]]$ROW)
v_col = as.vector(ore.corr.res[[i]]$COL)
v_statistic = as.vector(ore.corr.res[[i]][,3])
v_pvalue = as.vector(ore.corr.res[[i]][,4])
v_df = as.vector(ore.corr.res[[i]][,5])
}
else
{
v_group = c(v_group,rep(names(ore.corr.res[i]),length(ore.corr.res[[i]]$ROW)))
v_row = c(v_row,as.vector(ore.corr.res[[i]]$ROW))
v_col = c(v_col,as.vector(ore.corr.res[[i]]$COL))
v_statistic = c(v_statistic,as.vector(ore.corr.res[[i]][,3]))
v_pvalue = c(v_pvalue,as.vector(ore.corr.res[[i]][,4]))
v_df = c(v_df,as.vector(ore.corr.res[[i]][,5]))
}
}
}
else if(nchar(s_group_by) == 0)
{
ore.corr.res = ore.corr(ore.data,var = v_target_col_list)
v_group = rep(" ",length(ore.corr.res$ROW))
v_row = as.vector(ore.corr.res$ROW)
v_col = as.vector(ore.corr.res$COL)
v_statistic = as.vector(ore.corr.res[,3])
v_pvalue = as.vector(ore.corr.res[,4])
v_df = as.vector(ore.corr.res[,5])
}
df_res = data.frame(Group_by = v_group,
Row = v_row,
Col = v_col,
Statistic = v_statistic,
P_Value = v_pvalue,
DF = v_df)
}
After that, I run the function by following script:
dat = iris;
corr_type="spearman";
V_target_col= '"IRIS_N$10002"."Sepal.Length",
"IRIS_N$10002"."Sepal.Width",
"IRIS_N$10002"."Petal.Width",
"IRIS_N$10002"."Petal.Length"';
group_by =
'
"IRIS_N$10002"."Species"
'
df_result = f_sts_corelation(dat,target_col = target_col, group_by = group_by)
But following error happen.
Error: ORE object has no unique key
I have tried to run each R command inside my function step by step and I sure that the Error happen from the last R command:
df_res = data.frame(Group_by = v_group,
Row = v_row,
Col = v_col,
Statistic = v_statistic,
P_Value = v_pvalue,
DF = v_df)
I don't know how to avoid this error.

Resources