R Shiny : how to use several checkboxInput values as combination? - r

I am new to R, so I figure there is probably an easy way to do this. I searched here on Stack Overflow and couldn’t find a similar question, so I apologize if I missed it.
I have 9 checkboxInputs in my ui.R, numbered from "1" to "9". The values are in character format.
If I check boxes "3" and "9", the value in the table is "39".
If I check boxes "4", "6", "8" and "9", the value to serach in the table is "4689". There are a lot of combinations.
So, I would like to know how can I concatenate all values from the checked boxes, in reactive environment, to search the right combination in a data frame ? I think it's something too complicated for a newbie as me. I have to get more experience.
Thank you very much for setting me on the right path.

Related

Can the elements of vector belong to different classes in R?

I have just started R programming language and this is something I wanted to clear it up. can someone explain this.
or
Is it that the elements must belong to the same class?
I have read articles about this, but is still confusing so turning to the community
Try it for yourself:
v = c(1,2,'3')
This might make you think R accepts multiple classes, but if you print it, you get the following:
[1] "1" "2" "3"
They're all strings. So, no, you might type a vector with multiple classes, but R will try to turn it into a common class.

if function to create a new variable from three dummies in R

I need R to create a new categorical variable using three dummies that I have in my dataset. I am trying to use "if" and "else if". I have managed to do it in other sotfware but I can't find the solution in R. Would someone please take a look at my code and tell me what's wrong?
data$newvariable<- if(data$dummyA[1]) {
(data$newvariable<-"1")
} else if(data$dummyB[1]){
(data$newvariable<-"2")
} else if(data$dummyC[1]){
(data$newvariable<-"3") }
I hope not to duplicate questions. Sorry! And thank you in advance!
What I get as a result is all cases categorized as "1", which makes me think there is something missing between the first if statement and the following one. But I can't find the solution, help, please!
THANKS!
You can use ifelse() to do the job.
data$newvariable <- ifelse(data$dummyA[1], "1",
ifelse(data$dummyB[1], "2",
ifelse(data$dummyC[1], "3")))
This should work, but can not test it without a reproducible example.

Removing columns from an index request in a data frame

Hi I currently have the code:
matrixed.data <- data.matrix(df[1:row.dim,7:col.dim])
Where the row.dim and col.dim are variables for the size of the whole frame. I would like to remove the column "df$WEATHER" that is included in the col.dim selection but don't know how to word it. I have tried adding - df$WEATHER and !df$WEATHER inside the bracket but fear I'm misinterpreting the scope of these commands.
Is it possible to do this without creating a new col.dim variable; I'm trying to keep the code as limitless as possible as the data frame may increase in size in the future.
Thank you digEmAll! I thought it would be reasonably simple I'm just a bit too green at R to think of something like that. For others what worked for me was:
(df[1:row.dim, setdiff(7:col.dim,which(names(df) == "WEATHER"))])

R ifelse is erroneously replacing text with integers

I have some data that I'm working with from a Udacity course (Link: Reddit Survey Responses). I'm trying to simplify the Employment Status variable by replacing any multi-word values with single word alternates using
RS$employment.status <- ifelse(RS$employment.status == "Not employed, but looking for work",
"Unemployed", RS$employment.status)
However, when I run the code any values that aren't supposed to be replaced are replaced with numeric values. Given that the else case is to use the field's value, I'm not sure why the text isn't preserved as-is.
Here's a screenshot of the initial data
And the after
So if anyone could point out
why the substitution is being made when it doesn't look like it should be;
what would be the correct way to accomplish what I'm trying to achieve;
it would be much appreciated.
The problem is that this variable is set as a Factor, so to fix your problem you can either add this argument when you read your data stringsAsFactors = FALSE or you could do this:
RS$employment.status <- ifelse(RS$employment.status == "Not employed, but looking for work",
"Unemployed", as.character(RS$employment.status))

dc.js pie chart sum example

I have records like this :
{
{"Pass": "10", "Fail": "20", "Untested": "40"}
{"Pass": "20", "Fail": "40", "Untested": "50"}
{"Pass": "30", "Fail": "50", "Untested": "60"}
...
}
Obviously, total = pass + fail + untested. I want to sum up each of these values and then finally show a pie chart using dc.js that shows total Pass, total Fail, total Untested. I couldnt find a good example.
In the example below, each record is converted to a binary loss/gain value. Then the pie chart shows the aggregated loss/gain. I need to aggregate at the collection level, not individual record level. How can I do that ?
Any help is much appreciated.
Ref:
http://nickqizhu.github.io/dc.js/
Take a look at the example in this jsfiddle
The trick is to modify your input data, so that you can create an dimension for all records on the result. So the data should look like this:
[{"result":"Pass","value":"10","_id":0},
{"result":"Fail","value":"20","_id":0},
{"result":"Untested","value":"40","_id":0},
{"result":"Pass","value":"20","_id":1},
{"result":"Fail","value":"40","_id":1},
{"result":"Untested","value":"50","_id":1},
{"result":"Pass","value":"30","_id":2},
{"result":"Fail","value":"50","_id":2},
{"result":"Untested","value":"60","_id":2}]
I did this using a new js library I just created called melt, but it would be fairly simple to duplicate the same logic yourself. Also here is another example in a recent user list discussion.

Resources