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 ?
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)
}
))