I want to make a autorunable .BAT of some data that compares dates:
datos <- read.table(text = '
fecha edad
01/05/2020 20
02/01/2019 33
03/05/2017 19
04/05/2018 3
09/05/2020 25
', header = TRUE, stringsAsFactors = FALSE)
datos$fecha <- as.Date(datos$fecha, format="%d/%m/%Y")
fecha_referencia <- as.Date("25/11/2019", format="%d/%m/%Y")
datos$fecha < fecha_referencia
The reference_date may change. You would have to edit the file depending on the date before running the file.
Is there a way that when executing the file it asks the user for the date through a message on the screen?
R has its particularities when it comes to processing keyboard input, there is a behavior when the script is executed interactively, from the R or Rstudio ide and another when it is automated in a command line call.
For example, to request a date and validate that it can actually be transformed into a date, you could implement something like this:
As you can see, different functions are used depending on how the Script is invoked,
readline()
in the IDE andreadLines()
from the command line. To try it out, consider:source
Rscript.exe script.r
Sure, you can ask:
After that you will have a %date% variable available that you will have to validate to ensure that the user puts correct data.
One option would be to use
readline
within the script:You can set a test to make sure they enter the date correctly: