r - Reading a user input (character or string of letters) into ggplot command inside a switch statement or a nested ifelse (with functions in it) -


i have code like

aa <- as.integer(readline("select number"))  switch(aa,  1={       num <-as.integer(readline("select 1 of options \n"))       print('you have selected option 1')       #reading user data       var <- readline("enter variable name \n")       #aggregating 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 [dataset][1]

i have loaded dataset 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 getting error like

source ("switch_statement_check.r") select 1 of options  1 [1] "you have selected option 1" enter variable name  nphy error in eval(expr, envir, enclos) :    (list) object cannot coerced type 'double' >  

gg1 getting data fine. dont know make variable entered user work in ggplot command. please suggest solution 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), @ = 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 below suggestions have tried lot not working.

summarizing say:

var <- readline("enter variable name") cant use get(var) inside command not inside ggplot, wont work.

gg1$var doesnt work, after changing column names. have solution or should choose import excel sheet, thats better?

tried if else , functions

  fun1 <- function() {   print('you have selected option 1')   <- as.character((readline("enter variable name \n")))   gg1 <- aggregate(cbind(get(my))~mi+hours,a, fun=mean)   names(gg1)[3] <-   #print(names(gg1))   ggplot (gg1,aes_string(x="hours",y=(my),group="mi",color="mi")) + geom_point() }    <- as.integer(readline("enter 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...:(

it's naming problem. running code 1 line @ time, can see gg1 looks like:

> gg1 <- aggregate(cbind(get(var))~mi+hours,a, fun=mean) > head(gg1)   mi hours        v1 1  1     1 0.1021998 2  2     1 0.1021966 3  3     1 0.1022071 4  4     1 0.1022026 5  5     1 0.1022019 6  6     1 0.1022035 

notice column named v1, not nphy. correcting y = v1 in ggplot seems work fine.

if want column name preserved, rename after aggregate.

gg1 <- aggregate(cbind(get(var))~mi+hours,a, fun=mean) names(gg1)[3] <- var 

rather using aes() get(var), i'd recommend using aes_string() in case, i.e., aes_string(x = "hours", y = var, group = "mi", fill= "mi", color = "mi")).

edits:

in addition aes_string in ggplot, need eval(var) in aggregate formula if want in function. works fine me:

makeplot <- function(var = readline("enter variable name\n")) {     gg1 <- aggregate(cbind(get(eval(var))) ~ mi + hours, a, fun=mean)     names(gg1)[3] <- var     print(ggplot(gg1, aes_string(x = "hours", y = var, group = "mi",                           fill= "mi", color = "mi")) +         geom_point()) }  # works argument: makeplot(var = "nphy")  # works interactively: makeplot() # enter nphy @ console when prompted 

notice both aes_string in ggplot , get(eval(var)) in aggregate. assumes data.frame named a , exists in global environment.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -