Error while Scilab module is loading - scilab

I downloaded module Metanet 0.6.2 and ran by Scilab
atomsInstall
After that i ran
`atomsLoad('metanet')`
but it shows
atomsLoad: An error occurred while loading 'metanet-0.6.2':
error(msprintf(gettext('%s module required."),'graph'));
^^
Error: Heterogeneous string detected, starting with ' and ending with ".
at line 335 of function atomsLoad ( D:\Program Files\scilab-6.0.1\modules\atoms\macros\atomsLoad.sci line 351 )
Why did it happen so?

It turns out that the metanet module is not supported by Scilab 6.0.1 yet. I had to install version 5.5.2.

Unfortunately, both the question and the accepted answer here on this page are very vague and misleading. Ideally, this kind of post should be blocked / down-voted, but I will try to answer it as much as I can.
Firstly when you want to run a Scilab command you do not put it in quotation marks, unless you want to use execstr command. However, the characters you have used are not quotations but backticks! I'm not sure why you have done that.
Secondly, the error:
Error: Heterogeneous string detected, starting with ' and ending with "
happens when a double quotation is used inside the single quotation or vice versa:
"This is a' string"
'this is a" string'
to solve the issue you should change the above strings to
"This is a'' string"
'this is a'" string'
basically adding one single quotation before any of the ' and " characters to turn them into literal ' and ".
bonus point if you want to pass a string to Tcl use curly brackets
TCL_EvalStr("set myVar {Hello World!}")
or
TCL_EvalStr("set myVar '"Hello World!'"")
but for PowerShell
powershell('$myVar= ''Hello World!''')
or
powershell("$myVar= ''Hello World!''")

Related

How to pass a chr variable into r"(...)"?

I've seen that since 4.0.0, R supports raw strings using the syntax r"(...)". Thus, I could do:
r"(C:\THIS\IS\MY\PATH\TO\FILE.CSV)"
#> [1] "C:\\THIS\\IS\\MY\\PATH\\TO\\FILE.CSV"
While this is great, I can't figure out how to make this work with a variable, or better yet with a function. See this comment which I believe is asking the same question.
This one can't even be evaluated:
construct_path <- function(my_path) {
r"my_path"
}
Error: malformed raw string literal at line 2
}
Error: unexpected '}' in "}"
Nor this attempt:
construct_path_2 <- function(my_path) {
paste0(r, my_path)
}
construct_path_2("(C:\THIS\IS\MY\PATH\TO\FILE.CSV)")
Error: '\T' is an unrecognized escape in character string starting ""(C:\T"
Desired output
# pseudo-code
my_path <- "C:\THIS\IS\MY\PATH\TO\FILE.CSV"
construct_path(path)
#> [1] "C:\\THIS\\IS\\MY\\PATH\\TO\\FILE.CSV"
EDIT
In light of #KU99's comment, I want to add the context to the problem. I'm writing an R script to be run from command-line using WIndows's CMD and Rscript. I want to let the user who executes my R script to provide an argument where they want the script's output to be written to. And since Windows's CMD accepts paths in the format of C:\THIS\IS\MY\PATH\TO, then I want to be consistent with that format as the input to my R script. So ultimately I want to take that path input and convert it to a path format that is easy to work with inside R. I thought that the r"()" thing could be a proper solution.
I think you're getting confused about what the string literal syntax does. It just says "don't try to escape any of the following characters". For external inputs like text input or files, none of this matters.
For example, if you run this code
path <- readline("> enter path: ")
You will get this prompt:
> enter path:
and if you type in your (unescaped) path:
> enter path: C:\Windows\Dir
You get no error, and your variable is stored appropriately:
path
#> [1] "C:\\Windows\\Dir"
This is not in any special format that R uses, it is plain text. The backslashes are printed in this way to avoid ambiguity but they are "really" just single backslashes, as you can see by doing
cat(path)
#> C:\Windows\Dir
The string literal syntax is only useful for shortening what you need to type. There would be no point in trying to get it to do anything else, and we need to remember that it is a feature of the R interpreter - it is not a function nor is there any way to get R to use the string literal syntax dynamically in the way you are attempting. Even if you could, it would be a long way for a shortcut.

How to parse #{TEST TAGS} into only the Tags, eliminating current formatting?

Situation.. I have two tags defined, then I try to output them to the console. What comes out seems to be similar to an array, but I'd like to remove the formatting and just have the actual words outputted.
Here's what I currently have:
[Tags] ready ver10
Log To Console \n#{TEST TAGS}
And the result is
['ready', 'ver10']
So, how would I chuck the [', the ', ' and the '], thus only retaining the words ready and ver10?
Note: I was getting [u'ready', u'ver10'] - but once I got some advice to make sure I was running Python3 RobotFramework - after uninstalling robotframework via pip, and now only having robotframework installed via pip3, the u has vanished. That's great!
There are several ways to do it. For example, you could use a loop, or you could convert the list to a string before calling log to console
Using a loop.
Since the data is a list, it's easy to iterate over the list:
FOR ${tag} IN #{Test Tags}
log to console ${tag}
END
Converting to a string
You can use the evaluate keyword to convert the list to a string of values separated by a newline. Note: you have to use two backslashes in the call to evaluate since both robot and python use the backslash as an escape character. So, the first backslash escapes the second so that python will see \n and convert it to a newline.
${tags}= evaluate "\\n".join($test_tags)
log to console \n${tags}

R customize error message when string contains unrecognized escape

I would like to give a more informative error message when users of my R functions supply a string with an unrecognized escape
my_string <- "sql\sql"
# Error: '\s' is an unrecognized escape in character string starting ""sql\s"
Something like this would be ideal.
my_string <- "sql\sql"
# Error: my_string contains an unrecognized escape. Try sql\\sql with double backslashes instead.
I have tried an if statement that looks for single backslashes
if (stringr::str_detect("sql\sql", "\")) stop("my error message")
but I get the same error.
Almost all of my users are Windows users running R 3.3 and up.
Code execution in R happens in two phases. First, R takes the raw string you enter and parses that into commands that can be run; then, R actually runs those commands. The parsing step makes sure what you've written actually makes sense as code. If it doesn't make any sense, then R can't even turn it into anything it can attempt to run.
The error message you are getting about the unrecognized escape sequence is happening at the parsing stage. That means R isn't really even attempting to execute the command, it just straight up can't understand what you are saying. There is no way to catch in error like this in code because there's no user code that's running at that point.
So if you are counting on your users writing code like my_string <- "something", then they need to write valid code. They can't change how strings are encoded or what the assignment operator looks like or how variables can be named. They also can't type !my_string! <=== %something% because R can't parse that either. R can't parse my_string <- "sql\sql" but it can parse my_string <- "sql\\sql" (slashes much be escaped in string literals). If they are not savy users, you might want to consider providing an alternative interface that can sanitize user input before trying to run it as code. Maybe make a shiny front end or have users pass arguments to your scripts via command line parameters.
If you're capturing your user input correctly, for a string input of\, R will store that in my_string as \\.
readline()
\
[1] "\\"
readline()
sql\sql
[1] "sql\\sql"
That means internally in R:
my_string <- "sql\\sql"
However
cat(my_string)
sql\sql
To check the input, you need to escape each escape, because you're looking for \\
stringr::str_detect(my_string, "\\\\")
Which returns TRUE if the input string is sql\sql. So the full line is:
if (stringr::str_detect("sql\\sql", "\\\\")) stop("my error message")

R equivalent for .NET's Environment.NewLine

Is there an R equivalent for Environment.NewLine in .NET?
I'm looking for a character object that would represent a new line based on the environment, e.g. CR LF ("\r\n") on Windows and LF ("\n") on Unix. I couldn't find any such thing in the R documentation, or the default R options.
There’s no equivalent, but most of the time you won’t need it: as long as you’re writing to a text connection, the operating system will do the correct thing and treat '\n' according to the platform’s specification; for example, the documentation of writeLines says:
Normally writeLines is used with a text-mode connection, and the default separator is converted to the normal separator for that platform (LF on Unix/Linux, CRLF on Windows).
The \n should still work:
> s = "line 1\nline 2"
> cat(s)
line 1
line 2
Here's a separate question which explains that print(s) doesn't quite work when trying to output strings with escape characters, and we should use cat or writeLine instead: Printing newlines with print() in R

flex (python PLY) regex for strings

I'm using the python module PLY to write a parser, and I am implementing as I go. I have a simple rule to detect strings:
r'("|\').*("|\')'
When lexer errors are thrown I have this:
def t_error (t) :
print 'Illegal lexer input line ' + str(t.lineno) + ' ' + t.value[:16]
sys.exit(-1)
When I feed my parser the following input:
parse("preg_match('%^[\*\%]+$%', $keywords)")
I get back this in return:
Illegal lexer input line 1 %^[\*\%]+$%', $k
My questions are:
1) Why am I not parsing this string? It seems like my regex should properly handle this string.
2) How can I fix this?
edit:
I have narrowed the problem down a bit. The following strings throw illegal lexer input errors by themselves:
'%'
'^'
Even if this regex were working it isn't quite doing what you want it to, for example it would accept "this', which isn't really a string. This is also the cause of the "illegal lexer input"...
After having done it's job and found the first string in "preg_match(' the lexer is then upset when each of the next 11 characters %^[\*\%]+$% are illegal (and not in t_ignore), since they don't even start with " or '.
.
Try doing this with two cases for " and ': "Starts with quote, some things which aren't quote, ends with quote." That is:
r'("[^"]*")|(\'[^\']*\')'
Or, if you want to include escaped speech marks:
r'("(\\"|[^"])*")|(\'(\\\'|[^\'])*\')'

Resources