paste r commands inside rmarkdown file - regex

I want my function to place a piece of markdown in a readme.rmd file. However I would like to include some rcode that that will be executed when a new version of the readme file is rendered.
This is what I want in the readme.rmd:
[![Last-changedate](https://img.shields.io/badge/last%20change-r gsub("-", "--", Sys.Date())-yellowgreen.svg)](/commits/master)"
Which at knitr time will turn into a nicely formated shield with the date.
However to paste this in the document I have to escape some of the characters:
paste0("`r ", "gsub(\"-\", \"--\", Sys.Date())", "`")
But this results into
[![Last-changedate](https://img.shields.io/badge/last%20change-`r gsub(\"-\", \"--\", Sys.Date())`-yellowgreen.svg)](/commits/master)"
And this cannot be rendered by rmarkdown error: unexpected input: gsub(\ ^....

With the suggestion of Chinsoon12:
" would using single quote works? i.e. use paste0("r ", "gsub('-', '--', Sys.Date())", "") "
My problem is solved!
I now paste with double and single quotes.
paste0("https://img.shields.io/badge/last%20change-",
"`r ", "gsub('-', '--', Sys.Date())", "`",
"-yellowgreen.svg")

Related

render() versus Knit GUI for word_document

I am trying to knit a table created by kable() and produce a Word document. When I knit using the RStudio knit button, it works fine and produces a formatted table. When I use render(), it does not. It produces just a unformatted string of text. Here is a minimal example:
test.Rmd
---
title: "Test"
output:
word_document:
keep_md: true
---
```{r pressure2, echo=FALSE}
knitr::kable(mtcars)
```
The render() command is
rmarkdown::render("test.Rmd", clean=FALSE)
The pandoc command that is run by both the Knit button (RStudio) and the render() command is
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS test.utf8.md --to docx --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.docx --smart --highlight-style tango
I can see the problem in the test.utf8.md file produced by pandoc() when I run render(). The test.utf8.md file is an html table. I cannot see the test.utf8.md file produced by clicking the Knit button since that is not saved with keep_md=true. Only the test.md file is kept.
The RStudio Knit button must be changing the kable() format when output is word_document. If I change the kable() call to
knitr::kable(mtcars, format="markdown")
It works. The following sets the kable() format. I don't know what output format the user will select, so don't want to set format in the function call. Putting this with an if statement to detect if the output type is Word, fixes the problem.
options(knitr.table.format = 'markdown')

Unable to write text inside a file using with open r+ PYTHON

I am using Python 2.7.
I want to create a script to scan the text file for specific keywords like want to test and write or replace string (b3). My script:
#! usr/bin/python
import re
from os.path import abspath,exists
open_file = abspath("zzwrite.txt")
if exists(open_file):
with open(open_file,"r+") as write1:
for line in write1:
matching = re.match(r'.* want to test (..)',line,re.I)
if matching:
print ("Done matching")
write1.write("Success")
print >> write1,"HALELUJAH"
My input text file:
I just want to read 432
I just want to write 213
I just want to test b3 experiment
I just want to sleep for 4 hours
I managed to complete matching as there is a print "done matching" to indicates the codes are able to execute the last 'if' condition but no single string is written or replaced inside the text file. The string "b3" is different in every input text file so I do not prefer using str.replace("b3", "xx") method. Is there anything I missing in the script?

boost::program_options space in config file

I want to set options to my parser program via simple config file parsed by boost::program_options. I want to set prefixmiddle to (single space).
I already tried that options:
prefixmiddle=
prefixmiddle=" " # Equals to `" "`
prefixmiddle = " " # Same
prefixmiddle =
Is there a way to do it?
P.S. Of course, I can always use "" and remove them, but I wonder if there is another solution.

Writing in files

I have been trying to write onto a file while using python but for some reason it keeps writing onto my console and not my created file. Yes I know this question has been asked before and yes i have used the .close() command. Here is my block of code.
myfile= open ('C:/Users/12345/Documents/Grouped_data.txt','r')
with open ('C:/Users/12345/nanostring.txt','w') as output:
for line in myfile:
Templist= line.split()
print line
print Templist[0], Templist[4], Templist[5],Templist[6], Templist[7], Templist[8], Templist[9], Templist[10], Templist[12]
print output
myfile.close()
output.close()
This should be as simple as:
>>> with open('somefile.txt', 'a') as the_file:
... the_file.write('Hello\n')
From The Documentation:
Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single '\n' instead, on all platforms.
In python 2.7,
You can use >> after the print and use as name
So here it is print>>output,line
myfile= open ('C:/Users/12345/Documents/Grouped_data.txt','r')
with open ('C:/Users/12345/nanostring.txt','w') as output:
for line in myfile:
Templist= line.split()
print>>output,line # Note the changes
print>>output,Templist[0], Templist[4], Templist[5],Templist[6], Templist[7], Templist[8], Templist[9], Templist[10], Templist[12] # Note the changes
Note: print directly prints in terminal and print>>as name, prints to file.

regular expression Pattern object getting converted to string if fetched from config.cfg file

python file
import ConfigParser,re
config=ConfigParser.ConfigParser()
with open("temp.cfg",'r') as config_file:
config.readfp(config_file)
x=[]
x.append(re.compile(r'abc'))
x.append((config.get("ssp",'a')).strip('"'))
print x[0]
print x[1]
config file[temp.cfg]
[ssp]
a:re.compile(r'abc')
output
>>>
<_sre.SRE_Pattern object at 0x02110F80>
re.compile(r'abc')
>>>
What "print x[1]" should give is regular expression object but it seems to be returning string.
Looks like I am not doing it in the right way & am unable to figure it out
The output of x[1] is because of the following:
x.append((config.get("ssp",'a')).strip('"'))
Since, config is the cfg file parser object, you are accessing the option a of ssp section:
[ssp]
a:re.compile(r'')
which is obviously, the string: re.compile(r'').
Using eval:
x.append(eval((config.get("ssp",'a')).strip('"')))
You need to change your config file to this:
[ssp]
a:abc
Then you can simply do (without ugly eval())
x.append(re.compile(config.get("ssp", "a")).strip())
You read the regex string first from config file and then translate it to a regex object in your code with re.compile() instead of eval(). That's also better for maintainance, because you save a lot of text in the config file (a:abc instead of a:re.compile(r'abc') for each line).