Greeting all,
I was wondering how can I call a data.frame or an object outside a function class? i tried to use <<- but with no use.
simply put
I have a function
mod0 = function(Q1,Q2){ #where Q1 and Q2 are numeric input in the shiny app ui
A= Q1+2
B=Q2+A
long= 37.6543
lat= 127.5432
C <<- data.frame(A,B, lat, long)
return (list(C))
}
#later I want to use C values to add markers inside leaflet but I cannot call C data.frame outside the function class. it gives me an error of [there is no object called "C"]
# I want to call C dataframe outside the function here and use it for leaflet marker values as following
data <- C #C is the dataframe from the mod0 function
data <-data[complete.cases(data),] #remove empty cell data
data$long <- as.numeric(data$long) #to make sure this is a numeric column not text
data$lat <- as.numeric(data$lat)
data$results <- as.numeric(data$results)
thank you in advance for any reply and advises
best regards
Ahmed Ali
PhD Candidate
Chung Ang University
If you return C as the value of the function, you can use it elsewhere.
mod0 = function(Q1,Q2){ #where Q1 and Q2 are numeric input in the shiny app ui
A= Q1+2
B=Q2+A
long= 37.6543
lat= 127.5432
C <- data.frame(A,B, lat, long)
return (C)
}
DF <- mod0(1,2)
DF
#> A B lat long
#> 1 3 5 127.5432 37.6543
Created on 2019-10-17 by the reprex package (v0.3.0.9000)
mod0 = function(Q1,Q2){ #where Q1 and Q2 are numeric input in the shiny app ui
A= Q1+2
B=Q2+A
long= 37.6543
lat= 127.5432
C <- data.frame(A,B, lat, long)
D<- data.frame(A,lat)
return (list(C, D))
}
ListOfDF <- mod0(1,2)
ListOfDF
#> [[1]]
#> A B lat long
#> 1 3 5 127.5432 37.6543
#>
#> [[2]]
#> A lat
#> 1 3 127.5432
DF1 <- ListOfDF[[1]]
DF2 <- ListOfDF[[2]]
DF1
#> A B lat long
#> 1 3 5 127.5432 37.6543
DF2
#> A lat
#> 1 3 127.5432
Created on 2019-10-17 by the reprex package (v0.3.0.9000)
thanks alot FJCC,
that really solved a lot of problems for me,
I used to repeat copy/paste the whole Function to return a different dataset with mod0 , mod1, ..
now with your guided example I can get multiple outputs from the same function. thanks alot and have a blessed weekend.