Catch errors from rmarkdown::render - r

R newbie question: I am generating PDFs using rmarkdown from the console (not using rstudio). I have written a simple r script to render the rmarkdown file from the console, it basically looks like this:
# my_r_script.R
rmarkdown::render('mydoc.Rmd', output_file = opt$out,
params = list(
something = opt$something,
else = opt$else
)
)
In the rmd file bad errors might happen, e.g. some calculations might crash due to the given parameters. Question How can I access those errors in the R script? Or in other words: I want to know if something went wrong in the RMD file how could I achieve that?

You can wrap the rmarkdown::render statement in a try catch function - there is a great example here.
You should be able to store the output in a variable for further debugging.

Related

R Markdown script and R

I am calling one R Markdown script from another R script.Below you can see command
rmarkdown::render((file=paste(path1,"/Dashboard.Rmd",sep="")),params=list(args = myarg))
The script is executed without any problem but is not open automatically.
So can anybody help me how to solve this problem and open this script automatically after running of this command ?
First, your syntax probably isn't doing what you intended. Writing
rmarkdown::render((file=paste(path1,"/Dashboard.Rmd",sep="")),params=list(args = myarg))
will create a new variable named file and use it as the first parameter to rmarkdown::render. A more likely way to get what you want is to write it as
outfile <- rmarkdown::render(paste(path1,"/Dashboard.Rmd",sep=""),
params=list(args = myarg))
This removes the assignment from the first argument, and saves the
result (which is the name of the file that was produced).
Now, on to your question: You need to follow that line with
rstudioapi::viewer(outfile)
to view it in RStudio, or
browseURL(outfile)
elsewhere, because rmarkdown::render doesn't automatically call a previewer.

R Markdown: output with weird characters

I am seeking your help as I encountered a curious R markdown issue when using scripts sourced from external. My rmd. file contains several code chunks that generate tables. For some of the chunks, they work very well with scripts sourced from external, for example:
source(
here::here("file_name_1",
"file_name_2",
"file_name_3",
"file_name_4",
"*********.R"),
echo = FALSE,
print.eval = TRUE
)
However, some chunks with external script sourced using exactly the same code template as above do not give me the output (which is a table).
I ended up re-editing the source script and rmd file. First, in the script, I assigned the table with a name, e.g., Table_1 <- [codes that generate the table] . Second, in the rmd file, I added Table_1 below the code chunk to call the table generation function. For example:
source(
here::here("file_name_1",
"file_name_2",
"file_name_3",
"file_name_4",
"*********.R"),
echo = FALSE,
print.eval = TRUE
)
Table_1
Then I re-run the rmd, it seemed like starting to work and did give me a table that I want, but there were minor issues: some characters / signs in the table were weird. For example, 1-18 became 1–18.
Any ideas / hints for solving this problem from you would be very much appreciated.
It turns out that the "call function" do work. The reason why 1–18 became 1–18 is because the dash "–" came from non-standard keyboard or Copy-Paste, which can not be recognised in regular R script. However, theses special characters can be recognised in R Markdown.

Print message into R markdown console while knitting

I'm wondering if there is a way to print a message containing one of the variables into Rmarkdown console at the end of knitting?
I'm currently preparing an R Markdown template for others to use. I would like to inform the users about the readability index of the document once it finishes compiling. I have already found a way to calculate it in one of the chunks. Now I would like to automatically print it to the person who compiles it in a way that it is not shown in the final document. Any ideas?
Ben Bolkers comment is the perfect answer. I ended up putting:
{r cache = FALSE, echo = F, warning = F, message = F}
message(rdblty)
at the very bottom of the markdown file. rdblty is the variable calculated before that I wanted to print out.
How about ?
cat(file="readability.txt")

Click on a URL from an R string output

Suppose I have an output from the cat function of R which is a URL. For example:
cat("https://en.wikipedia.org/wiki/Statistics")
# Output: https://en.wikipedia.org/wiki/Statistics
Is there any command on the cat function or any other thing so that the output https://en.wikipedia.org/wiki/Statistics becomes a clickable URL in the R console?
As you mentioned in the comments you are using RStudio. It is not specified why it has to be the console in R, but I assume there is a good reason to display the links within RStudio and I assume the viewer pane on the right next to the console also works for you.
If that is the case you could do the following:
library(DT) # for datatable() function
library(shiny) # for tags$a() function
data <- data.frame(link = toString(tags$a(href = paste0("http://google.de"), "google")))
datatable(data, escape = FALSE)
Very close to the console ;)
It would depend on how the output is being viewed. If this output is going to an HTML file, then all you need to do is set this as the location for a hyperlink.
https://en.wikipedia.org/wiki/Statistics
But if, for example, you are just viewing the output in notepad, then I don't believe notepad has the functionality for hyperlinks.
I don't believe the R console directly supports hyperlinks or not. But it looks like from this: http://rmarkdown.rstudio.com/lesson-2.html that you can maybe use R Markdown to do what you want.
This is not currently possible in RStudio, unfortunately. However, it is an open issue which you can upvote if you think it should be prioritised.

Cannot save plots in Octave using print()

I'm simply trying to print the current figure in Octave to a .png file using the print() function. The documentation seems pretty straightforward, but it's not working. I get no error messages, but the files are nowhere to be found. I am doing
filename = sprintf('/home/me/Desktop/file_%d.png',n);
print(filename, '-dpng');
where n is just some loop counter variable. Again, I get no error, but when I check /home/me/Desktop/, there are no files. Am I doing this wrong?
Octave probably doesn't know where your current figure is or the filename is bad. Try using this version of print
print (h, filename, options)
with h given for the plot figure handle. Also try printing the filename to console and make sure it is correct

Resources