Convert CSV file to matrix

Hi, I'm quite new to R and Shiny and I'm trying to figure out how I can put the data of a CSV file into a matrix.

What I have now is this:

library(shiny)

# Define UI
ui <- fluidPage(
	titlePanel("Insole Data Analyzer"),
	
    fluidRow(
        column(width = 6,
               dataTableOutput('mytable1'),
               
               fileInput('file1', 'Left foot data',
                         accept = c(
                             'text/csv',
                             'text/comma-separated-values',
                             'text/tab-separated-values',
                             'text/plain',
                             '.csv',
                             '.tsv'
                         )
               )
        ),
        column(width = 6,
               dataTableOutput('mytable2'),
               
               fileInput('file2', 'Right foot data',
                         accept = c(
                             'text/csv',
                             'text/comma-separated-values',
                             'text/tab-separated-values',
                             'text/plain',
                             '.csv',
                             '.tsv'
                         )
               )
        )
    ),
	
	fluidRow(
		column(width = 6,
			tableOutput("tb1")
		),
		column(width = 6,
			tableOutput("tb2")
		)
	)	
)


# Define Server
server <- function(input,output){
  output$tb1 <-renderTable({
    data1 <- input$file1
    if(is.null(data1)){return()} 
    read.table(data1$datapath,sep="")
  }) 
  output$tb2 <-renderTable({
    data2 <- input$file2
    if(is.null(data2)){return()} 
    read.table(data2$datapath,sep="")
  }) 
}

# Run Shiny app ----
shinyApp(ui = ui, server = server)

This gives me two "browse file" buttons to insert my CSV files and it immediately plots the table.

Instead of this, I want to use the data of the table to do some calculations on.

How can I create a matrix of this table and loop over each row to check this data and alter the matrix?

Here is an example where I simply multiply all of the elements of the file by a constant. Is that enough to get you started? I changed the parameters of read.table to accmodate files I happen to have.

library(shiny)

# Define UI
ui <- fluidPage(
  titlePanel("Insole Data Analyzer"),
  
  fluidRow(
    column(width = 6,
           dataTableOutput('mytable1'),
           
           fileInput('file1', 'Left foot data',
                     accept = c(
                       'text/csv',
                       'text/comma-separated-values',
                       'text/tab-separated-values',
                       'text/plain',
                       '.csv',
                       '.tsv'
                     )
           )
    ),
    column(width = 6,
           dataTableOutput('mytable2'),
           
           fileInput('file2', 'Right foot data',
                     accept = c(
                       'text/csv',
                       'text/comma-separated-values',
                       'text/tab-separated-values',
                       'text/plain',
                       '.csv',
                       '.tsv'
                     )
           )
    )
  ),
  
  fluidRow(
    column(width = 6,
           tableOutput("tb1")
    ),
    column(width = 6,
           tableOutput("tb2")
    )
  )	
)


# Define Server
server <- function(input,output){
  Tbl1 <- eventReactive(input$file1, {
    if(is.null(input$file1)){return()} 
    Mat <- read.table(input$file1$datapath,sep=",", header = TRUE)
    Mat <- as.matrix(Mat)
    Mat * 10
  })
  Tbl2 <- eventReactive(input$file2, {
    if(is.null(input$file2)){return()} 
    Mat <- read.table(input$file2$datapath,sep=",", header = TRUE)
    Mat <- as.matrix(Mat)
    Mat * 100
  })
  output$tb1 <-renderTable({
    Tbl1()
  }) 
  output$tb2 <-renderTable({
    Tbl2()
  }) 
}

# Run Shiny app ----
shinyApp(ui = ui, server = server)

Thanks for your reply! I tried your code and I get a "non-numeric argument to binary operator" error... Besides that, I need to check every line of the matrix, like looping over it and checking the values in a row, deleting multiple rows, etc. Is that also possible this way?

I would also be glad to read a documentation if it contains this information, but I haven't found a good one yet...

You are probably getting that error because your matrix contains text values. A matrix must be all text or all numeric, it cannot be a mixture. If you have a single text entry in the matrix, the entire matrix will be converted to text.
Let's take a step back and work on the data processing outside of Shiny. It is best to have the data processing and visualization worked out before trying to implement anything in Shiny because the reactive environment in Shiny makes debugging difficult.
Please give some details about what your data look like and the specific changes you want to make in the data. You may find helpful the link below about Reproducible Examples. Try to keep the example as simple as it can be and still be useful.
Are you sure you want your data in a matrix? The typical container for data is a data frame, though a matrix can be a good choice under the right circumstances.
I have also attached a link to the book R for Data Science. That is a good source for learning about general data processing and display.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.