2

I have a complex code that generates a big matrix, but here I attach a simple, reproducible example in order to explain clearly what I want: Here's the code:

# ui.R

library(shiny)
shinyUI(
  mainPanel("Table Output",
           (tableOutput("My_table")))
        )   

# server.R

library(shiny)
 shinyServer(function(input, output, session) {
  My_table = matrix( 
   c(1:100), 
     nrow=20, 
     ncol=5)
  output$My_table <- renderTable(My_table)
 })

My goal is to put on the left and right sides of the table a small textbox, one for each row of the table. In my idea I need to be able to write a number inside the left textbox, eventually also on the right textbox and then do some calculations on the row, based on the number manually inserted. Of course the length of the table will vary therefore I need to use something like dim(My_table) in order to properly put one small textbox for each row, one on the left side and one on the right. I thought to use R shiny's numericInput function but I can't get a clue on how to apply in this scenario.

Can I do this only with R Shiny functions or am I forced to use an html ui.R ?

Axeman
  • 32,068
  • 8
  • 81
  • 94
GrilloRob
  • 262
  • 1
  • 3
  • 15

1 Answers1

2

You can bind your matrix with two vectors of strings of the HTML tags for numeric inputs (input1 and input2 in my code bellow), and add the sanitize.text.function to evaluate the HTML tags as is (and not as strings).

For example :

shiny::runApp(list(
  ui = basicPage(
    tableOutput("My_table")
  ),
  server = function(input, output, session) {

    My_table = matrix( 
      c(1:100), 
      nrow=20, 
      ncol=5)

    output$My_table <- renderTable({
      input1 <- paste0("<input id='a", 1:nrow(My_table), "' class='shiny-bound-input' type='number' style='width: 50px;'>")
      input2 <- paste0("<input id='b", 1:nrow(My_table), "' class='shiny-bound-input' type='number' style='width: 50px;'>")
      cbind(input1, My_table, input2)
    }, sanitize.text.function = function(x) x)

  }
))
Julien Navarre
  • 7,653
  • 3
  • 42
  • 69
  • 2
    Thanks for your support, appreciated, the code will do exactly what I wanted. Now suppose I have to do, for each row and after clicked a button, a calculation like (input1 * Variable1) + (input2* Variable2); do I have to calculate first and then put the result in the output$My_table, or can I do that all in one step (inside the output$My_table) ? – GrilloRob Mar 06 '14 at 11:36
  • Really good question, but I rly don't know how to do it in this case, because the input are created at the same time as the ouput so it's prety complicated to catch them without regenerating the output (isolate dont works here). I can just suggest you to create a new output table for example : `output$sum <- renderTable({ row.sum <- 1:nrow(My_table) for(i in 1:nrow(My_table)){ row.sum[i] <- input[[sprintf("a%d", i)]] + input[[sprintf("b%d", i)]] + sum(My_table[i,]) } data.frame(row.sum) })` – Julien Navarre Mar 06 '14 at 13:47
  • How do I save the values entered in 'input1' and 'input2' fields? Can I save the entire table "My_table" with the new values in each of the 'input1' and 'input2' fields into a data frame? – Jay Aug 01 '17 at 17:53