I have followed this tutorial on R blogger to create a file extension .Rexec to be able to run scripts on double click, without opening RStudio. The script is supposed to do the following :
Select file to be used as data input
Select the decimal separator
Render a
.rmdwith the two above elements as parameters.
At some point, I succeeded to run the script and generate a report. But now, for an unknown reason (I did not update R (which is R-4.2.2patched) nor RStudio (2023.06.1 Build 524) since then), RScript now executes the script in batch mode instead of interactive mode.
test.Rexec is the following:
interactive()
readline(prompt="What is your First name ?")
datainput<-choose.files()
rmarkdown::render(
input="test.rmd",
params=list(datainput=datainput))
for(i in 1:100) {
cat(".")
Sys.sleep(0.01)
}
test.rmd is the following:
---
title: "Untitled"
output: html_document
params:
datainput:
input: file
value: "test.txt"
---
```{r}
read.table(
file=params$datainput,
header=TRUE,
sep="\t")
test.txt is the following:
x y
1.5 2.1
When double clicking on test.Rexec , I can see that interactive() returns FALSE, which confirms that the problem is likely related to the interactive mode. I can select a file, but any other interactive function such as select.list or readline(prompt="...") results in an unexpected termination of the process when it is reached. The render function also induce a crash, making the whole thing unusable.
test.rmd is rendered perfectly when I execute each line of test.Rexec one by one in the console of RStudio. I read on this question that there was a package interactivity that allowed to change this, but it is now archived. Following this question tried creating a file test.bat with the command :
echo source("C:\Users\...\test.R") | R.exe --ess --vanilla without success. Replacing `R.exe` by RScript.exe and Rterm.exe with and without --vanilla also resulted in failure.
The user is not supposed to type any command in a terminal/console by himself, so suggesting to type execute from a command is not an option in this case.
I guess that if you follow the same protocol as explained above, things should go fine. I don't know what I may have done to mess up with RScript and I am therefore unable to reproduce the problem. Did someone had the same issue ? Can someone explain to me how to execute my script in interactive mode so it runs as expected ?