I have a scenario where I am writing a report in R Markdown and I want text to appear based on the values of 3 different variables. In the example below, I want the text to appear if a is greater than b and c. I have found out that you use eval but Ive only managed to use this with 2 variables as shown in the code below. How do I amend the eval part below to include a, b and c?
a<-6
b<-3
c<-2
```{r conditional_block, echo=FALSE, results='asis', eval=a>b}
cat("6 is greater than 3 and is greater than 2")
```
You can use any kind of conditional logic within the chunk option. If you have more than one condition which needs to be met, you can use the & symbol between them which will require both to be TRUE. Here is a basic example:
a<-6
b<-3
c<-2
a>b & a>c
TRUE
# Make a less than c but bigger than b
a <- 2.5
a>b & a>c
FALSE
If you want to find out more about the use of operators in R, I recommend this page: https://www.statmethods.net/management/operators.html
Related
I am trying to do a simple (I think) thing. I want to have a TextField and some variable bound to it and be able to modify this variable and by that the text displayed by the TextField.
I tried:
#bind x TextField()
print(x)
It works fine and whatever I type in the tex field is printed.
Now, what if I want programmatically change x and display it in the TextField?
if I write x="trelemorele", I am getting:
error of "Multiple definitions for x", if it is in a separate cell
no error but my TextField disappear, if it is in the same cell within begin .. end
Any ideas?
What you're trying to do is fundamentally at odds with Pluto's reactivity.
At the heart of the idea of Pluto is that cells are connected by a dependency graph which Pluto automatically figures out for you from the code you've written, and which it uses to determine which cells need to be updated when another cell changes, because they depend on the changed cell.
So in simple terms if you have:
# Cell 1
x = 1
# Cell 2
y = x^2
Pluto will determine that cell 2 depends on cell 1, as x is defined there and used as an input in cell 2. But what if you now add
# Cell 3
x = 2
? Well you've now broken reactivity - Pluto can't work out the result of cell 2 anymore because there's now two competing definitions of x. Now you might say "well but I've put in the definition on cell 3 after the one in cell 1, so clearly it should be using x = 2", but that's exactly what Pluto wants to avoid. This is possible in Jupyter notebooks - the value of x would depend on whether cell 1 or cell 3 was executed last, but this means there's "hidden state" in the notebook; it is not possible from just looking at cell 1 or cell 3 whether their definitions haven't been overwritten elsewhere.
When you do #bind x TextField() that's roughly the same as doing x = TextField() (the actual generate code is a bit more complicated, but in the end it assigns something to the variable x), so doing that in one cell and x = "trelemorele" is the same thing.
Now as to your second point, Pluto does not complain because you now have an unambiguous ouptut for what should be bound to x after executing your cell - it's the last assignment to x, which is the same in a normal Julia session:
julia> begin
x = 1
x = 2
end;
julia> x
2
In addition to the answer of Nils, there are ways to work around reactivity in Pluto using mutable objects.
# ╔═╡ 652416e0-e8a3-11ec-09d4-37c76de750be
begin
using PlutoUI
# make sure the refs are executed first by putting them together with the using
default_txt = Ref("Hello World!")
in_txt = Ref("")
end
# ╔═╡ 9c26218a-6c4a-434e-aad5-a6e0fe078949
#bind txt TextField(; default=default_txt[])
# ╔═╡ 32b3fa56-d922-4d37-baec-e4917a7ad26b
#bind upd Button("update")
# ╔═╡ 5204277d-5411-4c60-89d0-c4ffeb5b0994
in_txt[] = txt # use 2nd mutable object to prevent auto-updates of next cell if txt changes
# ╔═╡ b6b39250-6a94-4e2d-9fde-acefb9dae662
begin
upd
default_txt[] = in_txt[] # only update default_txt if button is pressed
end
Be aware that this is an advanced pattern that goes against the standard Pluto workflow and should only be used when really needed.
I am learning graphical analysis using R. Here is the code, which I can not understand.
barplotVS <- barplot(table(mtcarsData$vs), xlab="Type of engine")
text(barplotVS,table(mtcarsData$vs)/2,table(mtcarsData$vs),cex=1.25)
The output is like below. I can not understand the function of text(), I googled the text() function, which shows that the parameter of text(x,y) is numeric vectors of coordinates where the text labels should be written. Can anyone tell me what is barplotVS,table(mtcarsData$vs)/2,table(mtcarsData$vs),cex=1.25 in my code.
barplotVS <- barplot(table(mtcarsData$vs), xlab="Type of engine")
print(barplotVS)
outputs:
[,1]
[1,] 0.7
[2,] 1.9
These are the positions where the center of the bars in the barplot are on the x axis.
print(table(mtcarsData$vs))
outputs:
0 1
18 14
the numbers below are the occurrences of each value that is present in mtcarsData$vs and the numbers above are the actual value that is counted.
When you run the function:
text(barplotVS,table(mtcarsData$vs)/2,table(mtcarsData$vs),cex=1.25)
the first value will be the x positions where to put the labels (i.e. 0.7 and 1.9), the second parameter will be the y positions set in this case to total counts divided by two (i.e. 9 and 7) meaning to put the labels halfway in the bars, the third will be the labels (i.e. 18 and 14) and finally cex is a value that allows to change the size of the font.
Anyway R has in general a good documentation that you can call by using the ? operator (as suggested in the comments). In order to understand try to run the code and check what each variable contains with print or str functions. If you use a IDE (e.g. RStudio) have the content of the variables in a graphical panel so you don't event need to print.
I want to plot the data by using function(). My data consists of 4 vectors, say a, b, c and d.
I have to plot them by choice of vector at that time.
For example
i want to plot vector a and c then graph must have 2 lines....
if i want 2 plot all 4 vectors then there must be 4 lines in graph.
Till I have tried switch() but i think thats not suitable related to my work.
Is it even possible to write such code in an anonymous function ?
If yes, what is the right way, and if not, is there any workaround ?
I have a very basic question on importing STATA files into R, and I tried searching the forum but could not find what I was looking for.
I have a DHS file (AR - HIV test results) and it only has several fields as follows, after importing into R using the foreign package:
AR_HIV_dataset <- read.dta("RWAR71FL.DTA") #HIV test result file
My question is on how to filter some cases with dplyr based on the value of a variable e.g. HIV03. Using the structure command, the variable HIV03 is displayed as "HIV negative", "HIV positive", etc:
$ hiv03 : Factor w/ 8 levels "hiv negative",..: 1 1 1 1 1 1 1 1 1 1 ...
but the actual data values stored are just 0 or 1. However, I cannot refer to these numerical values as the filter command seems to need me to specify the label values, e.g.
filter(AR_HIV_dataset,hiv03=="hiv negative")
this will return the required cases, but I would like to be able to use the following command instead (using the actual values)
filter(AR_HIV_dataset, hiv03==0)
But if I do that,this returns an error.
Can you let me know what I need to change in order to use the second line of code instead?
Thanks in advance for your kind support.
Using the haven package (http://haven.tidyverse.org/) to import the stata file may be helpful especially if you are using dplyr given both packages are part of the tidyverse. The vignette on semantics may be particularly useful when looking at how variables in stata etc are treated when imported into R.
Two possible solutions are:
filter(AR_HIV_dataset, as.numeric(hiv03)==0)
Or, somewhat nicer
filter(AR_HIV_dataset, hiv03 == levels( hiv03 )[1] )
I have a csv file (tab deliminated) which contains 2 columns which looks like such
5 0
6 0
9 0
8 1
"+5000 lines similar lines"
I am attempting to create a ROC plot using ROCR.
This is what I have tried so far:
p<-read.csv(file="forROC.csv", head=TRUE, sep="\t")
pred<-prediction(p[1],p[2])
The second line gives me an error: Error in prediction(p[1], p[2]) : Number of classes is not equal to 2.
ROCR currently supports only evaluation of binary classification tasks.
I am not sure what the error is. Is there something wrong with my CSV file?
My guess is that your array indexing isn't setup properly. If you read in that CSV file, you should expect a data.frame (think matrix or 2D array, depending on your background) with two columns and 5,000+ rows.
So your current call to p[1] or p[2] aren't especially meaningful. You probably want to access the first and second column of that data.frame, which you can do using the syntax of p[,1] for the first column and p[,2] for the second.
The specific error you're encountering, however, is a complaint that the "truth" variable you're using isn't binary. It seems that your file is setup to have an output of 1 and 0, so this error may go away once you properly access your array. But if you encounter this in the future, just be sure to binarize your truth data before you use it. For instance:
p[,2] <- p[,2] != 0
Would set the values to FALSE if it's a zero, and TRUE for each non-zero cell in the column.