How can I expand a string to r-markdown code? - r-markdown

Using r-markdown and Ryacas, I often want to display a yacas expression as an inline equation. E.g.
```{r}
x <- ysym("a^2 + b^2");
```
$r yac_str(x)$
Since I do this so often, I'd like to find a more succinct way to express the inline equation command, something like
fn(x)
Is there any way to define a macro that will do this in r-markdown?
Thanks

An r-markdown snippet provides a way to do this.
See
https://community.rstudio.com/t/is-there-anything-like-a-macro-in-r-markdown/76406
for details.

Related

Calculating with AutoWikiBrowser

I'm using regular expression in AutoWikiBrowser to replace the input of several values with just one value, such as this:
|value1=4
|value2=5
|value3=6
To this:
|value={{#expr:4+5+6}}
While the correct result does show on the page, it does not look good in the code itself, so I'm trying to find a way to make it the result only (in this case value=15) but so far no luck. Can someone help me with out showing how to make this possible?
P.S. I tried the search function but didn't find a similar question.
MediaWiki parser allows the templates can be subst'ed, ie replaced by their rendering. That's also true for parser functions call.
You can subst a template prefixing the template call by subst:.
|value={{subst:#expr:4+5+6}}
Reference: Substitution on MediaWiki manual
Example: diff (the expression used is in the edit summary, the result in the diff)

Elegant and robust way to comment/uncomment specific lines in vim

I have a very long script R that plots very complicated data. I only use the plots to have a visual idea of what I am doing but I can compute the results without the plots and obviously not plotting anything makes things much faster. Occasionally, however, I still need to visualize what the program does to keep debugging it.
To achieve this plotting 'on or off' switch I am following this strategy.
For each line that has commands relevant to the plotting functions of the script, I have a specific commented tag #toplot at the end of each relevant line. Using the power of regex substitution I then comment / uncomment these lines with the following commands.
The sample code:
a <- c(1:10)
b <- a/sin(a)
png('sin.png') #toplot
plot(b) #toplot
dev.off() #toplot
print(b)
To comment the 'tagged' lines:
:%s/.\+#toplot/###commline###\0/g
I get this:
a <- c(1:10)
b <- a/sin(a)
###commline### png('sin.png') #toplot
###commline### plot(b) #toplot
###commline### dev.off() #toplot
print(b)
To uncomment them:
:%s/###commline###//g
I get this:
a <- c(1:10)
b <- a/sin(a)
png('sin.png') #toplot
plot(b) #toplot
dev.off() #toplot
print(b)
I am no computer scientist so I don't know if there is a better, more elegant way of performing these kind of operations.
EDIT: It is important to mention that for plotting my data I need to go through many rounds of calculations and transformations so the different kinds of data fit in the plotting device. To perform these operations I use the history, I go up and down depending what I need.
Your approach looks fine to me.
If you can come up with a regular expression that captures all plot-related lines, you could do away with the #toplot marker, and let the comment substitution directly work on that instead.
You didn't mention whether you re-type the substitutions or use the history. I would definitely define a buffer-local command (and/or mapping) for that:
autocmd FileType r command! -buffer Comment %s/.\+#toplot/###commline###\0/g
autocmd FileType r command! -buffer Uncomment %s/###commline###//g
(Or put the :commands! into ~/.vim/ftplugin/r_commands.vim.)
If you properly define the 'comments' setting for your filetype (e.g. add b:###commline###) and 'commentstring', you may also be able to use one of the general comment plugins (like The NERD Commenter), which offer nice mappings to toggle a comment on/off.
This is ok, but isn't it easier to wrap each plotting command with a condition?

Vim different textwidth for multiline C comments?

In our C++ code base we keep 99 column lines but 79-some-odd column multiline comments. Is there a good strategy to do this automagically? I assume the modes are already known because of smart comment line-joining and leading * insertion.
Apparently both code and comments use the same textwidth option. As far as I can see, the only trick is to set this option dynamically:
:autocmd CursorMoved,CursorMovedI * :if match(getline(.), '^\s*\*') == 0 | :setlocal textwidth=79 | :else | :setlocal textwidth=99 | :endif
Here the critical part is detecting when we are in a comment. If you only format comments this way:
/*
* my comment
*/
my regex should work... unless you have lines in the code starting with * (which I guess can happen in C, less frequently in C++). If you use comments like this:
// comment line 1
// comment line 2
the regex is even simpler to write. If you want to cover all possible situations, including corner cases, well... I guess the best thing would be to define a separate detection function and call that from the :autocmd instead of match().
I came across this same problem and think that I have found a suitable solution.
What I wanted my comments to word wrap so that when I'm typing I don't have to worry about formating text. This works well with comment text. But I wasn't comfortable with having vim format my code. So I wanted vim to highlight every thing in red after x column.
To do this with only cpp code you would add the following to your ~/.vim/ftdetect/cpp.vim file.
set textwidth=79
match ErrorMsg '\%>99v.\+'
note: You may have to create the file and folders if they don't exist.
If you have problems with this make sure that you have formatoptions set to:
formatoptions=croql
You can see this by running :set formatoptions inside of vim.

How to use the matched text in the replacement text in Vim

I have a block of codes with timestamp in front of each line like this:
12/02/2010 12:20:12 function myFun()
12/02/2010 12:20:13 {....
The first column is a date time value. I would like to comment them out by using Vim, thus:
/*12/02/2010 12:20:12*/ function myFun()
/*12/02/2010 12:20:13*/ {....
I tried to search for date first:
/\d\d\/\d\d\/\d\d\d\d \d\d:\d\d:\d\d
I got all the timestamps marked correctly. However When I tried to replace them by the command:
%s/\d\d\/\d\d\/\d\d\d\d \d\d:\d\d:\d\d/\/*\d\d\/\d\d\/\d\d\d\d \d\d:\d\d:\d\d*\//
I got the following result:
/*dd/dd/dddd dd:dd:dd*/ function myFun()
/*dd/dd/dddd dd:dd:dd*/ {....
I think I need to name the search part and put them back in the replace part. How I can do it?
I suppose I would just do something like:
:%s-^../../.... ..:..:..-/* & */-
I would actually not us a regex to do this. It takes too long to enter the correct formatting. I would instead use a Visual Block. The sequence works out to be something like this.
<C-V>}I/* <ESC>
3f\s
<C-V>I */
I love regex, and don't want to knock the regex solutions, but find when doing things with pre-formatted blocks, that this is easier, and requires less of a diversion from the real task, which isn't figuring out how to write a regex.
%s/\d\d\/\d\d\/\d\d\d\d \d\d:\d\d:\d\d/\/*&*\//
:%s/^\([0-9/]* [0-9:]* \)\(.*\)/\/*\1*\/ \2/

To comment out matches in Vim - independent on comment leader?

I want to comment out lines in some code I have. I have different kinds of codes, and they use different comment leaders. E.g. in latex: '%', in Fortran 90: '!' and in python: '#'. I want to do a substitute command that looks something like this:
:g/<search-string>/s/^/<add-comment-leader-here>/
If this is possible, I could also make a command in Vim that automatically commented out the selected text. Something like this:
vmap <z> :'<,'>s/^/<add-comment-leader-here>/
Any ideas are welcome! :)
If you haven't seen it already, you may be interested in the NERD Commenter Vim plugin.
Check out Enhanced Commentify: I think this does what you want: it determines the comment leader based on the file type.
If you want to do it yourself, the easiest way would be to define a mapping that uses exec to build a command and include a variable that is set in your ~/.vim/after/ftplugin/c.vim and other ftplugin files. Alternatively, just add the same mapping (with a different leader) to each ftplugin file.