I am trying to print out my code in sweave, along with the output. I have found that when I try to print a big chunk of code, it removes the blank lines that separate it out. I want to keep the spaces because I think it makes it easier to read. Would love some suggestions!
Sweave Document chunk
<<echo=TRUE, results=tex, strip.white=FALSE>>=
y = 2 + 2
purple = 5+8
cats = dogs
#
Output:
y=2+2
purple = 5+8
cats = dogs
There are a few ways to achieve what you want. One would be to add
\usepackage{etoolbox}
\usepackage{setspace}
\AtBeginEnvironment{alltt}{\doublespacing}
before your \begin{document}.
For future reference, I believe that #MartinSchmelzer is probably right. In RStudio (my current preview version is 1.3.926), navigate to Tools > Options... > Sweave, then select Weave Rnw files using: knitr.
Here's a minimal reproducible example that can then be compiled as PDF:
\documentclass{article}
\begin{document}
<<echo=TRUE, strip.white=FALSE>>=
y = 2 + 2
purple = 5+8
cats = dogs
#
\end{document}
The final PDF looks like this (note that dogs is not defined):
Related
I know of the m function Text.Proper which capitalizes all words in a sentence. However, I want to know how I can capitalize only the first word of a sentence?
Something along the lines of the following. You didn't specify any details
= Table.AddColumn(Source, "Converted", each Text.Upper(Text.Middle([Column1],0,1))&Text.Middle([Column1],1,Text.Length([Column1])))
Try this, Excel style ;-)
let
Input = "text to capitalize",
Output = Text.Upper(Text.Start(Input,1)) & Text.End(Input,Text.Length(Input)-1)
in
Output
There are a couple of decent answers already, but here's another option that demonstrates a couple more functions:
Text.Upper(Text.At([Text],0)) & Text.Range([Text], 1, Text.Length([Text]) - 1)
I have an input file that looks like(without such big spaces between lines):
3 4
ATCGA
GACTTACA
AACTGTA
ATC
...and I need to concatenate all lines except for the first "3 4" line. Is there a simple solution? I've tried manipulating getline() somehow, but that has not worked for me.
Edit: The amount of lines will not be known initially, so it will have to be done recursively.
If your concate 2 lines in 1 line then you can use easily concate "+",
e.g:
String a = "WAQAR MUGHAL";
String b = "check";
System.out.println(a + b);
System.out.println("WAQAR MUGHAL" + "CHECK");
Output:
WAQAR MUGHAL check
WAQAR MUGHAL CHECK
I have a big file, results.txt, that I want to take certain lines out of and put them into another file. The data I want to take out is some variable, omega and alpha. However for results.txt, there are two occurrences of omega and alpha for each set of data in results.txt, and I only want the second set of data. I am not sure how to proceed. I know I should use SED but I don't know how since I have only found help regarding replacing lines use sed. Any help would be appreciated. Thank you very much.
#
--- Sorry I was on mobile when I asked the question. Didn't know how to insert code. ---
So my file looks something like
Very big list of useless output
.
.
.
Results 1:
Omega = 121
Distance = 18.7037218936
Alpha = -1.05958217593e-05
Result 5 = 18983
Result 6 = 1231.903
-------------------------
Results 1:
Omega = 121
Distance = 18.7037218936
Alpha = -1.05958217593e-05
Result 5 = 18983
Result 6 = 1231.903
-------------------------
Second useless output for the next data set
.
.
.
The next data set begins after both sets of results. I have 600 data sets. I want to print Omega and Alpha from the second set of results from each dataset to some other file, preferably in two columns, which I don't know if it is possible.
I have tried using sed but the documentation I have found only talks about replacing words I searched for. Thanks for any help!
Made a test file for you:
$ cat > results.txt
foo
alpha 1
omega 1
foo
alpha 2
omega 2
foo
$ tac results.txt|grep -m 1 alpha; tac results.txt |grep -m 1 omega
alpha 2
omega 2
I'm a bloody beginner to Fortran (f90) and some apparently easy problems turn out to cause severe headaches...Thanks for helping me with this one:
My code runs through a loop, processes data and writes them into a file. I'd like to have those data written in columns of the same file until the looping is finished.
OPEN (unit=11,file=filename // '.csv')
WRITE(11,'(i4,A1,f10.6)') NUM4 , tab, NUMfloat10_6
CLOSE(11)
This code works fine for the saving of a single dataset.
"tab" is defined as char(9); filename is specified by the user at the beginning of the script.
When in loop-mode I'd like to add another tab as "A1" and another NUMfloar10_6 ("f10.6"). However, I can't so something like this:
OPEN (unit=11,file=filename // '.csv')
WRITE(11,'(Tk,i4,A1,f10.6)') NUM4 , tab, NUMfloat10_6
CLOSE(11)
with k defined as an integer, increasing with number of loop * 15.
How do you I solve that problem? How do I "add" columns to a file without knowing how many spaces to skip?
In case you use ifort, just add brackets <k>
k = 2
WRITE(*,'(T<k>,A)'), "Hello World!"
k = 6
WRITE(*,'(T<k>,A)'), "Hello World!"
k = 16
WRITE(*,'(T<k>,A)'), "Hello World!"
produces:
Hello World!
Hello World!
Hello World!
Problem
I need to insert text of arbitrary length ( # of lines ) into a template while maintaining an exact number of total lines.
Sample source data file:
You have a hold available for pickup as of 2012-01-13:
Title: Really Long Test Title Regarding Random Gibberish. Volume 1, A-B, United States
and affiliated territories, United Nations, countries of the world
Author: Barrel Roll Morton
Title: How to Compromise Free Speech Using Everyday Tools. Volume XXVI
Author: Lamar Smith
#end-of-record
You have a hold available for pickup as of 2012-01-13:
Title: Selling Out Democracy For Fun and Profit. Volume 1, A-B, United States
Author: Lamar Smith
Copy: 12
#end-of-record
Sample Template ( simplified for brevity ):
<%CUST-NAME%>
<%CUST-ADDR%>
<%CUST-CTY-ZIP%>
<%TITLES GO HERE%>
<%STORE-NAME%>
<%STORE-ADDR%>
<%STORE-CTY-ZIP%>
At this point I use bash's 'mapfile' to load the source file
record by record using the /^#end-of-file/ regex ...so far so good.
Then I pull predictable aspects of each record according to the line
on which they occur, then process the info using a series of sed
search replace statements.
The Hang-Up
So the problem is the unknown number of 'title' records that could occur.
How can I accommodate an unknown number of titles and always have output
of precisely 65 lines?
Given that title records always occur starting on line 8, I can pull the
titles easily with:
sed -n '8,$p' test-match.txt
However, how can I insert this within an allotted space, ex, between <%CUST-CTY-ZIP%> and <%STORE-NAME%> without pushing the store info out of place in the template?
My idea so far:
-first send the customer info through:
Ex.
sed 's/<%CUST-NAME%>/Benedict Arnold/' template.txt
-Append title records
???
-Then the store/location info
sed 's/<%STORE-NAME%>/Smith's House of Greasy Palms/' template.txt
I have code and functions for this stuff if interested but this post is 'windy' as it is.
Just need help with inserting the title records while maintaining position of following text and maintaining total line number of 65.*
UPDATE
I've decided to change tactics. I'm going to create place holders in the template for all available lines between customer and store info --- then:
Test if line is null in source
if yes -- replace placeholder with null leaving the line ending. Line number maintained.
if not null -- again, replace with text, maintaining line number and line endings in template.
Eventually, I plan to invest some time looking closer at Triplee's suggestion regarding Perl. The Perl way really does look simpler and easier to maintain if I'm going to be stuck with this project long term.
This might work for you:
cat <<! >titles.txt
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> Title 1
> Title 2
> Title 3
> Title 4
> Title 5
> Title 6
> !
cat <<! >template.txt
> <%CUST-NAME%>
> <%CUST-ADDR%>
> <%CUST-CTY-ZIP%>
>
> <%TITLES GO HERE%>
>
> <%STORE-NAME%>
> <%STORE-ADDR%>
> <%STORE-CTY-ZIP%>
> !
sed '1,7d;:a;$!{N;ba};:b;G;s/\n[^\n]*//5g;tc;bb;:c;s/\n/\\n/g;s|.*|/<%TITLES GO HERE%>/c\\&|' titles.txt |
sed -f - template.txt
<%CUST-NAME%>
<%CUST-ADDR%>
<%CUST-CTY-ZIP%>
Title 1
Title 2
Title 3
Title 4
Title 5
<%STORE-NAME%>
<%STORE-ADDR%>
<%STORE-CTY-ZIP%>
This pads/squeezes the titles to 5 lines (s/\n[^\n]*//5g) if you want fewer or more change the 5 to the number desired.
This will give you five lines of output regardless of the number of lines in titles.txt:
sed -n '$s/$/\n\n\n\n\n/;8,$p' test-match.txt | head -n 5
Another version:
sed -n '8,$N; ${s/$/\n\n\n\n\n/;s/\(\([^\n]*\n\)\{4\}\).*/\1/p}' test-match.txt
Use one less than the number of lines you want (4 in this example will cause 5 lines of output).
Here's a quick proof of concept using Perl formats. If you are unfamiliar with Perl, I guess you will need some additional help with how to get the values from two different files, but it's quite doable, of course. Here, the data is simply embedded into the script itself.
I set the $titles format to 5 lines instead of the proper value (58 or something?) in order to make this easier to try out in a terminal window, and to demonstrate that the output is indeed truncated when it is longer than the allocated space.
#!/usr/bin/perl
use strict;
use warnings;
use vars (qw($cust_name $cust_addr $cust_cty_zip $titles
$store_name $store_addr $store_cty_zip));
my $fmtline = '#' . '<' x 78;
my $titlefmtline = '^' . '<' x 78;
my $empty = '';
my $fmt = join ("\n$fmtline\n", 'format STDOUT = ',
'$cust_name', '$cust_addr', '$cust_cty_zip', '$empty') .
("\n$titlefmtline\n" . '$titles') x 5 . #58
join ("\n$fmtline\n", '', '$empty',
'$store_name', '$store_addr', '$store_cty_zip');
#print $fmt;
eval "$fmt\n.\n";
titles = <<____HERE;
Title: Really Long Test Title Regarding Random Gibberish. Volume 1, A-B, United States
and affiliated territories, United Nations, countries of the world
Author: Barrel Roll Morton
Title: How to Compromise Free Speech Using Everyday Tools. Volume XXVI
Author: Lamar Smith
____HERE
# Preserve line breaks -- ^<< will fill lines, but preserves line breaks on \r
$titles =~ s/\n/\r\n/g;
while (<DATA>) {
chomp;
($cust_name, $cust_addr, $cust_cty_zip, $store_name, $store_addr, $store_cty_zip)
= split (",");
write STDOUT;
}
__END__
Charlie Bravo,23 Alpa St,Delta ND 12345,Spamazon,98 Spamway,Atlanta GA 98765
The use of $empty to get an empty line is pretty ugly, but I wanted to keep the format as regular as possible. I'm sure it could be avoided, but at the cost of additional code complexity IMHO.
If you are unfamiliar with Perl, the use strict is a complication, but a practical necessity; it requires you to declare your variables either with use vars or my. It is a best practice which helps immensely if you try to make changes to the script.
Here documents with <<HERE work like in shell scripts; it allows you to create a multi-line string easily.
The x operator is for repetition; 'string' x 3 is 'stringstringstring' and ("list") x 3 is ("list" "list" "list"). The dot operator is string concatenation; that is, "foo" . "bar" is "foobar".
Finally, the DATA filehandle allows you to put arbitrary data in the script file itself after the __END__ token which signals the end of the program code. For reading from standard input, use <> instead of <DATA>.