【问题标题】:R shiny: how to create a matrix with numericInput as elementsR闪亮:如何创建一个以numericInput为元素的矩阵
【发布时间】:2014-11-04 04:03:07
【问题描述】:

我想创建一个可以插入数值的矩阵。另外,我希望可见的行数取决于 actionButton。只要我的矩阵中有 NA,下面的代码就可以正常工作,但如果我用一些 numericInput 替换 NA,则不能。

这里是 ui.R:

shinyUI(
pageWithSidebar(
headerPanel("test"),
sidebarPanel(
actionButtonGreen("test","add a row")),
mainPanel(
uiOutput("value"))
)
) 

这里是服务器。R:

shinyServer(function(input,output){

observe({
if (input$test == 0) 
return()
isolate({ 
output$value <-renderTable( 
mdat <- matrix(NA, nrow = input$test, ncol = 2, byrow = TRUE) 
)
##If I change the NAs to a vector of numericInput, I obtain the following error
##Error: number of items to replace is not a multiple of replacement length 
##That's when I replace the NA in the above matrix with
##c(numericInput(inputId="1",label="",value="2"),
##  numericInput(inputId="2",label="",value="2"),
##  numericInput(inputId="3",label="",value="2"), 
##  numericInput(inputId="4",label="",value="2"))                    
})})

} )

任何建议将不胜感激。

干杯

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这是您尝试插入矩阵的内容(numericInput(inputId="1",label="",value="2") 的值):

    [1] "<label for=\"1\"></label>\n<input id=\"1\" type=\"number\" value=\"2\"/>"
    attr(,"html")
    [1] TRUE
    

    这是他的结构,它是一个包含 3 个元素的 2 个列表的列表:

    List of 2
     $ :List of 3
      ..$ name    : chr "label"
      ..$ attribs :List of 1
      .. ..$ for: chr "1"
      ..$ children:List of 1
      .. ..$ : chr ""
      ..- attr(*, "class")= chr "shiny.tag"
     $ :List of 3
      ..$ name    : chr "input"
      ..$ attribs :List of 3
      .. ..$ id   : chr "1"
      .. ..$ type : chr "number"
      .. ..$ value: chr "2"
      ..$ children: list()
      ..- attr(*, "class")= chr "shiny.tag"
     - attr(*, "class")= chr [1:2] "shiny.tag.list" "list"
    

    您的问题是函数numericInput 返回的东西不能适合您的矩阵。

    那么我建议你直接在数据框中使用 html 标签,并使用 sanitize.text.function 函数来评估 HTML 标签(而不是字符串)。

    shiny::runApp(list(
      ui = pageWithSidebar(
          headerPanel("test"),
          sidebarPanel(
            actionButton("test","add a row")),
          mainPanel(
            tableOutput("value"))
      ),   
      server = function(input,output){
        observe({
          if (input$test == 0) 
            return()
          isolate({
            output$value <-renderTable({
              num.inputs.col1 <- paste0("<input id='c1n", 1:input$test, "' class='shiny-bound-input' type='number' value='2'>")
              num.inputs.col2 <- paste0("<input id='c2n", 1:input$test, "' class='shiny-bound-input' type='number' value='2'>")
              data.frame(num.inputs.col1, num.inputs.col2)
            }, sanitize.text.function = function(x) x)
          })
        })
      }
    ))
    

    【讨论】:

    • 这太棒了:这非常有效。非常感谢。但现在有一个额外的问题:如何提取矩阵第一个单元格的值?我试图在服务器中插入 output$c
    • 通常你可以通过这种方式访问​​它们,但我想你没有注意到我 id 中的“n”。尝试使用input$c1n1 而不是input$c11,它应该可以工作:)
    • 确实如此!这是一个非常优雅的解决方案。非常感谢
    • 您好 Julien,如果您不介意的话,只是一个简单的问题:我在哪里可以找到所有课程的综合文档?我看到您使用了闪亮绑定输入,但我真的不知道这意味着什么以及其他可用的闪亮类? (我试图用谷歌搜索但没有成功)。提前致谢
    • 不,抱歉,我不知道在哪里可以找到此文档。我刚刚检查了一个闪亮的应用程序的源代码,并得到了 numericInput 元素的类。
    【解决方案2】:

    我编写这些函数来做同样的事情。希望这会有所帮助。

     columm    <- function(x,checkval) { if (is.na(checkval)) {
                                                          return(paste('<td>',x,'</td>', sep=""))
                                                          } else {
                                                          return(paste('<td style="background-color:lightblue;">',x,'</td>', sep=""))
                                                          }
                                }
    num_input <- function(id,checkval,default){  if (!is.na(checkval)) {
                                                          return(default) 
                                                          } else { 
                                                          return(paste("<input id='",id,"' type='number' value=",default, 
                                                                 " class='myInputstyle'>",sep="")) 
                                                          }
      }
    

    【讨论】:

    • 谢谢,太好了
    猜你喜欢
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    相关资源
    最近更新 更多