Insert a numeric input for each row - R Shiny

余生颓废 提交于 2019-12-20 03:14:49

问题


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 ?


回答1:


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)

  }
))


来源:https://stackoverflow.com/questions/22221089/insert-a-numeric-input-for-each-row-r-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!