How to open the viewer pane in browser? - r

Just a simple question that I couldn't find an answer. I just updated my R Studio and R, and one function that I used a lot was opening the results in Viewer pane in my browser.
For example, with the resulting tables from packages like sjPlot or expss, I used the "Show in new window" to visualize the tables in Chrome or other default browser. Don't know if this was set by a old package, but I can't get this result right now. Actually, when I click this option, nothing is happening.
When I change to my old R version 3.6.2, I can use the "show in new window", but can't make to work in the version 4.0.1.
Anyone would know how can I get this function again?
Thanks

options(viewr=NULL) should do the trick.

In my case, i use browseURL instead of rstudioapi::viewer open file in browser directly.
browseURL(sprintf("%s/test.html", tempdir()))
if your file not in tempdir(), you should copy it to tempdir() first:
system(sprintf("cp %s %s", test_file, tempdir()))

Related

How to refresh plots without needing to close the previous plot?

How do I get VSCode to refresh the plot when making a new plot without needing to close the previous plot tab?
Ideally, it should open up a new tab with the new plot next to the old one.
Moreover, when using Httpgd, even tho the plot refreshes without me having to close the previous plot, it opens up in a separate window outside VSCode. How do I get it to open up as a new tab like in the gif above?
I had the exact same problem and what fixed it for me was simply, in vscode, going into:
Files > Preferences > Settings > section Extensions > scroll down and find R:
find the section "Plot: Use Httpgd" and check it, and restart Vscode.
I can't confirm but I believe it might have started after installing the httpgd package, because I had the behavior you desire before installation, and the exact same behavior you experience after httpgd installation. Of course if my solution works for you (I hope), it will work under httpgd after and not the base default VsCode/R-plot viewer as in your gif.
#coip pointed out that the steps given on this stackoverflow post to me. Downloading the httpgd package from CRAN fixed the problem for me.

Setting up R in Atom: How to Run a Help Page

I successfully set up R in my new Atom editor and can get in-line results using the Hydrogen package. I just noticed, however, that when I run lines to obtain "R Documentation" that would pop up automatically in RStudio, Hydrogen only gives me a check mark in-line result with no associated documentation.
Here is what is going on in my Atom editor when I run ?plot
Here is what happens in RStudio (bottom right pane), which I am hoping I can get in Atom
How can I get this working in Atom?
try hydrogen -> Toggle inspector
I had the same question and actually made a little progress. Perhaps someone more experienced than I can use this to make a package to enable an in-atom help documentation panel for us :)
Anyway you can install the atom package 'script' which will properly output some, but not all, help documents. I have an example of one working & one not working.
Working ?read.csv
Not-working ?geom_bar

Print with syntax color in R-Studio

