R shiny:如何在server.r中创建一个列表,其元素是由数字n索引的字符串?

use*_*694 1 r shiny

我需要在我的server.r这个类型的代码中的一行(基本上在服务器中我将安排来自ui.r的所有输入在列表中):

x<- list(input$field1,input$field2,input$field3, etc... until n=100)
x<- x[!= is.null(x)]
h4(x) 
Run Code Online (Sandbox Code Playgroud)

代码在renderUI({})中使用.当我手动编写它时,它工作正常.但肯定有一种方法可以使用cat and paste(或其他函数)来更简洁地编写它.

使用以下内容不起作用,我不明白为什么:

x <- cat(paste("input$field", 1:100,",", sep = ""))
list(x)
Run Code Online (Sandbox Code Playgroud)

任何帮助/建议将受到高度赞赏

ps:我需要这个,因为我的输入是根据一个按钮生成的,因此可能是没有创建具有较大Id的字段,例如field99,我需要测试哪些字段已经创建.

干杯

har*_*mug 6

要将所有输入变量作为列表,请尝试:

x <- reactiveValuesToList(input)
Run Code Online (Sandbox Code Playgroud)

要么,

x <- lapply(1:3, function(z) input[[paste0("field", z)]])
Run Code Online (Sandbox Code Playgroud)

简单演示:将三个用户提供的无功输入作为列表汇总

server.R(EDITED使用第二种方法)

library(shiny)

shinyServer(function(input, output) { 
  output$restable <- renderTable({
    mylist <- lapply(1:3, function(z) input[[paste0("slide", z)]])
    data.frame(Names=c("Slider 1", "Slider 2", "Slider 3", "Sum"),
               Values=c(do.call("c", mylist), do.call("sum", mylist)))
#Values=c(do.call("c", reactiveValuesToList(input)), do.call("sum", reactiveValuesToList(input))))
  })
})
Run Code Online (Sandbox Code Playgroud)

ui.R

library(shiny)

# Define UI for application
shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Sliders should sum to 100!"),

  # Sidebar with 3 slider inputs
  sidebarPanel(
   sliderInput("slide1", "Slider 1: ", min = 0, max = 100, value=40, step=1),
   sliderInput("slide2", "Slider 2: ", min = 0, max = 100, value = 30, step=1),
   sliderInput("slide3", "Slider 3: ", min = 0, max = 100, value = 30, step=1)
  ),

  # Create table output
  mainPanel(
    tableOutput("restable")
  )
))
Run Code Online (Sandbox Code Playgroud)