Generate a Pop-up box in R - r

Is it possible to make a pop-up box appear to the user to input a value in R? I've put together a pair of lines that perform the equivalent function using readline, but is it possible to make the prompt appear somewhere other than the console? Thank you!

The svDialogs package provides one solution.
Running the following, for example...
library(svDialogs)
## Ask something...
user <- dlgInput("Who are you?", Sys.info()["user"])$res
... gets me the following pop-up input box:
(Whatever value is in the box when you click 'OK' is, as you'd expect, assigned to the symbol user.)

This answer to a similar question shows how to do what you want using the tcltk package (actually it does a little bit more, so could be shortened for what you want). The code is longer than the svDialogs option suggested by Josh O'Brien, but does not require the installation of an additional package (tcltk is one of those that install with R).

Related

Asking for an input and printing in a C interface for R

I'm trying to modify a CRAN package. From what I understand, they use a C interface using .Call().
So I made changes in the C code (can't do it anywhere else since it's in a loop) but I need to ask the user to input an integer.
I read the "Writing R extension" doc and found out that you need to use specific functions as Rprintf() instead of printf().
But I can't seem to find a way to replace scanf() so how can we ask for an input?
And finally is Rprintf() supposed to print in the R console because it is what I'd want but I can't find where it is printed?
Edit:
I'm unable to try it right now but it may seem that using capture_output() may work to get the Rprintf() output. Therefore the only remaining issue would be the scanf() :)
Thanks a lot!

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

Equivalent of Matlab's "Run and Time" for R

When I've written stuff in Matlab, I've often greatly appreciated its "Run and Time" functionality: for those who don't know, this runs the file and upon completion outputs not only the run time, but also opens a new window showing the code, and saying how many times each line was run and how long the program spent on each line. For finding bottlenecks in my code, this has been invaluable!
I am not aware of a similar functionality in R -- whether that be an R package, or part of RStudio -- and searching using a well-known search engine has not rectified this.
Is it possible to do a similar thing for R? It would be most appreciated!
It would help you if you knew that the "Run and Time" option in MATLAB is simply a user interface on top of the profile command. In particular, in MATLAB you can do
profile on
% Run some code
profile off; profile viewer % Stops profiling and opens the timing window
I say this is helpful because you can "profile" in a similar way in RStudio, via the "Profile" menu.
Please see this RStudio Support page for in depth details.
To summarise the above RStudio help page, in essence, one wants to write
profvis({
#CODE
})
(Note that the package profvis may need to be installed.) Further details on how to use can be found by typing ?Rprof, and by visiting this related SO question: How to efficiently use Rprof in R?.

R: create text input panel and process it in a function

I'd like to parse pieces of text to pull out certain lines using R and I wrote a function for this purpose. The function is fine but so far I have to use readLines() to read a .txt file and then process it using my function.
However, ideally I'd like to create a popup window consiting of a text entry box, a "do it" button and a return text box. I could then copy-paste the text in a text-entry box, press the "do it" button (to send it to my funtion) and get the desired lines in the text box.
I played around with the rpanel package and its rp.textentry and rp.text functions but I'm having some problems understanding how the whole package works...
I would be grateful if you have suggestions how to make such a popup window with rpanel or any other package in R. Thanks!
There is a package called svDialogs. The following is the link to the documentation:
http://cran.r-project.org/web/packages/svDialogs/svDialogs.pdf
There is a function called dlgInput.
This is the simplest way to create widgets. Otherwise, my recommended solution is to use tcltk2 package.
Then:
library(tcltk)
tt<-toplevel(width=100,height=100)
submit<-function(){
print(tkget(input.text,"0.0","end"))
tkdestroy(tt)
}
input.text<-tktext(tt,width=100)
submit.bt<-tkbutton(tt,text="submit",command=submit)
tkpack(input.text, submit.bt)

Rstudio editor snippets

Does Rstudio have a mechanism to configure snippets of code, like Geany for example? For faster writing whole sections of user predefined frequent code.
It is not exactly the same as TAB completion already built in rstudio.
Example by mimicking geany snippets
While snippet definition is like line below:
fun = %cursor% <- function(x, ...)\s{\n\n}\n
the usage is like this:
fun<TAB> (like bash style completion)
# will end up in following insertion:
<- function(x, ...) {
}
so the user can then faster write the code by using her own snippets definition. And user can define ANY snippet of any size for completion by TAB.
It is not the Rstudio extract cmd, nieder Rstudio existing TAB context browser.
Code snippets are available in RStudio version 0.99.
https://support.rstudio.com/hc/en-us/articles/204463668-Code-Snippets
The "Extract Function" feature in RStudio may be what you're looking for. Scroll down to the Extract Function section and accompanying screenshot on this page of rstudio.com's documentation: http://www.rstudio.com/ide/docs/using/source
The text of the section reads, "RStudio can analyze a selection of code from within the source editor and automatically convert it into a re-usable function. Any 'free' variables within the selection (objects that are referenced but not created within the selection) are converted into function arguments."
Also see this screenshot: http://www.rstudio.com/images/screenshots/rstudio-code-transform.png
I do not know of such functionality. However, if you want to quickly want to implement functionality with small changes you could also achieve this using functions.
Ok, your question is now clear to me. To my knowledge, Rstudio currently does not have this kind of functionality. You could, however, post a request on their forum for this feature. They respond quite actively to these kinds of requests, so you could give it a try.

Resources