In R I always like to print out the script since it gives a good overview and one can adjust eventual errors. I like the syntax highlighting in R-Studio because it facilitates reading and fast comprehension of code.
Is there a way to print out the text with the highlighting I see in the editor?
Its not an R-Studio solution, but notepad++ will print R source with syntax highlighting.
RStudio will not print in colour, but it's easy to save the code as a PDF; in this case the syntax format is preserved. My favourite package is knitr.
library(knitr)
stitch("file_name.R")
The default output is PDF/Markup in .tex. If you prefer not to typeset, running the below will export as .html
stitch(script="file_name.R", system.file("misc", "knitr-template.Rhtml", package="knitr"))
Brief explanation
The reason this is an answer to this question in because of the last line of the question:
Is there a way to print out the text with the highlighting I see in
the editor?
so we are not limited to only and only using Rstudio software here.
After exploring the awesome answer by #rrg and realizing that it runs the code line by line, I wrote a comment below his answer and continued googling. My problem is that the code I wrote is so large and so time consuming to run that running it for the sake of having a syntax highlighted version is not feasible.
Most of the solution out there online involves having notepad++ which is a Windows application and I'm a dedicated Linux user, so I searched for a way I can do this in Linux (and possibly Mac)
The way I solved it:
Inspired by a blog post, I used the famous and beloved Vim to convert R to syntax highlighted HTML and then because you can open HTML in your browser, you can what ever you want with it (print, screenshot, etc.)
Activate synax highlighting in Vim:
open terminal
then open the vim config file by typing vim ~/.vimrc
press i from keyboard to go to "insert mode"
go to the end of the file using arrow keys on your keyboard
type syntax on at the end of the file
now you need to save and exit. For this you need to press Esc button from keyboard to come out of "insert mode" and then type :x and press Enter to save and close the file.
if you want to change the color scheme of the syntax highlighting, visit the bottom part of this website
From terminal open your file with Vim:
vim YOUR_FILE_PATH
Having you R code open in vim, you can turn on the line numbers if you like by pressing Esc and then write :set number and press Enter.
For converting R to HTML, press Esc to make sure you are not in "insert mode" and then type :TOhtml and press Enter. This will result is having a split window in terminal, half is your R code and the other half id your new HTML code.
For saving the files, type :x along with Enter button from keyboard twice to save both files (your R file will be unchanged if you have not typed anything extra in it and your HTML file will be created with the same name near your R code)
Now open it with your favorite browser (in my case Vivaldi) and do what ever you want (in my case converting the whole HTML into PNG)
Best way:
download https://github.com/jaredpetersen/codeprinter and paste in the r code. then choose syntax highlighting Xcode
For those using a Mac (and thus without access to Notepad++) cutting and pasting into Xcode and printing from there will also work.
As with Ron Jensen's earlier comment, this isn't an R Studio solution, but in the interests of "just getting it to work", I hope this helps someone.

Make R project Automatically open Specific Scripts

I am working in team, we mainly use R, I am quite used to use R project in Rstudio, which I like because when I open them I have all my scripts and everything at the right place. However when another member of the team opens one of my project it loads the values and data but does not open the R script (one can see that by physically clicking on the project through the windows explorer rather than using the menu at the top right in R). I guess something can be done in the .Rprofile but I did not find any command to open physically a script, I tried
file.edit("./Main.R")
but it did not open anything. It just got me the message :
Error: could not find function "file.edit"
As always,
Thanks for your help !
**EDIT
I tried to use
file.show
file.edit
shell.exec(file.path(getwd()), "Main.R"))
in the .Rprofile. Nothing worked.
Romain
You can use the following code in the .Rprofile file.
setHook("rstudio.sessionInit", function(newSession) {
if (newSession)
rstudioapi::navigateToFile('<file name>', line = -1L, column = -1L)
}, action = "append")
The rstudioapi library has the function navigateToFile to open a file in Rstudio. The problem is that the code in the .Rprofile runs before Rstudio initialization. To deal with this problem you can use the setHook function (from base package) to make the code execute after the Rstudio initialization.
file.edit requires the utils package
library(utils)
file.edit("Master.R")
However, if it opens in Notepad rather than RStudio you have the same problem as me. I've tried editing the editor= in all possible places: .RProfile, RProfile, RProfile.sites, with and without .First() function statements and calls. However, RStudio does not load the .R file in RStudio if told to. It may be linked to the .RData file being loaded after .RProfile. Bug? Or at least a feature RStudio should incorporate in their RProject file specification.

Preventing what was/were previously copied on to my clipboard from appearing on R Console when using R

I am having some problems with my clipboard contents when using R. When I run my scripts/commands in tinn-r, very often I would get something that I had ran earlier pasted onto my R Console instead of the command that I have just selected.
To get over this, I would go to my clipboard and delete its content(text/syntax). However, the same text/syntax that I had just deleted would re-appear on my clipboard and would again appear on my R Console when trying to run a different syntax from my tinn-r.
Good luck. Tinn-R seems to be a particularly buggy way of interacting with R. Though it takes some effort to set up, the StatET plugin interface from Eclipse has been working way better for me than Tinn-R ever did.

Resources