MobaXterm console and Rscript - r

I recently started using MobaXterm console and I would like to execute Rscripts in it. I read that I should include # !usr/bin... code in my R script, but this doesnt work, as Moba still doesn't recognize Rscript command.
I also tried to move Rscript.exe in the directory where the R script is, but it doesn't work either.
I.e. Rscript my_code.R
The code is straightforward :print("Hello"), and doesn't send in error when ran in Rstudio

To make Rscript work from Windows shell in Windows 10:
-Right click on lower right part screen (windows legend) to open the menu
-Click on System
-Click on Associate Parameters / System information (right part of screen)
-Click on System advanced parameters
-Click on Advanced / Environmental variables
-In the lower window (system variables) click on "Path" and edit
-Add the location of Rscript.exe (i.e. C:\Program Files\R\R-3.4.4\bin)
To test whether it works:
-Right click on lower right part screen (windows legend) to open the menu
-Windows PowerShell
-Type Rscript
-Text should come out

Related

Best way to add environment variables to MobaXterm

I'm just now setting up MobaXTerm on my local computer and I'm looking to see what is the best way to port over all my environment variables I have on my windows host over to MobaXTerm. The whole goal of this exercise is that I plan to not use command prompt/git bash but instead just use MobaXterm.
For example, if I go to my Command Prompt and type in 'python', I can type Python code in the command line. Now when I do this in MobaXTerm, it says that I don't have it installed. This extends to more applications so I would rather not try and manually install git,python, etc. in my MobaXTerm manually.
I've extracted a list of all environment variables on my Windows host by typing in 'set > paths.txt' in the command prompt, but I want my MobaXTerm command line to reference these when I am using the terminal there. What is the best way to do this? Is it setting it up in a .csrhc?
Edit: I tried changing the settings in MobaXterm to use the Windows PATH environment, and even when I restarted my computer I still haven't gotten it to work
In the "Terminal tab settings" section, so you will find this option in the "Terminal" tab.
Go to MobaXterm global settings window, then click on the "Terminal" tab and check the "Use Windows PATH environment". Note that if you are using a session, you will have to do the same in this session: edit your session, then go to the "Terminal settings" tab and check the "Use Windows PATH" option.
I had the same issues while trying to use git. It looks like MobaXterm keeps using his own executables located %USERPROFILE%\Documents\MobaXterm\slash\etc, and even recreates them when you delete them.
You can still force MobaXterm to use windows PATH configurated executables by launching cmd /c yourcommand, in my case cmd /c git push. It will run windows CMD command using your PATH environment variable.
You have to check "Use Windows PATH" on the "Terminal settings" of your MobaXterm session settings in order to works.

R pop-up boxes not working when run in terminal

I am using the svDialogs (an R wrapper library for zenity) to create GUI pop-up boxes, and this works fine when I run the code through either R studio, or from an R terminal session (running Ubuntu 16.04).
A minimal example is:
library(svDialogs)
dlgMessage("Hello Stackoverflow!")
However, when I run the code directly through the terminal it does not work:
Rscript --vanilla -e 'source("path/to/file.R")'
The terminal shows that the library loaded, and does not display an error message: but the pop-up does not appear! If I add an additional line after the call to dlgMessage, that line runs. i.e. if I run the modified code
library(svDialogs)
dlgMessage("Hello Stackoverflow!")
print("Goodbye Stackoverflow!")
then the second line does show in the terminal window (i.e. the code is not crashing at dlgMessage).
Happy for solutions not relying on dlgMessage if there is a workarond: I'd previously tried using Zenity natively through R using system() but couldn't get this to work.
R can be run in either interactive or non-interactive modes, with the default depending on whether or not it is assumed that there is a human operator, see documentation for interactive.
When run in non-interactive mode, R will not display any pop-up boxes. The default is that when running code in the terminal, R runs in non-interactive mode. Following the documentation above, this can be overwritten by using the command in linux
R --vanilla --interactive < "path/to/file.R"
Similarly in Windows using --ess with Rterm.exe

rmarkdown: specify manually pandoc path?

I have a rmarkdown document that I render regularly with rmarkdown::render
It works fine on my computer (Windows) as I have RStudio installed, and it has setup automatically the path to pandoc.
However when I try to run it on my server (outside RStudio), it fails with error that pandoc is not found.
How can I set up manually the path the pandoc from my script? Without changing the configuration on the server.
I have tried this but it doesn't work:
Sys.setenv(RSTUDIO_PANDOC="PATH_TO_MY_PANDOC_BIN/binaries/pandoc/pandoc.exe")
I suggest to set the system path for pandoc (Windows 10 instructions):
1. In the Start menu, search for "Edit the system environment variables"
2. At this point, Windows may ask you for username + password (for a user with permission to change system settings)
3. Click "Environment Variables" button
4. In the lower part of the window (System variables), select the line with "Path" and click Edit
5. Add the path to the folder where pandoc.exe is, but not including "pandoc.exe" or a slash at the end
Sys.setenv(RSTUDIO_PANDOC="PATH_TO_MY_PANDOC_BIN/binaries/pandoc/pandoc.exe")
That is actually almost correct, just omit the *.exe files, write the path only up to folder contain pandoc.exe
like this :
Sys.setenv(RSTUDIO_PANDOC="PATH_TO_MY_PANDOC_BIN/binaries/pandoc")
i believe this doesn't require admin rights

Creating a desktop icon for Shiny App

