Shiny and Javascript - Uncaught ReferenceError: TRUE is not defined - javascript

I have a shinyapp which has a basic login screen.
When the app loads, the "Logged" variable is FALSE. When the user successfully logs in within the session, the R login code is as follows:
X$Logged <- TRUE
At this point, in the javascript console I get warnings saying "Uncaught ReferenceError: TRUE is not defined".
I click on the link to go to the code, and the javascript code is:
(function() {
with (this) {return (X$Logged=TRUE);}
})
I am a beginner. I am assuming that firstly, this is the javascript render of my R code above, and secondly that it doesn't like it because javascript expects a lower case boolean "true".
How can I get around this? Equally, R doesn't like lower case boolean.

How can I get around this? Equally, R doesn't like lower case boolean.
Define one in the other, e.g. as you're going to JavaScript, before your code (in the JavaScript)
var TRUE = true, FALSE = false;
Then
(function() {
with (this) {return (X$Logged=TRUE);} // should now work
})
Alternatively, you could use bool-like things, i.e. 1 and 0 instead.
Finally, how are you doing the R to JS transformation? This well may be a bug in the framework you've used

Paul's and Mritunjay's answer helped me to get this fixed.
The misunderstanding was in a conditionalPanel() within my ui.R file. I had:
conditionalPanel(
condition="X$Logged=TRUE"
...
)
I spotted the single "=" instead of "==" and realised the condition within the inverted commas is JS, and not R. I therefore changed my ui.R to this:
conditionalPanel(
condition="X$Logged=true"
...
)
However, I kept upper case "TRUE" in all of my server.R logic, like this:
X$Logged <- TRUE
...and it now works.

Related

Inline code with and is not working but using the regular if works

I have the following code amd it gives me compilation error:
for(var i in test){
(this.watcherFailureCount> 10) && break
}
However the following works:
if(this.watcherFailureCount> 10)
{
break
}
Am I missing anything here? why the first one is not working for me?
The && tries to evaluate your expression and cast its return value to boolean. The break you use is a keyword that controls the loop and should not be used in expressions.
Some languages allow that but it just seems that js doesn’t. And to be fair it is ok not to because is missleading. Imagine conditions like: a && b && break && c && d = a.
There is no real benefit in the first option unless you codegolf or something, and if you codegolf you chosed the wrong language :).
Dont fully understand what youre trying to achieve here however impretty sure the first code snippet is incorrect syntax.
If you want that as an inline if statement try:
if(this.watcherFailureCount>10)break;
However ensure if you are using break that it is inside of some form of code loop like a while or a for loop. And using && with break is invalid as break cannot be a true or false statement so it cant be used like that.

Haskell Webviewhs textfields and returns to Haskell

I've discovered webviewhs and tried it recently. It just works and I think it can do what I need it to do. Samples are abundant, but I would need some pointers of Haskell experts.
{-# LANGUAGE
OverloadedStrings
, QuasiQuotes
#-}
import Data.Text
import Language.Javascript.JMacro
import qualified Graphics.UI.Webviewhs as WHS
main :: IO ()
main =
WHS.withWindowLoop
WHS.WindowParams
{ WHS.windowParamsTitle = "webviewhs - How do I run some JavaScript inside the window?"
-- This could be a localhost URL to your single-page application (SPA).
, WHS.windowParamsUri = ""
, WHS.windowParamsWidth = 800
, WHS.windowParamsHeight = 600
, WHS.windowParamsResizable = True
, WHS.windowParamsDebuggable = True
}
-- This is the callback JavaScript can execute.
(\ _window stringFromJavaScript -> print stringFromJavaScript) $
-- This function runs every window loop.
-- Return True to continue the loop or False to exit the loop.
\ window -> do
let string = "Hello from Haskell." :: Text
-- runJavaScript returns either True on success or False on failure.
WHS.runJavaScript
window
[jmacro|
alert ("Yahoo! Alert windows, but little too persistent!");
window.external.invoke("Hello from JavaScript.");
|]
If I use the code above, the alert-dialog (test) keeps popping up. I would love to have it gone after ok. I also tried several times to get information (document.getElementById) from a sites' input field. Cannot get that to work. For me webviewhs is the missing link in Haskell and I would really like to get it to work.
The problem is in WHS.withWindowLoop. Referring to the documentation:
Creates a window for you. Accepts a function that is called with each iteration of the window loop. If the accepted function returns False, the loop is exited and the window is destroyed. If the accepted function returns True, the loop is continued provided it can.
(my emphasis)
Note that the last parameter of withWindowLoop is invoked repeatedly until it returns False. However, you are always returning True, meaning that the dialog box is repeatedly opened without ever finishing the loop. I can see two ways out of this problem:
Use WHC.createWindow instead; this function simply creates a window and returns it, without looping. This is the approach used in the relevant example.
Keep on using withWindowLoop, but return False instead of True. This will stop the loop immediately after one iteration.
If you really want to use withWindowLoop, then you could declare an IORef which is True at first, then changed to False after one iteration. That approach isn't very idiomatic though. (Don't use this approach, it doesn't work, it actually launches the dialog box twice, and it's much more complicated than needed anyway)

