Configure fix() and edit() to open in Notepad++ from R/RStudio - r

When I do this in RStudio or RGUI:
fix(SomeFunction)
(or using edit()) I can see the function's code in Notepad. Is there any way that I could change this so that the code preview opens in Notepad++ rather than plain old Notepad? And similarly, is there anyway that I could force View(SomeDataFrame) to open up in Excel?

fix and edit functions invoke the editor defined in the "editor" argument.
By default, that argument is set to getOption('editor') as shown in the edit function documentation.
Therefore, you can either pass the notepad++ path as function argument, i.e. :
path <- "C:\\Program Files (x86)\\Notepad++\\Notepad++.exe"
fix(somefunction,editor=path)
or set notepad++ as default editor by changin R options for the current session i.e. :
path <- "C:\\Program Files (x86)\\Notepad++\\Notepad++.exe"
options(editor=path)
# from now on, all calls to fix and edit will open notepad++ as default editor...
fix(somefunction)
N.B.
If you want to set the new option as the default for all the next sessions, you should edit Rprofile.site script in RHome\etc path as explained here.

You could try something like this to create a temporary .csv and open in Excel.

Related

Outsource function/script permanently

In every Script I paste the same path to get some functions/r-scripts. Is there a possibility to save this path globally and permanently? It should be independently of the workingspace or other loading data paths and should be available on every new R-Session.
Thanks.
1) Place this in your .Rprofile file. The messsage line is optional but will allow you to confirm that .Rprofile was run when you start R.
message("Hello")
options("mypath" = "...path goes here...")
and then in your script get it using the following which will use that path if the mypath option was defined or the current directory otherwise. Omit the second argument if you don't want to have a default.
getOption("mypath", ".")
2) You could also just put this in your .Rprofile
mypath <- "...your path goes here..."
and then just refer to mypath in the script but that will cause mypath to clutter up your workspace which you may not want.
3) R also will read the environment variables defined in .Renviron so if that file contained:
MYPATH="...path goes here..."
then the script could get it via Sys.getenv("MYPATH") .
See ?Startup for more information on .Rprofile and .Renviron .
4) You could also create an exported shell variable on Linux or environment variable on Windows and then read it in the script using Sys.getenv .

How to extract path from Windows shortcut in R?

I have a shortcut to a file (or a folder) under Windows, and I would like to resolve the path name under R and use that info to open a file.
The shortcut is called shortcut.lnk and it is placed in my working directory, and the shortcut is directed to another place, say C:\Users\XX\Desktop\something.txt
I would like to extract the path name of the shortcut to use that info to open the file, something like: read.table(resolved.link). I tried Sys.readlink, but it does not work on Windows.
This could help you :
library(R.utils)
lnk <- readWindowsShortcut("C:/Users/indi/Desktop/a.lnk")
lnk$pathname
More about it here

Saving .R script File Using Script

I am using R Studio and I want to save my script (i.e., the upper left panel). However, the only ways that I can find to do it are by either clicking the blue floppy disk icon to save or using the drop down menu File > Save > name.R
Is there any way besides using these shortcuts to save the script to a .R file or is the shortcut the only way?
Thanks.
You can use rstudioapi::documentSave() to save the currently open script file to disk.
From the source documentation, one can see that it can be used in conjunction with the id returned with getActiveDocumentContext()$id to make sure the document saved is the one running the script.
For your intended use, try:
rstudioapi::documentSave(rstudioapi::getActiveDocumentContext()$id)
For future reference, here is the reference manual of rstudioapi:
https://cran.rstudio.com/web/packages/rstudioapi/rstudioapi.pdf
I'm not yet allowed to comment, but this refers to the comment above that this does not work with .rmd files:
rstudioapi::documentSave(rstudioapi::getActiveDocumentContext()$id)
I tried and in Rstudio Version 1.2.5042 it does seem to work.
Every new tab in R(created by ctrl+shift+n) can be independently saved by using ctrl+s in the respective tab. If you intend to rename the file though, you may do it as you would rename any file in windows(goto the file location and single click on the filename). Hope my answer was of some help!

Calling Skim from inside R

I'm making a simple line in r to automatically open my generated plots.
I output the plots to a file called "plots.pdf" in the same directory as my r file, and at the end i use this two lines to try to open it:
dir <- paste("/Applications/Skim.app/Contents/MacOS/Skim ",getwd(),"/plots.pdf",sep="")
system(dir)
Basically, dir concatenates the full path of the skim app and the full path of the generated plot.
If i run the string stored at dir in a shell it works perfect, it opens the pdf file in Skim, but when i run it with system() from inside R it doesn't work (Skim says 'The document “plots.pdf” could not be opened.').
I believe this is a very little mistake somewhere in the syntax regarding the absolute/relative paths, but haven't managed to find it... Any advice is welcome! (Or a better way to achieve the same)
I found a way to bypass that problem, i just changed the path to Skim for the 'open' command and i let the system to assign the default app for pdf viewing. So:
dir <- paste("open ",getwd(),"/plots.pdf",sep="")
And it works.

How do I open a script file in RStudio using an R command?

I have a simple script file called test.R. It's saved in the working directory. When I go to File > Open > test.R, it opens the file in the editor (without executing the code, which is what I want).
How do I do this by typing a command in the console window? To be clear, I just want to open (not execute) the file.
You are looking for file.edit
file.edit('test.R')
should open the file in an editor (if you are in RStudio, this should default to RStudio)
It appears that if you are using RStudio then another option is to use rstudioapi::navigateToFile("test.R")
I had the same issue that utils::file_edit() within my package opens outside R-studio. I changed it to usethis::edit_file(). With this function the file opens inside R-studio.
Another alternative is the shell.exec function.
shell.exec("text.R") # This will open the file or URL using the path associated with it
Also, I think for your use case. This code snippet might be a little bit useful.
file <- "example.csv"
sub_dir <- "subdirectory"
dir.create(sub_dir)
writeLines("myfile",file.path(sub_dir, file))
# Works
shell.exec(file.path(sub_dir, file, fsep = "\\"))
shell.exec(file.path(sub_dir, file))

Resources