Search code examples
ruser-interfaceexecutablerscript

r script fail to executate


I'm trying to create a shortcut that when I click on, the r script run automatically. I used instructions posted in https://www.r-bloggers.com/2015/02/making-r-files-executable-under-windows/ by Peter Meissner and the example work fine but my script not. Why my code doesn't work?

Here a piece of my code.

library(readxl)
library(dplyr)

accentless <- function( x ) {
  chartr(
    "áéóūáéíóúÁÉÍÓÚýÝàèìòùÀÈÌÒÙâêîôûÂÊÎÔÛãõÃÕñÑäëïöüÄËÏÖÜÿçÇ",
    "aeouaeiouAEIOUyYaeiouAEIOUaeiouAEIOUaoAOnNaeiouAEIOUycC",
    x );
}

my_tempdir <- tempdir()

setwd(my_tempdir)

if(!file.exists("./data")){dir.create("./data")}

planilha <- select.list(c("VIDRARIA e DESCARTÁVEIS", "LIMPEZA e ESCRITÓRIO", "E.P.I."), 
        title = ("Qual material deseja pesquisar?"))

In this point the script stop.

Thanks in advance to elucidate the problem.


Solution

  • You cannot use select.list() when running R non-interactively.

    If this is the only point in your code where user input is required, you can have your shell ask the user for input, and then pass that input to R for you.

    How you do that will depend on your operating system:

    For MacOS or Linux you would write a shell script (.sh file):

    #!/bin/bash
    echo -e "1. VIDRARIA e DESCARTÁVEIS\n2. LIMPEZA e ESCRITÓRIO\n3. E.P.I."
    read -p "Qual material deseja pesquisar? (Numero 1-3): " choice
    Rscript /path/to/your/script.R $choice
    

    (Note - on MacOS you may or may not need to change /bin/bash to /bin/tcsh)

    For Windows, you would write a batch script (.bat file) that looks something like:

    ECHO 1. VIDRARIA e DESCARTÁVEIS
    ECHO 2. LIMPEZA e ESCRITÓRIO
    ECHO 3. E.P.I.
    set /p choice=Qual material deseja pesquisar? (Numero 1-3)
    RScript c:\path\to\your\script.R %choice
    

    (Caveat: This code is not tested, I am not on a Windows computer right now.)

    The thing you would click on to run would be your .sh/.bat file, which will get the necessary input from the user and then start your R code for you. Regardless of your operating system, the location of the RScript command (which lets you run R scripts via the command line) must be in your PATH variable for this to work.

    You would then need to set up your R script to be able to accept the argument that's passed to it on the command line:

    args <- commandArgs(trailingOnly = TRUE)
    numero <- as.numeric(args[[1]])
    planilha <- c('VIDRARIA e DESCARTÁVEIS','LIMPEZA e ESCRITÓRIO','E.P.I.')[numero]
    

    This will work if your R script only needs user interaction for this first choice. If there is more user input needed after this, you probably cannot use this approach to running R, and may benefit from learning shell scripting instead. If you're on MacOS or Linux, bash scripting is great and you'll soon learn to intermix using bash and R as needed. If you're on Windows, DOS batch scripts are much more limited, and your best option is probably to install Cygwin so you can also use bash.