Reading a user input (character or string of letters) into ggplot command inside a switch statement or a nested ifelse (with functions in it)
Posted
by
statisticalbeginner
on Stack Overflow
See other posts from Stack Overflow
or by statisticalbeginner
Published on 2014-06-02T18:30:39Z
Indexed on
2014/06/05
3:26 UTC
Read the original article
Hit count: 220
I have code like
AA <- as.integer(readline("Select any number"))
switch(AA,
1={
num <-as.integer(readline("Select any one of the options \n"))
print('You have selected option 1')
#reading user data
var <- readline("enter the variable name \n")
#aggregating the data based on required condition
gg1 <- aggregate(cbind(get(var))~Mi+hours,a, FUN=mean)
#Ploting
ggplot(gg1, aes(x = hours, y = get(var), group = Mi, fill = Mi, color = Mi)) + geom_point() + geom_smooth(stat="smooth", alpha = I(0.01))
},
2={
print('bar')
},
{
print('default')
}
)
The dataset is [dataset][1]
I have loaded the dataset into object list
a <- read.table(file.choose(), header=FALSE,col.names= c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH"))
I am getting error like
source ("switch_statement_check.R")
Select any one of the options
1
[1] "You have selected option 1"
enter the variable name
Nphy
Error in eval(expr, envir, enclos) :
(list) object cannot be coerced to type 'double'
>
gg1 is getting data that is fine. I dont know what to do to make the variable entered by user to work in that ggplot command. Please suggest any solution for this.
The dput output
structure(list(Ei = c(1L, 1L, 1L, 1L, 1L, 1L), Mi = c(1L, 1L,
1L, 1L, 1L, 1L), hours = 1:6, Nphy = c(0.1023488, 0.104524, 0.1064772,
0.1081702, 0.1095905, 0.110759), Cphy = c(0.6534707, 0.6448216,
0.6369597, 0.6299084, 0.6239005, 0.6191941), CHLphy = c(0.1053458,
0.110325, 0.1148174, 0.1187672, 0.122146, 0.1249877), Nhet = c(0.04994161,
0.04988347, 0.04982555, 0.04976784, 0.04971029, 0.04965285),
Chet = c(0.3308593, 0.3304699, 0.3300819, 0.3296952, 0.3293089,
0.3289243), Ndet = c(0.04991916, 0.04984045, 0.04976363,
0.0496884, 0.04961446, 0.04954156), Cdet = c(0.3307085, 0.3301691,
0.3296314, 0.3290949, 0.3285598, 0.3280252), DON = c(0.05042275,
0.05085697, 0.05130091, 0.05175249, 0.05220978, 0.05267118
), DOC = c(49.76304, 49.52745, 49.29323, 49.06034, 48.82878,
48.59851), DIN = c(14.9933, 14.98729, 14.98221, 14.9781,
14.97485, 14.97225), DIC = c(2050.132, 2050.264, 2050.396,
2050.524, 2050.641, 2050.758), AT = c(2150.007, 2150.007,
2150.007, 2150.007, 2150.007, 2150.007), dCCHO = c(0.964222,
0.930869, 0.8997098, 0.870544, 0.843196, 0.8175117), TEPC = c(0.1339044,
0.1652179, 0.1941872, 0.2210289, 0.2459341, 0.2690721), Ncocco = c(0.1040715,
0.1076058, 0.1104229, 0.1125141, 0.1140222, 0.1151228), Ccocco = c(0.6500288,
0.6386706, 0.6291149, 0.6213265, 0.6152447, 0.6108502), CHLcocco = c(0.1087667,
0.1164099, 0.1225822, 0.1273103, 0.1308843, 0.1336465), PICcocco = c(0.1000664,
0.1001396, 0.1007908, 0.101836, 0.1034179, 0.1055634), par = c(0,
0, 0.8695131, 1.551317, 2.777707, 4.814341), Temp = c(9.9,
9.9, 9.9, 9.9, 9.9, 9.9), Sal = c(31.31, 31.31, 31.31, 31.31,
31.31, 31.31), co2atm = c(370, 370, 370, 370, 370, 370),
u10 = c(0.01, 0.01, 0.01, 0.01, 0.01, 0.01), dicfl = c(-2.963256,
-2.971632, -2.980446, -2.989259, -2.997877, -3.005702), co2ppm = c(565.1855,
565.7373, 566.3179, 566.8983, 567.466, 567.9814), co2mol = c(0.02562326,
0.02564828, 0.0256746, 0.02570091, 0.02572665, 0.02575002
), pH = c(7.879427, 7.879042, 7.878636, 7.878231, 7.877835,
7.877475)), .Names = c("Ei", "Mi", "hours", "Nphy", "Cphy",
"CHLphy", "Nhet", "Chet", "Ndet", "Cdet", "DON", "DOC", "DIN",
"DIC", "AT", "dCCHO", "TEPC", "Ncocco", "Ccocco", "CHLcocco",
"PICcocco", "par", "Temp", "Sal", "co2atm", "u10", "dicfl", "co2ppm",
"co2mol", "pH"), row.names = c(NA, 6L), class = "data.frame")
As per the below suggestions I have tried a lot but it is not working.
Summarizing I will say:
var <- readline("enter a variable name") I cant use get(var) inside any command but not inside ggplot, it wont work.
gg1$var it also doesnt work, even after changing the column names. Does it have a solution or should I just choose to import from an excel sheet, thats better?
Tried with if else and functions
fun1 <- function() {
print('You have selected option 1')
my <- as.character((readline("enter the variable name \n")))
gg1 <- aggregate(cbind(get(my))~Mi+hours,a, FUN=mean)
names(gg1)[3] <- my
#print(names(gg1))
ggplot (gg1,aes_string(x="hours",y=(my),group="Mi",color="Mi")) + geom_point()
}
my <- as.integer(readline("enter a number"))
ifelse(my == 1,fun1(),"")
ifelse(my == 2,print ("its 2"),"")
ifelse(my == 3,print ("its 3"),"")
ifelse(my != (1 || 2|| 3) ,print("wrong number"),"")
Not working either...:(
© Stack Overflow or respective owner