I have a system consisting of parameters in Access, which are read by an R script, which then starts an Rmarkdown report. In Rmarkdown, a Stata script is built, which reads a data file and creates a graph specified by the Access parameters. To get the Stata graph into the report, I have to store it as a PNG file and link to this file in the Rmarkdown code. Finally, the report is rendered as a Word file (using knitr and Pandoc).
In the present setup, I have several places in the report where a graph can be called for. I can create a single PNG file for each of these places, I know the filenames (controlled by the Access parameters), and I link to each file using the standard command ![](path/to/filename.png. This works properly.
The next development step is that in each place, I need to create an unknown and varying number of PNG files (up to ca. 20 files). I will do this in Stata. The problem is to link to a varying number of files in the Rmd code. I haven't found a way to do this, and need advice on how.
I have some ideas for a solution, but I cannot find the commands or syntax to implement them. I have read the Introduction to Rmarkdown from Rstudio.com, and the Rmarkdown Reference Guide (5 pages) from the same source. I am rather new to both R and Rmarkdown, so I might have overlooked or not understood that there is a solution.
Is it possible to set up a loop or branch (e.g. "if", "for" or "while") in Rmarkdown? Then I could loop over the current number of files, or branch around unused file links.
Can I fetch all files in a certain directory, e.g. by making a link containing wildcards in the filename? Or is there another way of achieving this?
Is there a way of having links to files that do not exist in the present run, without crashing the program? Then I could set up enough links to cover all foreseeable cases.
Or, does anyone have other suggestions?
Sure, you could use a loop like
```{r, results="asis"}
files <- list.files(path = '/path/to/your/pngdirectory/',
pattern = '\\.png', full.names = T)
for(f in files) cat(paste0('![](',f,')\n'))
```
If you want to filter for certain png files you can extend the pattern argument to some more sophisticated regular expression. For exampele, If I only want png files containing '2017-07-11' in their name I would do
list.files(path = '/Users/martin/Dropbox/Screenshots',
pattern = '.*2017-07-11.*\\.png', full.names = T)
where .* matches any character.
Related
I need to increment/add/renumber numbers (BibTeX keys) selected using regex over several hundred TeX files, maintaining the sequence from one file to the next, when sorted in alphanumeric order.
Files:
latex-01.tex
latex-02.tex
latex-03.tex
etc
Each file containing something like,
Text ... [bibkey01a] ...
More text [bibkey02] ...
I know it is easily possible to do it on one file. I have found several other similar pages on stackoverflow and other forums, but all deal only with one file at a time.
I could open each file, increment/add/renumber the numbers using TextPastry or Sublime-Evaluate and manually carry over the proper value to the next file and repeat the procedure for all the files.
That is possible but a daunting task when one has several hundred related files that need to have value renumbered in a continuos related way. Also, it would be quite easy to make a mistake and carry over the wrong number.
How to automatically increment/add/renumber numbers in Sublime 3 over many related files in a continuos way?
It seems Sublime 3 + extensions is not able to do what I need at the moment.
Of course I can do it with a script. I believe Emacs can do it too, using helm-swoop and wgrep, then, using a replace expression that contains elisp code.
I am using the rmarkdown with the rshiny for generating word file reports. I am using the R studio-server for development. On executing the rshiny application, it halts due to some error in the one of the rmarkdown.
The error says...
Quitting from lines 11-486 (/home/KS127/dev/shiny_apps/pashiny/inst/shiny/dataframe_source.Rmd)
Quitting from lines NA-486 (/home/KS127/dev/shiny_apps/pashiny/inst/shiny/dataframe_source.Rmd)
It's providing the line numbers which are not useful to identify the root cause. Adding print statements are also not useful as I am generating the word file report, until and unless the complete .Rmd doesn't get successfully executed, I won't be able to see print statements output.
I tried changing the rmarkdown output setting from chunk output inline to chunk output to console as mentioned here as well but it is of no use.
Is there any way to print the .Rmd file print statements or the output to the console or is there any way to debug the .Rmd file?
In addition to my comment above, Abhinandan, I've recently stumbled across a new package, called testrmd.
Although it is new, it seems to work with a number of different test packages and provides a useful front-end for Rmarkdown documents. (I'm certainly going to use it.)
You might want to check it out. Here's the link: https://github.com/ropenscilabs/testrmd.
I hope this helps you.
See My .Rmd file becomes very lengthy. Is that possible split it and source() it's smaller portions from main .Rmd?
That's what I do. -
Split your code chunks in separate files and add them one by one
I am trying to make a program that could automatically scan the images or texts on a user's desktop and then convert it to a .txt file for text analysis.
So far I have found source codes to convert PDF and HTML into .txt. However I would like to make my program automatically scan the desktop screen at certain time intervals rather than manually inputting the source such as:
$pdf2txt.py samples/simple1.pdf
I don't know where to start so any suggestion will be appreciated.
First of all, the desktop is just a location in the file directory like:
C:\Users\Kirsteen\Desktop
So the next step would be to search through this directory for the types of files you are interested in. You'd be aiming to generate a list of valid file names that need to be converted. This Q/A might help you.
Once the files have been found run those converting scripts you have. To repeat this automatically put all of this in a loop and add a delay so that it runs once an hour/week.
To tidy things up, think about running this process in the background and making sure the program doesn't convert the files more than once if they haven't changed.
I have a folder that contains 300 different files. There are 150 .cft files and 150 .s01 files. Each .cft file has a corresponding .s01 file of the same name. I would like to create a program that can read the files from the folder and place each .cft file and its corresponding .s01 file into an excel document. I would like the .cft file to be on the first worksheet in the document and the .s01 file to be on the second sheet. Then I would like the program to save the file and name it (---------).xls. The (---------) would be the name of the .cft and .s01 file since they are both the same.
So!!! I wrote a program that is able to take the .cft file and the .s01 file, append them and place them in a user defined .xls document. However...I don't want to manually get the names of the 150 files and have to type each one into the program. I also don't want the files to be placed on the same worksheet.
So!!!! I don't want to waste time trying to code something impossible, so before I spend anymore time on this I have a few questions:
Is it possible to read all of the files in a folder and match files of the same name but with different types?
If this is possible, is it then possible to place the corresponding .cft file and .s01 file in the same excel document but on different worksheets?
Then, is it possible to create and save this worksheet as (---------).xls, (-------) being the name of the matching .cft and .s01 file?
So basically...I want to write this code because I am lazy and I don't want to do anything manually ><;;; lol
Example:
The main folder contains 8 files:
dog.cft dog.s01 cat.cft cat.s01 tree.cft tree.s01 bird.cft bird.s01
The program reads all of the files in the folder and recognizes that dog.cft and dog.s01 go together.
The program then creates an excel document and on worksheet 1 places dog.cft and on worksheet 2 places dog.s01.
The program then saves the excel document as dog.xls
Then the program loops through the main folder repeating this process for each of the .cft and .s01 pairs until all 150 pairs have been separated and saved in their own excel document.
I don't know if I'm dreaming a little too big with this but any advice is much appreciated!
personally I would do this with a macro in excel rather than in c++ because doing excel related functions is much easier that way. All of the requirements are possible using VBA within excel.
Yes, it's possible.
For the listing of files in a folder, you can use the Windows API functions FindFirstFile and FindNextFile. When you finish iterating the folder, you'll need to call FindClose.
For creating the Excel spreadsheet and working with the workbook's sheets, you can use COM automation. Here's a link to an article on doing so from C++ (MFC); the article explains where to find one that isn't MFC based.
If you get started and have specific questions about either of the tasks, please post them as separate questions. This should have been two individual questions, in fact - one about iterating the content of a folder and a different one about working with Excel files from C++.
In C++ I'm generating a PDF report with libHaru. I'm looking for someway to append two pages from an existing PDF file to the end of my report. Is there any free way to do that?
Thanks.
Try PoDoFo
http://podofo.sourceforge.net/
You should be able to open both of the PDFs as PDFMemDocuments using PDFMemDocument.Load( filename ).
Then, acquire references to the two pages you want to copy and add to the end of the document using InsertPages, or optionally, remove all but the last two pages of the source document, then call PDFDocument.
Append and pass the called document. Hard to say which would be faster or more stable.
Hope that helps,
Troy
You can use the Ghostscript utility pdf2ps to convert the PDF files to PostScript, append the PostScript files, and then convert them back to a PDF using ps2pdf.