Why does Jupyter notebook call the object after / in a line? - jupyter-notebook

When I runned this code cell:
x = 1
y = 10
/print x y
I got the output:
1 10
I searched why this works but couldn't find any answers. I would be glad if someone explains why.

IPython forward slash documentation
has the same question answered.
In short:
You can force auto-parentheses by using '/' as the first character of a line. For example:
In [1]: /globals # becomes 'globals()'

Related

Automate Response at Prompt in R interactive

Please see below my reference to a previous question asked along these lines.
I am running the library taxize in R. Taxize includes a function for getting a stable number associated with a scientific name, get_tsn().
I can run this in interactive mode or non-interactive mode so that I am either
prompted or not, respectively, to choose among multiple hits.
Interactive:
> tax.num <- get_tsn("Acer rubrum", ask=TRUE)
Retrieving data for taxon 'Acer rubrum'
tsn target commonNames nameUsage
1 28728 Acer rubrum red maple accepted
2 28730 Acer rubrum ssp. drummondii NA not accepted
3 526853 Acer rubrum var. drummondii Drummond's maple accepted
...
More than one TSN found for taxon 'Acer rubrum'!
Enter rownumber of taxon (other inputs will return 'NA'):
Non-interactive:
> tax.num <- get_tsn("Acer rubrum", ask=TRUE)
Retrieving data for taxon 'Acer rubrum'
Warning message:
> 1 result; no direct match found
I need to run this library in interactive mode so that I do not get an empty result when there is more than one match. However, babysitting this script is totally unrealistic for the size of my data, which are in the millions of scientific names. Thus, I want to automate a response to the prompt so that the answer is always 1. This will be the right answer for probably 99% of cases and will ultimately still lead to the right answer downstream in 100% of cases for reasons that are probably beyond the scope of this question.
Thus, how can I automate the response to always be 1?
I looked at this question and tried modifying my code accordingly.
options(httr_oauth_cache=T)
tax.num <- get_tsn("Acer rubrum",ask=T)
However, this gave the same result shown for interactive mode above.
Your help is appreciated.
UPDATE: Ignore below. Obviously Nathan Werth posted the best answer in a comment above.
tax.num <- get_tsn_(searchterm = "Acer rubrum", rows = 1)
works wonderfully!
...
I decided to modify the source code to handle this. I suspect that there is a more desirable solution, but this one meets my needs.
Thus, in the file get_tsn.R from the source, I replaced the following block of code
# prompt
message("\n\n")
print(tsn_df)
message("\nMore than one TSN found for taxon '", x, "'!\n
Enter rownumber of taxon (other inputs will return 'NA'):\n")
# prompt
take <- scan(n = 1, quiet = TRUE, what = 'raw')
with
take <- 1
I could have deleted other echoing to screen bits, that are unnecessary and now not true.
The revised function, which I tested using trace("get_tsn",edit=TRUE), returns as follows:
> print(tax.num)
[1] "28728"
attr(,"match")
[1] "found"
attr(,"multiple_matches")
[1] TRUE
attr(,"pattern_match")
[1] FALSE
attr(,"uri")
[1] "http://www.itis.gov/servlet/SingleRpt/SingleRpt?
search_topic=TSN&search_value=28728"
attr(,"class")
[1] "tsn"
I will recompile and install it on Linux now with the edit for use with this particular project.
I still welcome other, better answers.

R script with user input from command line [duplicate]

This question already has answers here:
How to include interactive input in script to be run from the command line
(3 answers)
Closed 5 years ago.
I cannot find a solution to this particular problem, even though more or less similar questions have been questioned before in:
Run R script from command line
http://www.cureffi.org/2014/01/15/running-r-batch-mode-linux/
Running a script from bash is easy enough, however once one needs user interaction I couldn't find a solution. Please consider the example:
userInput<-function(question) {
n = 0
while(n < 1 ){
n <- readline(question)
n <- ifelse(grepl("\\D",n),-1,as.integer(n))
if(is.na(n)){break} # breaks when hit enter
}
return(n)
}
investedLow<- userInput("Invested value in low risk since last time: ")
Now if I save this script as test.R and run it for R --no-save < teste.R the entire script is run and the time for user input does not happen.
The script works fine in Rstudio, for example.
How to wait for user input in a script to be run in the command line?
Here is a total hack, repurposing a very specific purpose-built package for your more-general question:
library(getPass)
userInput<-function(question) {
n = 0
while(n < 1 ){
n <- getPass::getPass(msg = question)
n <- ifelse(grepl("\\D",n),-1,as.integer(n))
if(is.na(n)){break} # breaks when hit enter
}
return(n)
}
investedLow <- userInput("Invested value in low risk since last time: ")
print(investedLow)
Maybe the worst part about this is that getPass hides the user input. There must be a way to modify the source code to fix that.
Update: The getPass author pointed out that the solution could be as simple as using readLines slightly differently:
cat(question)
readLines(file("stdin"), n=1)

differences between .sage and .spyx in numerical evaluation

the question seems very basic, i'm sorry but i could not find an answer in the documentation.
the content of both files test.sage and test.spyx is identical; it's just
a = 1/sqrt(2)
print a
if i run test.sage with
$ sage test.sage
i get
1/2*sqrt(2)
but the outcome is different from if i run the file test.spyx with
$ sage test.spyx
where i get
Compiling test.spyx...
0.707106781187
how can i prevent sage from numerically evaluating 1/\sqrt(2) in .spyx mode?
(i asked the question on ask.sagemath.org as well but got no answer yet...)

Syntax Error in Octave plotting

I got the following syntax error while I want to plot values:
syntax error
>>> plot(freq1, abs(fft1/max(fft1)),xlabel('f(Hz)'), ylabel('Amplitude I(f)');
^
My definitions are as follows:
a=x+y+z; % a is a sinus mixture of different curves/functions
n1 = fa/0.05; % N is 50 ms
fft1=fft(a,n1);
freq1 = [0:deltaF1:fa-fft1];
plot(freq1, abs(fft1/max(fft1)),xlabel('f(Hz)'), ylabel('Amplitude I(f)');
EDIT: It would be nice stop voting me down, I know that question is not too interesting (please see below in the comment to the answer), thank you!
You have more opening brackets ( than closing ones ), that's a syntax error.
It should be:
plot(freq1, abs(fft1/max(fft1)),xlabel('f(Hz)'), ylabel('Amplitude I(f)'));

how to prevent the output of R to scroll away in bash? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Equivalent to unix “less” command within R console
I am using R under unix in bash.
Sometimes the output of a command has more lines than the bash.
How do I prevent the output from scrolling away? I.e. is there some equivalent of less and less -S in R?
A way to do this in R is also to redirect to a file:
sink("a_file.txt")
...your_commands...
sink()
I think the page() function is like having | less in an R session. It allows two representations of the object; i) a version you'd get from dput(), and ii) a version you'd get if you print()-ed the object.
dat <- data.frame(matrix(rnorm(2000), ncol = 5))
page(dat, method = "print")
It might be possible to wrap your expression in capture.output, and then page the result to the terminal.
pager <- function(cmd,nlines=10){
output = capture.output(cmd)
pages = seq(1,length(output),by=nlines)
for(p in pages){
f = p
l = min(p+nlines-1,length(output))
cat(paste(output[f:l],"\n"))
readline("*more*")
}
return(invisible(0))
}
Usage: pager(ls()), then hit Return (not space or anything else) at each 'more' prompt.
currently it doesn't return the value. Oh and it fails if there's no output. But you can fix these :)
Or use emacs with ESS and let it all scroll back...
Wouldnt
any_command | more
work fine?
“the bash” has no lines, your terminal has.
You can set the number of lines of your terminal in the settings of that application.
Your question is unclear. If you're talking about using R interactively and accidentally running a command which spits out a huge number of lines, run something like this in your R session: options(max.print=4000)

Resources