1

I am running an R script daily that I would like to prompt me to enter data when selecting all the whole script.

I have already tried readline(prompt = ), which prompts in the rstudio console, but it does not prompt me if I select all code to run. I also did not like the prompt being in the console because it was easy to overlook.

I have also looked into library(tcltk), in hopes that a message box could help, but nothing I tried seemed to work.

neilfws
  • 32,751
  • 5
  • 50
  • 63
Philip
  • 39
  • 1
  • 12
  • 2
    What did you try exactly and how did it "not work" specifically? It's easier just to pass in values to R scripts via command line parameters and such. Creating graphical user interfaces really isn't R's strong suit (outside of something like shiny). – MrFlick Dec 04 '18 at 20:32
  • @MrFlick I tried `tk_messageBox` because it seemed be the only thing in that package that might be what I am looking for, but it does not have the option to input a string. I agree that a graphical interface is not ideal, but I was just trying anything I could think of. – Philip Dec 04 '18 at 20:48
  • Do you want it to just **reread the same data or filename every invocation, or do you want it to prompt you for a new value each time** (e.g. a date, stock ticker, search term etc.)? If the former, set up a config file that gets imported. If the latter, pass in a command-line keyword (or edit your config file). Unless you *really* want to invest time in developing a GUI, which is not R's strong suit. It would help hugely if you told us how you want to *"enter data"*, and what type, and how much of it. – smci Dec 04 '18 at 23:12
  • @smci maybe it will help if I am a little more specific. The script I am running has a variable that has a date stored that will be referenced throughout the script. I would like the prompt work as a reminder to change the date. The reason while the prompt is necessary is because I have to hand this off to someone relatively unfamiliar with rstudio and will not necessary know to change necessary variables unless prompted to do so. – Philip Dec 05 '18 at 21:18
  • There is zero reason why you *must* run R code in RStudio. RStudio is only an IDE for R. You can simply run on the (Linux/Windows) shell, using R. [What's the best way to use R scripts on the command line (terminal)?](https://stackoverflow.com/questions/750786/whats-the-best-way-to-use-r-scripts-on-the-command-line-terminal). That will allow you to pass whatever command-line args you want, and avoid wasting time coming up with a GUI to workaround RStudio. In any case, if you ever want to run your code in batch mode, automation, unit-testing, continuous integration etc., you'll need to. – smci Dec 05 '18 at 22:11
  • There are tons of existing questions on "run R script command line" or "with pararameters", if command-line is what you really want then this question is a duplicate, – smci Dec 05 '18 at 22:13

1 Answers1

1

Here's a method using library(tcltk)

EntryBox <- function(label = 'Enter', title = 'Entry Box') {
    tt <- tktoplevel()
    tkwm.title(tt, title)   
    done <- tclVar(0)
    tkbind(tt,"<Destroy>", function() tclvalue(done) <- 2)
    result <- tclVar("")
    cancel.but <- tkbutton(tt, text='Cancel', command=function() tclvalue(done) <- 2)
    submit.but <- tkbutton(tt, text="Submit", command=function() tclvalue(done) <- 1)
    tkgrid(tklabel(tt, text=label),  tkentry(tt, textvariable=result), pady=3, padx=3)
    tkgrid(submit.but, cancel.but, pady=3, padx=3)
    tkfocus(tt)
    tkwait.variable(done)
    if(tclvalue(done) != 1) result <- "" else result <- tclvalue(result)
    tkdestroy(tt)
  return(result)
}

x <- EntryBox(label = 'Enter a string'); x
dww
  • 30,425
  • 5
  • 68
  • 111