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)
Related
Is it possible to interact with the RStudio application using R code inside it? I mean interactions like opening a file in a card, creating a tab for a new unsaved file or changing some text inside an opened tab.
I know a very similar thing can be obtained by just simply creating a new text file or changing its content with R but this way it doesn't interact anyway with the RStudio app itself.
Some context: I was thinking of a tool that could automate inserting some reprexes / snippets of code which could work as a line of code that, when run from a file, replaces itself with a block of code or make a new unsaved file tab and put some code inside it. And yes, I know a very similar thing can be achieved other ways (e.g. simply copying the intended code block into the clipboard) but I'm curious and exploring the possibilities.
Thanks to the link provided by Konrad Rudolph I managed to find the answer myself.
There is a package called rstudioapi built into the RStudio that allows many different functionalities and doesn't require using plugins or addins.
All the features can be found in the official manual.
Opening a new unsaved file tab with some code in it can be obtained by running:
rstudioapi::documentNew(
"example code here",
type = "r",
position = rstudioapi::document_position(0, 0),
execute = FALSE
)
Inserting code can be easily done with insertText(text = "") which inserts text at the current position of the text cursor.
Changing one line into another can be obtained with the combination of getActiveDocumentContext(), which returns among others the document content and the selection range which is basically equivalent to the cursor position. Then the changing itself can be done with modifyRange() respectively to the cursor position and/or the document content.
That allows many possibilities including for example some smoother automation of the workflow.
This issue is really strange, I want to read a csv file and after getting rid of all unnecessary parts my entire code boils down to this two-liner:
library(tidyverse)
read_csv('data1.csv')
If I knit the Rmd file, it works and a new webpage opens as usual:
However, if I run it either by (1) clicking the green play button; or (2) clicking Run -> Run All button:
Then it just doesn't work (In case you are wondering whether or not there is a third line of code, I make the scope of the screenshot larger). The code is so short that I have no idea what could possibly be wrong.
Following the comment from #user12728748, I changed
read_csv('data1.csv')
to
readr::read_csv('data1.csv')
and it works! But this is still odd since my understanding is that suppose there aren't namespace conflicts prepending namespace is not needed.
Loading readr explicitly, regardless of the order, does not work since it is loaded by tidyverse already:
I, an R user, decided recently to try using Emacs-ESS combo. So far I have been work in a single-window mode (C-x 1) just for text highlighting.
Now I am trying split it in two windows (C-x 3) to work on an .R file in the left window, and have R execute the commands in the right window. Something like this:
After selecting sections of code on the left, how can I "send" it as a command to the right? Essentially, I want the input to remain on on the left (so that I can incrementally build the code up) and the actual R output on (including error messages) the right.
Two good options:
ess-eval-region-or-function-or-paragraph vis
(C-M-x): Sends the current selected region or function or paragraph.
ess-eval-region-or-function-or-paragraph-and-step (C-c C-c): Like ess-eval-region-or-function-or-paragraph but steps to next line of code.
Source: ESS manual.
You can also use:
C-c-p to send a paragraph or region between two empty lines
C-c-r to send a region that has been selected and is highlighted
I prefer this way because you only need to press the Ctrl key
there are normally many ways to send code to the R console. It will take you time to realize what is best for you, or you may eventually change the key bindings.
I have tried different shortcuts within the code>run region menu, but I can't seem to find a command that runs all code within a function or an if statement, so for very long functions I have to either highlight the whole function or used ctrl+enter multiple times. In Sublime Text for example using ctrl+enter sends the whole code section.
Ctrl Alt F should do the trick
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).