Reputation: 737
I want to ask the user to enter a list of values in integers. User can enter a single value or a bunch of multiple values like 1 2 3 (spcae or comma seperated) Then use the data entered for further computation.
I am using the following code
EXP <- as.integer(readline("enter the experiment numbers (You can include multiple number of experiments with space seperate) \n"))
Here with or without integer, whatever values I store inside EXP, I cant use them in further programming. Like below
gg2 <- subset((aggregate(cbind(var)~En+Mn+hours,a, FUN=mean)),(aggregate(cbind(var)~En+Mn+hours,a, FUN=mean))$En == c(EXP))
The above command fails for condition $En == c(EXP), Because the values entered through readline doesnt work.
How do I do?
2) The above question is regarding the integers. The same solution doesn't work in case of characters as input. just like below
prompt1 <- "enter the variable_name \n"
var <- as.character((readline(prompt1))[[1]])
Here if I enter Ph for the prompt then var will store like "Ph", this value I cant use inside the command line
gg2 <- subset((aggregate(cbind(var)~En+Mn+hours,a, FUN=mean)),(aggregate(cbind(var)~En+Mn+hours,a, FUN=mean))$En == c(EXP))
It will throw error like, variable lengths differ (found for 'En') but if I replace var with the actual value like Ph, then it will work fine.
The dataset is uploaded here... Dataset a <- dataset imported into
Consider names(a) = 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 doing this
v1 <- quote(var1 <- as.numeric(readline('Enter a number from 1 to 15: ')))
eval(v1)
v2 <- quote(var2 <- as.numeric(readline('Enter a number from 1 to 15: ')))
eval(v2)
variable1 <- as.name(names(a[var1]))
variable2 <- as.name(names(a[var2]))
gg4 <- subset((aggregate(cbind(variable1,variable2)~Ei+Mi+hours,a, FUN=mean)))
Reading the user input and then using the user input into the command to aggregate the required data. The above command is using the variable1 and variable2 and so its not working and its throwing error. If I replace variable1 and variable2 with actual column names like Nphy, Cphy then everything is working fine.
like this gg4 <- subset((aggregate(cbind(Nphy,Cphy)~Ei+Mi+hours,a, FUN=mean)))
So all I want to know how do we make the user input work inside the command lines?
Upvotes: 2
Views: 17335
Reputation: 21
You can try scan()
. Here is an example:
> x <- scan()
1: 4
2: 6
3: 10
4: 11
5:
Read 4 items
> x
[1] 4 6 10 11
Upvotes: 2
Reputation: 89057
The first part should be:
prompt <- "enter the experiment numbers (space-separated list) \n"
EXP <- as.integer(strsplit(readline(prompt), " ")[[1]])
The second part is more obscure (can you explain what your data looks like and what you want to do?) but maybe you are looking for something like this:
sub.a <- subset(a, En %in% EXP)
aggregate(var~En+Mn+hours, sub.a, FUN=mean)
I don't see the link with ggplot2
. If none, please delete any mention of it.
Upvotes: 6