I have a shiny app that opens in the browser when I provide the following code in the base R prompt:
shiny::runApp("C:/Myapp")
I use windows 7. I am trying to create a desktop icon to avoid my client typing the above code every time he wants to use the app. I have created a desktop icon and the set the path in "Target" as follows:
"C:\Program Files\R\R-3.2.2\bin\R.exe" -e "shiny::runApp("C:\Myapp")
and in the "start in" box I have included
"C:\Myapp"
The app is not opening. I have tried changing the \ to / in C:/Myapp - doesn't work. However, when I try the following:
"C:\Program Files\R\R-3.2.2\bin\R.exe" -e "shiny::runApp()
that is, without referring to my app folder, rhe R program runs, loads the code shiny::runApp() and prints the message
Listening on http://127.0.0.1:4354
Can someone help on how to resolve this? I have tried various combinations of the above.
First, if your app folder is "C:\Documents\myApp", then your working directory should be "C:\Documents" (to insert in "start in" box).
Second, use ' ' for your inner quotes:
"C:\Program Files\R\R-3.2.2\bin\R.exe" -e "shiny::runApp('C:/Myapp')"
Third, consider launching your browser with your runApp command. Otherwise there might be nothing to see. (shiny::runApp('C:/Myapp', launch.browser = TRUE))
The following code saved as a '.bat' file worked for me on Windows 10 as suggested by RBloggers
start "" "C:\Program Files\R\R-4.0.3\bin\Rscript.exe" C:\RShiny\MyShinyApp\app.R /k
start "MyShinyApp" "http://127.0.0.1:4354/"

How do you configure Msys's default size, color, and font?

I've been exploring use of MSys lately as an alternative, 'nix-like shell for my windows development. However, the default colors and size are driving me crazy. Anyone have any idea as to how I can change the default size, color, and / or font? Honestly, I'd be happy if I could make the default character width/height of the shell larger, but the others would be nice too...
Clarification: the msys shell is a separate app from the windows shell - the normal 'right-click-upper-left -> properties' configuration (like with the windows shell) is not present.
Looks like underneath it's firing off rxvt. Thanks!
If you're running MSYS bash from a command line, Behind the scenes it's a windows command shell window, so configuring it is just the same.
Right click on the title bar at the
top of the shell window and select
'properties'
Configure the window with the options
on the tabs on the dialog; these
should be fairly self-explanatory.
When you close the dialog, select
'Modify shortcut that started this
window' to save the configuration
back as defaults.
EDIT: If you're running the shell in an xterm or RXVT window through an X server, start the xterm window with -fg (foreground), -bg (background) and -fn (font). Can't remember what the RXVT switches are, but you should be able to see them in the man page. You can also configure them through your .Xdefaults file. Again, see the man page for details.
EDIT: Thanks Paulo - note that MSYS also comes with an X-less version of RXVT, which the OP seems to be using. Here's a link for configuring RXVT on MSYS.
Open the msys.bat file an aroun line 72 it reads like this:
rem Setup the default colors for rxvt.
if "x%MSYSBGCOLOR%" == "x" set MSYSBGCOLOR=White
if "x%MSYSFGCOLOR%" == "x" set MSYSFGCOLOR=Black
if "x%MINGW32BGCOLOR%" == "x" set MINGW32BGCOLOR=LightYellow
if "x%MINGW32FGCOLOR%" == "x" set MINGW32FGCOLOR=Navy
if "%MSYSTEM%" == "MSYS" set BGCOLOR=%MSYSBGCOLOR%
if "%MSYSTEM%" == "MSYS" set FGCOLOR=%MSYSFGCOLOR%
if "%MSYSTEM%" == "MINGW32" set BGCOLOR=%MINGW32BGCOLOR%
if "%MSYSTEM%" == "MINGW32" set FGCOLOR=%MINGW32FGCOLOR%
change the first 4 lines with the colors you like, to change the font, just bellow:
start %WD%rxvt -backspacekey -sl 2500 -fg %FGCOLOR% -bg %BGCOLOR% -sr -fn Courier-12 -tn msys -geometry 80x25 -e /bin/sh --login -i
change the Courier-12 with your favourite font and the 80x25 with your prefered window size.
I create HOME env var, which have value like 'e:\home' and put into %HOME% dir .Xdefaults file with such content:
Rxvt*background: white
Rxvt*foreground: black
Rxvt*geometry: 120x40
Rxvt*font: 7x14
Rxvt*scrollBar: on
Rxvt*saveLines: 5000
Also note that this config successfully work on Cygwin and various Unix boxes.
If you're tired of using cmd for msys git, I recommend using minty and/or ConEmu
:
to use mintty, download latest version from google code/mintty and drop it in the bin folder of git msys + make following shortcut:
"C:\Program Files (x86)\Git\bin\mintty.exe" --title "Git Bash" -w full --exec "C:\Program Files (x86)\Git\bin\sh.exe" --login -i
(set working dir to your home dir)
to use vim, make sure to read this bug report
for colors, make sure gitconfig is set to color = always
I set sublime as my default editor as vim didn't work, see notes below
note:
vim packaged with msys expects cmd terminal and won't work with mintty
interaction to enter username/password for git pushes using basic http auth won't work unless you host mitty in ConEmu. (ssh works fine on mitty by itself)
ref:
rpavlik/git-windows-mintty - notice this does not bundle latest mintty version.
/oumu/mintty-color-schemes

Resources