javascript expression expected

I am still new a Javascript tried earching and tried the development tool in Chrome, too see if I could find the problem.
Working in Intellij IDEA 13, Java, javascript and xhtml.
My problem is that I have a piece of javascript, then in IDEA when moused over, says that
Expression Expected
the javascript code looks the following
<script type="text/javascript>
function nameOfFunction(){
if(#{trendAnalysisLocationReportController.model.showTargetLine}){
this.cfg.series[this.cfg.data.length-1].pointLabels = {
show: false
};
}
}
<\script>
the method in the if sentence is a java method with a boolean return value.
the error is shown when hovering
'#{'
if Had a look at the following questions, before :
Expected Expression
boolean in an if statement
But didnt get me a solution.
what Iam I doing wrong ?
It looks as though the problem is the part that you've got within the #{...} block. Without knowing the context, it's hard to be sure, but is this something in a view/JSP page that's supposed to be replaced with a property at runtime? The if block will expect the part inside the brackets to be a boolean value, so if that's rendered to either 'true' or 'false' it would execute at runtime but would likely show the error you're seeing in your IDE as it's not actually a valid piece of JavaScript. If, on the other hand, you're expecting to be able to call your Java method/property from your JavaScript code, you're going to need to do something that requests that value from the server-side code - AJAX or similar.
Also worth noting that we can't see what this.cfg is supposed to represent. If that's your entire script block, then there's nothing that defines the cfg object within the current scope.
One last thing, you should change the <\script> end element to as it won't be understood properly by some browsers.

Evaluate string as a conditional statement

I am working on a c++ web-based IDE for beginners where one of it's core function is to let the user know the evaluation result (true or false) of the conditional statement they have on my contenteditable div.
Say i am already able to fetch the "fragment" (what i call it based on my architecture) that i wanted to evaluate and i already know the values of the variables...
Can you please suggest a way on how would i evaluate the text on the fragment as a conditional statement that will return either true or false...
Example
when var b = 5;
$('.frag').eq(1).text() //"if( (b>1) || (b==10) )"
after evaluation(what im asking for help) should return true
$('.frag').eq(2).text() //"(b>1)"
should return true
**$('.frag').eq(3).text() //"(b==10)"
should return false
UPDATE
The problem i am seeing with eval is that if i have a var1 in the contenteditable div and i have a var1 in my code. I should put a header on the variables from the contenteditable then right? like sc_var1 to prevent messing up with var1 from my source code?
You can use Eval like this:
var expression = $('.frag').eq(1).text();
var result = eval(expression);
However I generally do not recommend using eval because there is always a much better workaround.
if I get the bigger picture of your needs I would be able to provide a better solution.

How to set variables inside a mocha test

I am having trouble understanding how to set variables for use in my tests. For example I have a function called spark.isTriple() that takes a number and looks at an array called dice and returns true if the value occurs three or more times or false if it doesn't.
#In game.coffee
window.spark =
isTriple: (n)->
triples = _.filter dice, (i)->
n is i
if triples.length >= 3
true
else
false
And I have a test that looks like this.
# In game_spec.coffee
describe 'spark.isTriple', ->
it "Should return true if the given value is found 3 or more times in the dice roll", ->
dice = [1,2,2,2,4,5]
spark.isTriple(2).should.be.true
The problem is that 'dice' is also being set in an earlier test that is actually testing a rollDice() function. In that test dice is being set to a random array of values which is what it should be doing and testing. But for this particular test I want to be able to set 'dice' to a specific array in order to test my isTriple() function. I've tried placing the "dice = [1,2,2,2,3,4]" in a before() call and a beforeEach() call but it doesn't seem to make any difference.
If anyone can give me some help or pointers on this it would be much appreciated.
So mocha + coffeescript sometimes requires an explicit declare in the right scope if you want a variable that a whole suite of tests can use and can be manipulated during before/beforeEach/after/afterEach.
describe 'spark.isTriple', ->
dice = null #force coffeescript var declaration
it "should blah blah", ->
dice = [1,2,2,2,4,5]
I'm pretty sure something along those lines will fix your problem, but if you post a full file that demonstrates the issue I can be sure.
I'm gonna disagree with Peter here. I think depending on a variable across tests is a bad idea -- it sets you up for intermittent and weird failures in tests. Anything that's going to change as a result of the test should be contained only in that test. To that end, I'd write your re-write your code like so (from the coffee console):
window.spark = hasTriple: (dice, n) ->
ld.filter(dice, ((i) -> n is i)).length >= 3
{ hasTriple: [Function] }
coffee> window.spark.hasTriple([1,2,1,2,5,6], 2)
false
coffee> window.spark.hasTriple([1,2,2,2,5,6], 2)
true
I renamed _ to ld (aka Lodash) because _ has a special meaning in the console so I think it's a good practice to avoid using it.

Categories

Resources