Using "if else" (and possibly other) conditionals inside \Sexpr{} - sweave

Is it possible to use conditionals inside \Sexper{} in Sweave? An example of what I'm trying to do is
\Sexpr{if(coef(model1)[3]<0){-1*round(coef(model1)[3],3)}else{round(coef(model1)[3],3)}}
More elaborately, I want something like
\Sexpr{if(x<0){paste(-1*x, "lower", sep="")}else{paste(x, "higher", sep=""}}
When I try the first bit of code I get the following error:
Error in parse(text = cmd) : <text>:2:0: unexpected end of input
1:if(coef(model1)[3]<0){-1*round(coef(model1)[3],3)
Any ideas?
Thanks for your help,
-Mark

Curly brackets are not allowed in Sexpr expressions. Instead do the computation in a hidden code chunk and use the result in an Sexpr.
See the Sweave manual: https://stat.ethz.ch/R-manual/R-devel/library/utils/doc/Sweave.pdf

Related

Rendering VueJS prop within HTML array

I have an HTML array with data like so:
data-groups='["category_all", "data goes here"]'
I have a prop called "title" which contains the string I need to render in the "data goes here" area. I've tried using v-bind, but then I lose the array which I need to have in order for the original sort feature to work.
I google'd a few different ways to either escape or render quotes, and most refer to v-bind which again, won't work in this instance.
Any help would be appreciated :)
I was using Shuffle.js and for anyone else seeking an answer, it was in the documentation:
https://vestride.github.io/Shuffle/docs/getting-started
Alternatively, you can set the delimiter option to a comma (delimiter: ',') and the data-groups attribute will be split on that character.
Then changing the above line of code to:
:data-groups="item.category.title + ',all'"
works just fine :)

How I can use curly braces inside a div element in Angular?

