Create Matrix by using R program get a input from the user or run time

I am the beginner of R language.I want to create a matrix by using get a input from the user
for example

readinteger <- function(x,y)
{ 
  x <- readline(prompt="Enter an integer x: ")
  y <- readline(prompt="Enter an integer y: ")
  
  return(as.integer(c(x,y)))
}
print (readinteger ())
m <- matrix (1:100 ,nrow = x, ncol = y)
print(m)

Can you please give me some valuable suggestions

Here's an example to get you started, keep in mind that the data in the matrix is somewhat defined by its dimensions which is why I've told it to create a matrix of all NA.

matrix_maker <- function() {
    
    x <- as.integer(readline("Number of rows: "))
    y <- as.integer(readline("Number of cols: "))
    
    matrix(data = NA,
           nrow = x,
           ncol = y)
}

matrix_maker()

This will prompt the user to input the number of rows and columns in the console of the new matrix.

2 Likes

A few references that might help explain some of the differences between the code you started with and @ttrodrigz's example:

Using (or not using) print():

Required arguments in functions and using (or not using) return()

How R assigns values to variables

3 Likes

very thanks for your reply me answer....

very thanks for your reply me question and answer....