Rstudio: Cmd + C/V not working in editor - r

I have used pipe to copy and paste data between Rstudio (v0.99.467) and Excel on my Mac OSX 10.9.5.
pipe("pbcopy", "w")
pipe("pbpaste")
For some time, I tried to use pipe("pbcopy", "r"), but Rstudio is not responding (because my code is wrong). After a while, I found Cmd + C/V is not working in the editor any more (but it still works in the R console). I re-install R-studio, removed .rstudio-desktop, the problem still exists. Does anyone know what is going on? Can I remove the .bash file that stores the Rstudio shortcut preferences (assuming re-install won't delete it)? BTW, where is the shortcut .bash file in Rstudio?

On OSX Mojave using R 3.5.1, you can use the following block to capture the clipboard:
clipboard <- system("pbpaste", intern = T)
I can also confirm that the following block is working:
clipboard <- scan(pipe("pbpaste", "r"), what = character())
However, connections are sometimes tricky to work with. For example:
clipboard <- readLines(pipe("pbpaste", "r"))
Returns an empty character vector, likely because there's no newline terminator in the clipboard!

Related

double ~ in R interactive terminal in VSCode

I am trying to use R in VSCode. I downloaded the R extension and followed the steps specified but I keep getting an annoying issue: when I use Alt+126 to write the character '~' in the script everything is fine, but when I run the line, in the R interactive terminal it appears twice. Also, if I use Alt+126 in the R interactive terminal, '~~' appears instead of '~' (two times the character instead of once). I have no idea why this is happening, I tried to uninstall and reinstall both VSCode and R and reset the pc but nothing changed.
I hope somebody knows how to solve this, thanks in advance!

Cloudera Workbench string enoding problem

I am pulling changes from a git repo where my coworker pushed R codes from his local windows
word <- gsub("=gesellschaftmitbeschränkterhaftung=","",fixed = T,x = word)
The code contains weird letters, such as "German Umlaute", e.g., "ä" in the example above. On windows, this works fine. But when I open the same code on the Cloudera Data Science Workbench, it messed up the special character:
word <- gsub("=gesellschaftmitbeschr�nkterhaftung=","",fixed = T,x = word)
I could manually replace it, but that is obviously a very painful solution and defeats the purpose of git. Is there any way to circumvent this issue? Find here the original R code, as pushed to Git from the windows machine containing all lines that cause issues.

Where can I find the logs for R executable files?

I have a file in R that I open with Rscript ( R for front-end windows) and it doesn't work, even if on other laptop it works. The black screen open and close in 1 second and I can't see the error.
Is there a way to find the log or smth to see why doesn't work? The file is simple, just with 3 command for install packages.
Thank you,
Edit: Here is the code. I repeat, this worked on other laptop. For sure there is an error, but because the window is close so quick, I can't see where is the problem.
install.packages("exifr",repos = "http://cran.us.r-project.org")
install.packages("dplyr",repos = "http://cran.us.r-project.org")
install.packages("rlang",repos = "http://cran.us.r-project.org")
install.packages("leaflet",repos = "http://cran.us.r-project.org")
PS: I have Strawberry installed.
edit 2: I still search for a solution. I got this message when I tried to run with Rscript:
I seems that it just opens command line console, runs the script and closes the console. This way you cannot see the output.
Simple solution (not suggested): open the script in text editor and add line Sys.sleep(60) to the end of the file which will keep the console open for extra minute.
Long-term solution: install RStudio and open the file there
Apparently you can choose a filename for R to put either it's output or it's errors into. The function
´sink` is meant to define exactly that.
Try
help(sink)
in your console to read how to put sink into your script:
sink diverts R output to a connection (and must be used again to finish such a diversion, see below!). If file is a character string, a file connection with that name will be established for the duration of the diversion. [...] Messages sent to stderr() (including those from message, warning and stop) can be diverted by sink(type = "message") (see below).
[...]
zz <- file("all.Rout", open = "wt")
sink(zz)
sink(zz, type = "message")

Magic %% commands in R inside Jupyter

How do I run %%magic in R inside Jupyter?
%%javascript
IPython.OutputArea.auto_scroll_threshold = 99999;
The auto scroll feature on longer output is vastly annoying as I have several functions and scripts that spit out a lot of output.
The above Javascript works fine in python notebooks but not in R notebooks.
When I run the %% magic command in R, it barfs:
Error in parse(text = x, srcfile = src): <text>:1:1: unexpected SPECIAL
1: %%
Any suggestions?
According to this post disable_autoscroll.py, it may be possible to put that Javascript into a profile_dir/static/js/custom.js file. Pray tell, where is the profile_dir on a Windows box?
I found: c:/Anaconda2/Lib/site-packages/notebook/static/custom/custom.js but that is the central custom.js file.
References:
Auto-scrolling of long output should be configurable in the UI
In my anaconda install of the notebook, the custom.js file is in %USERPROFILE%\.jupyter\custom\custom.js.
For the "magics": magics are a thing of the python kernel, not the notebook. The R kernel does not implement a magic system and so these don't work. As mentioned above, use IRdisplay::display_javascript('IPython.OutputArea.auto_scroll_threshold = 99999;') for your usecase.

Alter file.show() behaviour

I'm running file.show() in RStudio on a Mac, like this:
filename <- "/Users/me/reports/myfile.pdf" # replace with file location/name
file.show(file.path(filename), title="My File")
This does two things:
Opens my file.pdf
Opens a blank file in RStudio called "My File".
options()$pdfviewer
returns:
"/usr/bin/open"
How can I stop (2) from happening?
If you want to simply open a PDF file via RStudio on a Mac, the simplest way (as far as I know) is to use the system function:
filename <- "/Users/me/reports/myfile.pdf"
pdf <- getOption("pdfviewer") # same as options()$pdfviewer
cmd <- paste(pdf,filename)
system(cmd)
For example it is used by openPDF in Biobase (see here)
I don't know a way to alter the file.show behaviour in order to prevent a blank file being opened in RStudio. I think it has something to do with The R.app Mac OS X GUI uses its internal pager irrespective of the setting of pager. that you can see on the manual here.
Note: it is different on a Windows machine, use shell.exec(filename) directly.

Resources