<p>Pattern Format (All Parameters are Optional):</p>
<p>{Parameter: 1, Parameter 2}</p>
Above is my code. the second line throws an error because I'm using curly braces in Angular. The error goes away if I use '(' braces.
But I want the curly braces printed.
What can I do so that I get the following result in the web UI? -
Pattern Format (All Parameters are Optional):
{Parameter: 1, Parameter 2}
P.S: I want to print the curly braces. I'm not trying string interpolation.
Values inside tags must be interpolated, which means surrounded by double curly braces {{ YOUR_VALUE }}.
You can take a look at official documentation to see if it can help
your case, since it's not very clear what are you trying to do.
Guide to interpolation:
https://angular.io/guide/interpolation
If you are trying to write it down you can try with:
{{"{Parameter: 1, Parameter 2}"}}
Please check this solution, I hope it will solve your problem.
<span>{{'{'}} {{Parameter:1, Parameter 2}} {{'}'}}.</span>
Its output will be as:
{ParameterValue1, ParameterValue2}
If you want to display single bracess { only instead of variable, you can use ng-non-bindable.
The ngNonBindable directive tells AngularJS not to compile or bind the contents of the current DOM element, including directives on the element itself that have a lower priority than ngNonBindable. Example
<div>Normal: {{1 + 2}}</div>
<div ng-non-bindable>Ignored: {{1 + 2}}</div>
Output of above will be:
Normal: 3
Ignored: {{1 + 2}}
When use ngNonBindable it will ignore parentheses in DOM element.
See Documentation ngNonBindable
Just add the #ngNonBindable directive to the element.

Is there something like a Safe Navigation Operator that can be used on Arrays?

I have used Safe Navigation Operator for Objects to load on Asynchronous calls and it is pretty amazing. I thought I could reproduce the same for Arrays but it displays a template parse error in my Angular code. I know *ngIf is an alternative solution, but is there a more simpler(by code) way just like the Safe Navigation Operator?
<div class="mock">
<h5>{{data?.title}}</h5> //This works
<h6>{{data?.body}}</h6> //This works
<h6>{{simpleData?[0]}}</h6> // This is what I tried to implement
</div>
Is there something like a Safe Navigation Operator that can be used on Arrays?
Yes, what you are looking for is known as the Optional Chaining operator (JavaScript / TypeScript).
The syntax shown in the MDN JavaScript documentation is:
obj.val?.prop
obj.val?.[expr]
obj.arr?.[index]
obj.func?.(args)
So, to achieve what you want, you need to change your example from:
<h6>{{simpleData?[0]}}</h6>
To:
<h6>{{simpleData?.[0]}}</h6>
^
Also see How to use optional chaining with array in Typescript?.
is there a more simpler(by code) way just like the Safe Navigation Operator?
There is ternary operator.
condition ? expr1 : expr2
<h6>{{simpleData?simpleData[0]:''}}</h6>
Of cause it's a matter of taste, but in such cases I tend to use a shorter approach:
<h6>{{(simpleData || [])[0]}}</h6>
The other answers amount to the same thing, but I find foo && foo[0] to be the most readable. The right side of the logical-and operator won't be evaluated if the left side is falsy, so you safely get undefined (or I guess null, if you don't believe Douglas Crockford.) with minimal extra characters.
For that matter, you asked for a "simpler" solution, but actually *ngIf is probably correct for the use case you gave. If you use any of the answers here, you'll wind up with an empty h6 tag that you didn't need. If you make the tag itself conditional, you can just put foo[0] in the handlebars and be confident that it won't be evaluated when foo is still undefined, plus you never pollute the page with an empty tag.

Selenium: test if element contains some text

With Selenium IDE, how can I test if an element's inner text contains a specific string? For example:
<p id="fred">abcde</p>
'id=fred' contains "bcd" = true)
The Selenium-IDE documentation is helpful in this situation.
The command you are looking for is assertText, the locator would be id=fred and the text for example *bcd*.
It can be done with a simple wildcard:
verifyText
id="fred"
*bcd*
See selenium IDE Doc
You can also use:
assertElementPresent
css=p#fred:contains('bcd')
A solution with XPath:
Command: verify element present
Target: xpath=//div[#id='fred' and contains(.,'bcd')]
Are you able to use jQuery if so try something like
$("p#fred:contains('bcd')").css("text-decoration", "underline");
It seems regular expressions might work:
"The simplest character set is a character. The regular expression "the" contains three
character sets: "t," "h" and "e". It will match any line with the string "the" inside it.
This would also match the word "other". "
(From site: http://www.grymoire.com/Unix/Regular.html)
If you are using visual studio there is functionality for evaluating strings with regular expressions of ALL kinds (not just contains):
using System.Text.RegularExpressions;
Regex.IsMatch("YourInnerText", #"^[a-zA-Z]+$");
The expression I posted will check if the string contains ONLY letters.
Your regular expression would then according to my link be "bcd" or some string you construct at runtime. Or:
Regex.IsMatch("YourInnerText", #"bcd");
(Something like that anyway)
Hope it helped.
You can use the command assertTextPresent or verifyText

How to write a regular expression for URLs

I've been using the Regular Expression Explorer but I still can't come up with the right pattern.
Here's my URL:
http://pie.crust.com:18000/TEST/TEST.html
Here's my RegExp:
/[^http:\/\/][\w-\W]+[\/]/
And the output is:
ie.crust.com:18000/TEST/
All I want is the domain (basically everything inbetween // and /):
pie.crust.com:18000
What am I missing? I just can't figure it out. Any ideas?
Thank you in advance.
Try this one: http:\/\/([^\/]+)
The part [^http:\/\/] is the same as [^htp:\/] and just enumerates all the characters which shouldn't be in the start part of the resulting string. So for http://pie.crust.com:18000/TEST/TEST.html http://p matches this enumeration. I suggest you the following expression:
/http:\/\/([^\/]+)\/.*/
You can use String.replace() the following way:
var myUrl:String = "http://pie.crust.com:18000/TEST/TEST.html";
var refinedUrl:String = myUrl.replace(/http:\/\/([^\/]+)\/.*/, "$1");
Try this:
#http://+(.*?)/#
(Your regexp doesn't have to start and end with / - it's easier to use something else that isn't in your search string.
(?<=http:\/\/)[a-zA-Z.:0-9-]+
The p of "pie" is being matched as part of the http rule, and so is not included. Using a positive look-behind fixed this.
http://regexr.com?2uhjf
try this...
//http:\/\/([^\/]+)\/.***/