Vediamo come prendere input da tastiera con R. Con R è possibile importare dati da file SAS, SPSS, Excel, csv, database o qualsiasi file ASCII. Per ottenere un dato in maniera interattiva quindi avere un input da tastiera con R dobbiamo agire come segue:
# creiamo un dataframe da 0 eta <- c(18, 18, 30) genere <- c("male", "female", "male") altezza <- c(80, 100, 90) mydata <- data.frame(eta, genere, altezza) # inserire i dati usando l’editor mydata <- data.frame(eta=numeric(0), genere =character(0), altezza =numeric(0)) mydata <- edit(mydata)

edit(name = NULL, file = "", title = NULL, editor = getOption("editor"), …)
Guarda la documentazione di edit.
Se vogliamo leggere un solo valore possiamo allora
> readinteger <- function() + { + n <- readline(prompt="Inserisci un intero: ") + return(as.integer(n)) + } > > print(readinteger()) Inserisci un intero:
oppure
my.name <- readline(prompt="Nome: ") my.age <- readline(prompt="Età: ") # conversion character in integer my.age <- as.integer(my.age) print(paste("Ciao,", my.name, "il prossiamo anno avrai", my.age+1, "anni old."))
Guarda la documentazione di readline.
Guarda tutti i tutorial di Data input oppure torna su R tutorial.
Commenti recenti