Can I access the colours of the R console, from R code? - r

I sometimes have up to 4 sessions of R, each running a different piece of software, between which I must change often. It would be helpful if the colour of the console background (or text) were to be different depending on which code was loaded, automatically, so that I could locate the window easily on my (cluttered) desktop and avoid typing in the wrong commands into the wrong console in my fast moving financial activities.
Any way to do this from within R code? Currently I am menuing up and changing the colours manually but it's tedious, especially that, under Windows, the R colour dialogue box is 4 lines deep and there are something like 500 colours.

This is not a direct answer to your question, but it might still be helpful. I like using multiple workspaces to solve this problem. If you have different R sessions running, having them on different workspaces (or desktops) separates them nicely. The platforms I work on (OS X and Linux) have native support for this workflow, but there must be tools like this for Windows (a few years back there where at least).

Related

Shiny apps in R: how to structure them correctly

I created my first Shiny app, which runs perfectly in my laptop.
However, I need to submit it to my professor and I want to make sure he will be able to run it.
I have a UI file, a server file, a global file and a process file.
The process file stores the data preparation.
The global file reads two RDS files which are the datasets that I use in the server.
Where should my libraries be loaded? For example, the app does not run without leaflet, how can I ensure the libraries are run automatically?
My RDS files are saved to my local drive, which means that my professor will need to change the path in order to use them, how can avoid this?
Shall I put the UI, server and global into one R script or is it ok to have them onto two different scripts?
Thank you!
As you describe your current set up, the most obvious place to load your libraries is at the start of your global file. (Or at the start of app.R if you move to a single file configuration.) Though not exactly what the reprex package is designed for, you could probably use reprex to make sure that your code is reproducible and independent of anything you may have overlooked. (You've already identified the obvious issue with data files.) Look here for more information on reprex.
Indeed. This is a problem. If you must load the data from files, you need to find a way of providing them to your professor. Telling him to edit your code by hand is not a good way to start. Exactly how to do this depends on the infrastucture your institution has set up, so it's difficult to advise you what to do. Do you not have a shared area for your course? Ask your fellow students - or even your professor.
Whether you bundle the app in a single file or in seperate files is really a matter of choice. I don't think there's a wroing way and a right way. For me, the deciding factor is usually the size of the app. Large apps get several files. For small apps, separation is just an unnecessary complication. Another factor - that doesn't seem to be an issue here - is whether I see the app as a potential front end for some methodology that might be worthy of its own package. In that case, I develop the app as a front end, and the package itself as separate entities.
Question 1 and 3 depend on criteria for code quality like performance and maintainability. When the code grows, there will come the point when it is more difficult to handle all code in one file. Once it grows even further you will encounter the point when you need split the app into modules to keep your code easy to maintain.
Regarding libraries I advise declaring them at the entrance point of an app (though, basically, that is a matter of taste and style). That way, you provide maximum clarity regarding the dependencies of the app. Again, if the app becomes very large and not all parts of the app rely on the same packages, it could improve performance and maintainability that each part loads the packages as required. It could give you a performance advantage when you do not need to load all packages at once. However, that is probably only true for very large apps.
However, since this all seems to be an exercise at university, I doubt that your app will reach higher levels of complexity.
Question 2: In a shiny app you can provide fileInput widget. This SO question shows you how.

Gephi automation: how to make multiple graphs at once

I am looking for a tool to automate making graphs with Gephi. I have dozens of similar graphs to prepare (only labels and figures in data differ), so need something able to automatically:
load nodes and edges from a file (e.g., csv)
set up layout (e.g., "type='force atlas 2', iterations=n, threads=m, scaling=p, gravity=q...")
apply selected laoyut
tune appearance (ranking: color, size)
export graph
I am aware of Gephi Toolkit, but it is for Java. Maybe there is some other way to automate Gephi? (or I will have to learn Java, which is not bad, but requires time)
I've also found Gephi Scripting Plugin, which is good but lacks some features, e.g. can't set up layout (only runs it with default settings).
Thanks in advance!
It is a bit tricky and haven't tried myself but if you don't want to get your feet wet with Java then you could try one of the automation frameworks that allow you to control a GUI application, e.g. Robot, PyAutoGUI or if you use Windows you can use pywinauto.
I know it not a complete answer but it might give you a lead.

R statistics console in QT

I am planning to link a Qt project against R to provide some statistical funcionality. I thought it might be quite cute to add some generality to the project by having an R console as a Qt widget within the tab to allow me to do analyses that I haven't thought of in the design stage later on. I was wondering whether it is something that might be accomplished fairly easily?
In particular I'm stuck on how I would access the RTerm from QT? Has anyone else attempted something similar or can give some hints on where to start?
One of the examples for RInside does something pretty close---in around 200 lines most of which deal with the other GUI aspects, it wraps R functionality inside a Qt application.
The example implements a GUI density slider, and the edit box allows you to write an almost arbitrary R expression, or rather the parts that are then passed into an evaluation to generate random number: rnorm(50), or for a mixture c(rnorm(50), rt(50)) etc. You could possibly build on top of that.
See this blog post for more.
Check out Carson Farmer's work on manageR:
http://www.ftools.ca/manageR/
this is a plugin for Quantum GIS (mapping package) that interfaces it to R, giving you exactly what you want - an R console wrapped in a Qt4 body - as well as data transfer between Qgis and R. It handles plots as well.
I've tried to encourage Carson to produce a standalone R Gui project from this code, but he's a busy guy. Aren't we all?
RStudio is largely written using QT, you should be able to have a look at their code and build something similar into your Qt based application:
https://github.com/rstudio/rstudio

Specifying the font in an R console - multiplatform solutions?

This is fairly mundane as R questions go, but I would like to specify the font in my R console. It's easy enough to do in Windows - John Cook has an excellent little writeup on that.
However, that is specific for the Windows R GUI. I'm not able to find a way to do this (at least from within R) on RStudio, Linux, etc. Is there a global, platform-agnostic solution? If not, what would need to be wrapped to make this feasible given per-platform idiosyncracies?
Or is this infeasible?
Note that I'm interested in the font family, rather than the font size, though font size suggestions are welcome.
Edit: I plan to apply this inside a program, if possible. Since R can make calls to the shell, I am happy with a two-pass method that first configures external files in preparation for a subsequent invocation of R, if that is necessary.
No, each of the front-ends to R are responsible for the way that the console is displayed to the user. This will be inherently platform and front-end specific.
I believe that Gavin is right: this is set per frontend, and I fail to find anything for Linux. However, the standard R frontend for Windows is addressed in my question. Mac users may find this page helpful: Permanently change default console font.
So, the answers as I found them may be summarized as:
Windows / default R - see John Cook's page
Mac - see link above
Linux / default R frontend - not possible (or am I wrong?)
(Windows, Mac, Linux) / Rstudio - Font family is not available (yet) - see this page. Font size is - this page
Windows / Revolution R - this page
Other / Revolution R - not found
So, the universal trick is that I need to use R via a web server, and then I can configure my browser. :) Not going to happen. I'll wait on Rstudio.

