Executable script

Hello,

Guys, I can make an executable .exe from my script that is in R? I saw it in some forums that you can do in others not. I did some tests, but without effectiveness.

Does anyone know how to do ??

Thank you!

Best regards.

Adding a Rscript shebang at the top of the script and changing the mode to rwx via chmod certainly gives an executable "feel". Someone else can perhaps offer better answers than I can.

For example, if you create a script called script.R containing:

#!/usr/local/bin/Rscript --vanilla

print(head(iris))

Then in a shell (on linux or Mac OS)

chmod 755 script.R

Then run the script

./script.R
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

Thanks for the answer. Sorry, but I didn't understand how an executable script can be. I don't know if I asked correctly, but I intend to make an .exe file that when when I double click on the file, it will run the script and apply the desired functions. In my case I would like to show the graph

This is the script:

#database
df <-structure (list (Latitude = c (-23.8, -23.8, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9, -23.9 , -23.9, -23.9, -23.9, -23.9, -23.9, -23.9), Longitude = c (-49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, -49.6, - 49.6, -49.7, -49.7, -49.7, -49.7, -49.7, -49.6, -49.6, -49.6, -49.6), Waste = c (526, 350, 526, 469, 285, 175, 175, 350, 350, 175 , 350, 175, 175, 364, 175, 175, 350, 45.5, 54.6)), class = "data.frame", row.names = c (NA, -19L))
Q1 <-matrix (quantile (df $ Waste, probs = 0.25))
df_Q1 <-subset (df, Waste> Q1 [1])
#cluster
d <-dist (df_Q1)
fit.average <-hclust (d, method = "average")
clusters <-cutree (fit.average, k = 4)
plot (clusters)

What operating system are you using?

I'm using Windows 10

Probably best to write an executable script that calls the R script. I don't use Windows so I can't offer a full solution how to do this, but in Mac OS, I create a .command file that I can double-click that calls to the Rscript.

Lets say I have a file called runme.command:

#!/usr/bin/env bash

/path/to/file.R

Where file.R has:

#!/usr/bin/env Rscript --vanilla

library(ggplot2)

p <- ggplot(iris, aes(Sepal.Length)) +
  geom_histogram()

ggsave('/path/to/figure.pdf', p)
system('open /path/to/figure.pdf')

When double-clicking the executable file, it runs the Rscript in a new shell and opens the newly generated plot using the default system PDF viewer.

1 Like

Hello, I managed to generate the pdf, however I double click on the executable, but it does not execute.

I did it as follows:

#file. R

library (ggplot2)
p <- ggplot (iris, aes (Sepal.Length)) + geom_histogram (bins = 10)
ggsave ('C: / Users / Jovani Souza / Documents / figure.pdf', p)
system ('open C: / Users / Jovani Souza / Documents / figure.pdf')

#teste.cmd

To create the executable I followed the following steps based in this site https://www.google.com/amp/s/khailiangtech.wordpress.com/2011/06/07/how-to-create-a-cmd-command-file/amp/ :

1 ° I executed the notepad

2 ° Insert the following function:

C: / Users / Jovani Souza / Documents / file.R

3 ° To save: file name: teste.cmd, save as type is All Files, and Encoding as ANSI

However, it did not execute. Is it correct that way to save the executable?

Thanks again.

Best Regards.

Any idea how to solve the problem?

I made File.R in my Documents folder containing this code

library (ggplot2)
p <- ggplot (iris, aes (Sepal.Length)) + geom_histogram (bins = 10)
ggsave ('C:/Users/fxcampos/Documents/figure.pdf', p)
system ('open C:/Users/fxcampos/Documents/figure.pdf')

I then made a file called ExecFile.bat, also in my documents folder containing this command.

"C:\Program Files\R\R-3.6.2\bin\Rscript.exe" C:\Users\fjcc\Documents\File.R

Double clicking on that file makes the PDF and opens it in my PDF viewer. Is that what you want?

It worked, thank you very much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.