代码之家  ›  专栏  ›  技术社区  ›  TarJae

如何使用paste0与输入$in闪亮

  •  0
  • TarJae  · 技术社区  · 2 年前

    这是一个后续问题 Avoid DRY with 13 sliderInputs and 13 textInputs :

    如何缩短此代码:

      sliderValues <- reactive({
        
        data.frame(
          Name = c("A",
                   "B",   
                   "C"),
          Value = as.character(c(input$a,
                                 input$b,
                                 input$c
                                )),
          stringsAsFactors = FALSE)
      })
    

    enter image description here

    我尝试过:

      # Reactive expression to create data frame of all input values ----
      sliderValues <- reactive({
        
        data.frame(
          Name = c(LETTERS[1:3]),
          Value = paste0("input$", letters[1:3]),
          stringsAsFactors = FALSE)
      })
    

    enter image description here

    1 回复  |  直到 2 年前
        1
  •  3
  •   stefan    2 年前

    一种选择是使用 sapply

    # Reactive expression to create data frame of all input values ----
      sliderValues <- reactive({
        
        data.frame(
          Name = c("A",
                   "B",
                   "C"),
          Value = as.character(sapply(letters[1:3], function(x) input[[x]])),
          stringsAsFactors = FALSE)
        
      })