Link chrome to Rstudio for pagedown (Rmarkdown) - r

I want to convert my html document, created on Rmarkdown to pdf.
One of the way is to use the pagedown package.
However, when I add:
knit: pagedown::chrome_print
in the yaml header, I get the error:
Error in find_chrome() : Cannot find Google Chrome or Edge automatically from the Windows Registry Hive.
Please pass the full path of chrome.exe or msedge.exe to the 'browser' argument
or to the environment variable 'PAGEDOWN_CHROME'.
However, i have no idea on how to link the Chrome browser to rstudio, or where can I access to 'PAGEDOWN_CHROME'.
I tried:
knit: pagedown::chrome_print(browser="C:/Users/johndoe/AppData/Roaming/Microsoft/Windows/Start Menu/Programs"
which is the path to Chrome but the knitting systematically hangs without giving an error.
I am sure this is an easy procedure but but I'm a beginner at this stuff.
I've done a lot of research on SO and the internet, but obviously everyone knows how to do it !
Thanks :-)

Related

Quarto Render not Rendering

I am trying out Quarto, I am using R version 4.2.1. When I go to RStudio, File -> New File -> Quarto Document and create a new document, when I try to render via the RStudio Render button, I get the error 'C:/Program' is not recognized as an internal or external command, operable progam or batch file
I have tried installing R in a directory which has no white space in the installation path, but this did not resolve the issue.
UPDATE: on doing quarto check as suggested in comment gave me
Not sure what that means, but I was able to solve the problem by ensuring my qmd quarto file did not have white space in its name (replaced white space with underscores. The error then went away.
On changing my qmd file name, from HVL campaign.qmd to HVL_campaign.qmd (removing the white space), the rendering works. The error message was misleading though, that may need to get looked at...?
This error pops up if there is a space anywhere in the filepath unfortunately. I am running into this error when I try to make a new project in a directory that has a filepath with a space (thanks onderive...)

Rstudio Global Changes: show chunk output in console via CLI?

How can I force RStudio (v1.1.383) to evaluate R chunks always in console (instead of inline) when working with Rmarkdown documents, using a script?
I know I can set up Output chunks in Console by clicking on it:
According to this RStudio support post I could also un-check 'Show output inline for all R Markdown document' under 'Tools -> Global Options...':
But, is there a way to do it from a command line?
The reason I ask is that, I often work on my university machines and they all restore to defaults after each reset. Each time when in class, we have to manually go thru menus.
Knowing how to do it via a console command would as useful as starting each of my classes with
rm(list=ls())
There's not currently an elegant way to do this. This preference is stored inside an internal RStudio state file, in %localappdata%\RStudio-Desktop\monitored\user-settings. If you're sufficiently motivated you can write a script which sets the rmd_chunk_output_inline preference, but it's going to be unpleasant.
One thing you can do is set the chunk output type in the YAML header, like this:
---
editor_options:
chunk_output_type: console
---
You could also use an R Markdown document template with this set up for you (maybe your script could write this out).
Finally, there's an open issue for this on RStudio's github page which you might comment on and/or vote for:
https://github.com/rstudio/rstudio/issues/1607

Why does RMarkdown `render` behavior depend on whether it's called from RStudio Server or from a PHP shell?

I have an RMarkdown document that includes 'special characters', such as ë. If I render the document using RStudio Server's "knit document" button, it renders fine. When I render it by using the RStudio Server button to source another R script that calls RMarkdown's render function, it also renders fine.
However, from some reason that's beyond me (but hopefully not for long), I get different results when that same R script is called by index.php using:
$results = shell_exec("R --file='/home/username/public_html/some/subdirectories/process.R' --no-save 2>&1");
When I do this, in the resulting .html file, the special symbols (I guess the unicode symbols) are replaced by <U+00EB>. I've tried to look up whether this is some kind of variation of HTML elements that I didn't know about yet, but I have been unable to find anything about this.
(note: any link to a place where I can learn more about this (and, while we're at it, why my browser doesn't show it as, for example, the ë it represents, is also greatly appreciated!)
Reproducable example
Contents of example.php:
<?php
shell_exec("R --file='/home/username/public_html/subdirectory/example.R' --no-save 2>&1");
?>
Contents of example.R (this is what I needed on my server):
workingPath <- "/home/username/public_html/subdirectory";
### Set path to RStudio's pandoc version
Sys.setenv(PATH=paste(Sys.getenv("PATH"),
"/usr/lib/rstudio-server/bin/pandoc",
sep=":"));
### Set HOME and LANG
Sys.setenv(HOME = '/home/username');
Sys.setenv(LANG = 'en_US.UTF-8');
require(rmarkdown);
renderResults <-
render(file.path(workingPath, 'example.Rmd'),
output_file = file.path(workingPath, 'example.html'),
intermediates_dir = file.path(workingPath, 'tmp'),
encoding="UTF-8");
Contents of example.Rmd:
---
title: 'Reproducable example'
output: html_document
---
```{r}
cat("This is an ë symbol.");
```
Results of this example:
When I run this from R Studio, I get:
cat("This is an ë symbol.");
## This is an ë symbol.
When I run this from PHP, I get:
cat("This is an ë symbol.");
## This is an <U+00EB> symbol.
(note how, interestingly, the echo'ed ë does show up normally...)
I now resorted to doing a str_replace in the index.php file, but that's not ideal.
I've checked the render manual, but I can't find anything about this behavior.
I've also looked at specifying options for pandoc in the YAML header of the .Rmd file, but the only thing that seems to come close is the --ascii option, and that doesn't do anything. The R Studio RMarkdown page doesn't provide any hints, either.
Could it perhaps have to do with environment variables that are set in RStudio? I already had to set:
Sys.setenv(HOME = '/home/oupsyusr');
Sys.setenv(LANG = 'en_US.UTF-8');
in the R script to get Pandoc going in the first place when called in the R script called from the PHP shell; but if this is the problem, how can I figure out which settings RStudio sets to which values, or more accurately, which of those are important? I ran:
Sys.getenv()
From within R Studio, and that shows quite a list. I recognize none of the entries as having to do with encoding or so.
Or, does knitr cause this? When I store and inspect the .md file, the Unicode element things already show up. However, the knitr help page with chunk options doesn't say anything about unicode or encoding in general.
Does anybody know where this is documented, or does anybody happen to have encountered this situation before?
I'm running RStudio 0.99.903 and R 3.3.1 on CentOS 6.8.
Usually, issues of this form (where unicode characters are converted to a unicode code point representation, e.g. <U+00EB> in this case) are caused by an attempt to run R within a non-UTF-8 locale.
Typically, this can be verified by checking the output of Sys.getlocale("LC_ALL"). If you see a C locale reported, then you likely need to enforce a UTF-8 locale with something like:
Sys.setlocale("LC_ALL", "en_US.UTF-8")
substituting the particular UTF-8 locale flavor based on your desired language. (For reference, the set of available locales can usually be queried from a terminal with something like locale -a).

R Studio Convert .RMD to pdf

all
Ufff... I believe I did my hw to research this and still don't have an answer so asking all:
I have R-Studio, valid RMD file, when I tried to I don't get new .pdf file, I have viewer which is still html.
No problem to create HTML, it works fine, but I need pdf.
tried to use render ("file.RMD", "file.PDF") to find that I don't have render, tried install pandoc, got an error < package ‘pandoc’ is not available (for R version 3.1.2)>
so what is the simple way to get that pdf, why my doesn't work as desinged? what I'm missing I put that info into header too...
output: pdf_document
Im totaly lost, appreciate help.
render("Proj1.RMD", "Proj1.pdf")
Best
Mario

gvisMotionChart is not working in R markdown file

I need to embed gvisMotionChart into an R markdown file and find an example interactive which is really helpful. However when it comes to the googlevis plotting I keep getting this error when I press the "run code" button:
connection to openCPU failed:
error
undefined
NetworkError: A network error occurred
Can anybody figure out the solutions?
Thanks in advance.
The googleVis package creates the javascript that has to be embedded into an HTML document in order for the interactive plot to run on a webpage. You won't be able to get it to work in the console or in Rstudio for that reason. The only way to know for sure if it's working is to knit it to HTML, and then publish it to Rpubs (or anywhere else online). Once it is online it will work. Depending on your browser settings, you might be able to get it to work by opening the HTML locally, but the browser often blocks the embedded plot when opened locally.
Without seeing your actual code, I can't provide you with anything more specific than that.
EDIT: I thought I would add that several months ago I made a markdown file detailing interactive plots in R for a class. You can find it here: http://rpubs.com/crmhaske/uwloo670_Interactive_Plots
If you scroll down to the very end you'll see the example I did using gvisMotionChart. You have to make sure the place where you print the graph is in it's own chunk, and that results is asis:
```{r results='asis'}
print(p,'chart')
```

Resources