How to insert single backslash in the code of markdown in GitLab? - jupyter-notebook

I wish to add a jupyter notebook file, which contains a Markdown cell, to my GitLab project. I write some code in the Markdown cell and I wish it shows like this:
python train.py \
--batch_size=16 \
--acc=8
I wrote as follows:
`
python train.py
--batch_size=16 \
--acc=8
`
The code shows correctly in Jupyter. After uploading it to GitLab, however, the code shows as follows, where the single backslash becomes double backslashes:
python train.py \\
--batch_size=16 \\
--acc=8
I have tried to write two backslashes, i.e. "\\", however it shows four backslashes (\\\\).
How to write the code to make it show only one backslash?

Related

Zsh: string getting expanded

I am trying to create a Zsh version of the Command Line Window in Vim.
I want to use the moreutils program vipe to pipe history into.
For that purpose, I have something like:
EDITOR='nvim -c "normal G"'
fc -ln | vipe
Here, fc -ln represents the history, and $EDITOR represents the program that I'll be piping into.
The problem is, the above does not work.
In this specific case I get the file G" opened. It seems that the double quotes to surround the command are not being recognized.
Nor could I get it to work with any other combination of single quotes, double quotes or variables.
How can I pass in the string "normal G"?
vipe splits its arguments by space, so it is not possible to use multi-word c options. I resolved this by creating a .vim file and using nvim -S file.vim.

.REnviron with special characters

I am having trouble trying to add environment variables to a REnviron file that have special characters. This is on a Debian machine with the file located at /usr/lib/R/etc/Renviron. If my value has a &, I get a weird error when installing packages (although the package installs fine):
REnviron file: TEST_KEY=HEY&X&THERE
Command: install.packages(futures)
Error:
/usr/lib/R/bin/Rcmd: 468: /usr/lib/R/etc/Renviron: THERE: not found
/usr/lib/R/bin/Rcmd: 468: /usr/lib/R/etc/Renviron: X: not found
Which seems like it's because & is a special character. I can fix this by putting quotes around the value like this: TEST_KEY="HEY&X&THERE". However at that point I can't figure out how to handle when a value itself has a " in it. For example if I wanted the value to be HEY&"&THERE I am not sure how to format that (a backlash in front of the quote didn't work). I tried "HEY&\"&THERE", but that left the \ in the string once loaded into R. Which leads me to my broader question:
How can I ensure that anything that satisfies linux environment variable styling rules works in an REnviron file?
Update: this seems to be a Debian specific issue. You can recreate it using the debian:bullseye-slim docker image, installing R, then editing the Renviron to have a & in it.
Okay I spent an hour looking into this and I think there is the answer.
In both Ubuntu and Debian (and maybe other systems too), the Renviron file gets executed within bash. So what you're typing in the file is exactly bash commands. You can see in lines 39-40 of RCmd the commands:
. "${R_HOME}/etc${R_ARCH}/Renviron"
export `sed 's/^ *#.*//; s/^\([^=]*\)=.*/\1/' "${R_HOME}/etc${R_ARCH}/Renviron"`
The first line runs the Renviron file in the shell, the second then exports the variable names based on lines that have a = in them.
So in our case the way to handle this is to put double quotations around all the values, and any double-quote within the string should get a \ before it. The reason why I didn't realize the solution before I posted the question is that I didn't use cat() when printing my text in R, which removes the leading \. So: "HEY&\"&THERE" would be the right way to do it.
To recap:
The Renviron file is executed on the shell
To handle special characters in strings you use the same logic you would in the OS (so double quotes with \ to escape actual double quotes).

R Shiny: Is there any way to read a single backslash in user input?

I'm making a Shiny app that constructs a bash script to run on a cluster (basically just a txt file). One of the user inputs is a curl command (provided by the database where the files are stored) that they can copy/paste into a textInput field in the app. When run on the cluster, it will download the file for further processing. However, the curl command they provide contains several single backslashes. Example:
curl --cookie jgi_session=/api/sessions/ec32f2d578304a9e62b4646ae2bec6d4 --output download.20210731.211924.zip -d "{\"ids\":[\"5d94dc9fc0d65a87debccfd3\"]}" -H "Content-Type: application/json" https://files.jgi.doe.gov/filedownload/
It works fine if I paste this directly into a script or if I manually add in double backslashes, but I want to keep this as user friendly as possible. Every other post I've seen about this just says to use double backslashes, but I'd rather do this automatically if at all possible. So any ideas? I'm open to alternate solutions, less work for the user the better.
Your code is picking up the curl line as escaped characters. When you write to file, those escaped characters get converted to their actual character (i.e \" gets converted to literal ".
To avoid, replace special escaped characters by the character sequence that literally created the escape sequence. So to build \" in the final written string, you have to produce \\" as escaped character sequence (which is what the output of a print commmand should show).
Once way to achieve this for this particular character sequence is
escapedString = gsub('\"', '\\"', curlString)
Note that, in terms of string interpretation, \" is a single character (converting to "), while \\" is a sequence of two characters: an escaped \ and a literal ", converting to \" when written, which is the desired output.

writeLines() not producing a valid makefile

I'm trying to auto-generate a makefile using R, and have run into a very peculiar problem.
The makefile is produced using the following code:
v <- "histogram.tsv: histogram.r\r\tRscript histogram.r"
fileConn <- file("Makefile")
writeLines(v, fileConn)
close(fileConn)
This produces the following Makefile
histogram.tsv: histogram.r
Rscript histogram.r
This Makefile doesn't build, but when I manually type in the Tab before "Rscript" it does! When I compare the text generated by write.lines to that generated by hand, identical() returns TRUE.
It works fine when I test it on Linux Mint. It's probable that your distribution is not forgiving when it comes to carriage return characters, typically used in windows. You can try removing the carriage return characters or use dos2unix:
In DOS/Windows text files a line break, also known as newline, is a
combination of two characters: a Carriage Return (CR) followed by a
Line Feed (LF). In Unix text files a line break is a single character:
the Line Feed (LF). In Mac text files, prior to Mac OS X, a line break
was single Carriage Return (CR) character. Nowadays Mac OS uses Unix
style (LF) line breaks.
sudo apt-get install dos2unix
dos2unix Makefile
In your case, you'd also need to insert a newline character, hence set v as follows:
v <- "histogram.tsv: histogram.r\n\tRscript histogram.r"
You can try a messier way of writing the makefile, in order to avoid such sort of problems:
target: dependencies; \
command1; \
command2

Executing expressions in Rscript.exe

I'd like to put some expressions that write stuff to a file directly into a call to Rscript.exe (without specifying file in Rscript [options] [-e expression] file [args] and thus without an explicit R script that is run).
Everything seems to work except the fact that the desired file is not created. What am I doing wrong?
# Works:
shell("Rscript -e print(Sys.time())")
# Works:
write(Sys.time(), file='c:/temp/systime.txt')
# No error, but no file created:
shell("Rscript -e write(Sys.time(), file='c:/temp/systime.txt')")
Rscript parses its command line using spaces as separators. If your R string contains embedded spaces, you need to wrap it within quotes to make sure it gets sent as a complete unit to the R parser.
You also don't need to use shell unless you specifically need features of cmd.exe.
system("Rscript.exe -e \"write(Sys.time(), file='c:/temp/systime.txt')\"")

Resources