(R Language) How to ask a user for data frame input?

Asked

Viewed 110 times

1

I’m doing a function that traverses data frames in search of non-numerical values. To do so, I have to ask the user to provide a data frame as input. Thus, I write:

data.frame <- readline(prompt = "Insira Nome do DataFrame: ")

However, after entering the name of the data frame, what is saved in "data.frame" is a string corresponding to what was typed. I did a search and saw that the readline() function does just that, but I couldn’t find one that did what I wanted, that is, receive and save the data in the data frame format. Does anyone have a suggestion? Perhaps it is important that I mention that data frames serving as input are loaded in the R Enviroment. I’ve got Tidyverse up and running.

1 answer

5


To get an R object from a string with its name, use the function get.
Note: I renamed the string to dataframe, without the point, since data.frame is already the name of a base R function.

dataframe <- readline(prompt = "Insira Nome do DataFrame: ")
#Insira Nome do DataFrame: iris

dataframe <- get(dataframe)
head(dataframe)
#  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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.