Automatically "sourcing" function on change

While I am writing .R functions I constantly need to manually write source("funcname.r") to get the changes reflected in the workspace. I am sure it must be possible to do this automatically. So what I would like would be just to make changes in my function, save the function and be able to use the new function in R workspace without manually "sourcing" this function. How can I do that?
UPDATE: I know about selecting appropriate lines of code and pressing CTRL+R in R Editor (RGui) or using Notepad++ and executing the lines into R. But this approach has a disadvantage of making my workspace console "muddled". I would like to stop this practice if at all possible.
You can use R studio which has a source on save option.
If you are prepared to package your functions into a package, you may enjoy exploring Hadley's devtools package. This provides a suite of tools to write, test and document
packages.
https://github.com/hadley/devtools
This approach offer many advantages, but mainly reloading the package with a minimum of retyping.
You will still have to type load_all("yourpackage") but I find this small amount of typing is small beer compared to the advantages of devtools.
For additional information, including how to setup devtools, have a look at https://github.com/hadley/devtools/wiki/development
If you're using Eclipse + StatET, you can press CTRL+R+S, which saves your script and sources it. As close to automatic as I can get.
If you can get your text editor to run a system command after it saves the file, then you could use something like AutoIt (on Windows) or a batch script (on UNIX-derivative) to pass a call to source off to all running copies of R. But that's a heck of a lot of work for not much gain.
Still, I think it's much more likely to work being event-driven on the text editor end vs. having R constantly scan for updates (or somehow interface with the OS's update-event-messaging-system).
This is likely not possible (automatically detecting disc changes without intervention or running at least one line).
R needs to read into memory functions, so a change on the disc wouldn't be reflected in the workspace without reloading your functions.
If you are into developing R functions, some amount of messiness during your development process will be likely inevitable, but perhaps I could suggest that you try writing an R-package to house your functions?
This has the advantage of being able to robustly document your functions, using lazy loading so that you have access to your functions/datasets immediately without sourcing them.
Don't be afraid of making a package, it's easy with package.skeleton() and doesn't have to go on CRAN but could be for your own personal use without distribution! Just have fun!
Try to accept some messiness during development knowing you are working towards your goal and fighting the good fight of code organization and documentation!
We are only imperfect people, in an imperfect world, but we mean well!

Resources