首页 > 解决方案 > 如何连接闪亮的字符串列表?

问题描述

我想根据用户输入生成一个字符串列表。但是当我使用粘贴功能时,我收到了这个错误: object 'b' not found

我正在寻找一种方法来返回所有要在服务器中显示或进一步使用的输入值——

例如:

LV =~ x1 + x2 + x3
LV ~ LV1

等等。

也许我需要将用户输入的值存储为反应值。但我不确定该怎么做。

这是完整的代码:

library(shiny)

newlist <- as.list(c("LV", "LV2", "x1", "x2", "x3", "x4", "x5", "x6"))

lvar <- function(...) {
    params <- list(...)
    stopifnot(length(params)%%2==0)
    lefts = params[seq(1,length(params), by=2)]
    rights = params[seq(2,length(params), by=2)]
    rights <- Map(paste, rights, collapse="+")
    paste(paste0(lefts, " =~", rights), collapse="\n")
}

regs <- function(...) {
    params <- list(...)
    stopifnot(length(params)%%2==0)
    lefts = params[seq(1,length(params), by=2)]
    rights = params[seq(2,length(params), by=2)]
    rights <- Map(paste, rights, collapse="+")
    paste(paste0(lefts, "~", rights), collapse="\n")
}


ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
            selectInput("variable1", "Variable:", choices = c(' ', newlist), selected = NULL),
            selectInput("variable2", "Variable:", choices = c(' ', newlist), selected = NULL, multiple = TRUE),
            selectInput("variable3", "Variable:", choices = c(' ', newlist), selected = NULL),
            selectInput("variable4", "Variable:", choices = c(' ', newlist), selected = NULL, multiple = TRUE)
        ),

        mainPanel(
            verbatimTextOutput("listout1"),
            verbatimTextOutput("listout2"),
            verbatimTextOutput("listout3"),
            verbatimTextOutput("listout4"),
            verbatimTextOutput("listout5")
        )
    )
)

server <- function(input, output) {
    
    output$listout1 <- renderText({
        input$variable1
        })
    
    output$listout2 <- renderText({
        input$variable2
        })
    
    output$listout3 <- renderText({
         a <- lvar(lefts = input$variable1, rights = input$variable2)
         })
    
    output$listout4 <- renderText({
         b <- regs(lefts = input$variable3, rights = input$variable4)
         })
    
    output$listout5 <- renderText({
         paste(a, b, sep = "/n")
         })
}
shinyApp(ui = ui, server = server)

标签: rshinyconcatenation

解决方案


a并且b内部的变量分配renderText不适用于服务器中的其他功能。
尝试:

library(shiny)

newlist <- as.list(c("LV", "LV2", "x1", "x2", "x3", "x4", "x5", "x6"))

lvar <- function(...) {
  params <- list(...)
  stopifnot(length(params)%%2==0)
  lefts = params[seq(1,length(params), by=2)]
  rights = params[seq(2,length(params), by=2)]
  rights <- Map(paste, rights, collapse="+")
  paste(paste0(lefts, " =~", rights), collapse="\n")
}

regs <- function(...) {
  params <- list(...)
  stopifnot(length(params)%%2==0)
  lefts = params[seq(1,length(params), by=2)]
  rights = params[seq(2,length(params), by=2)]
  rights <- Map(paste, rights, collapse="+")
  paste(paste0(lefts, "~", rights), collapse="\n")
}


ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      selectInput("variable1", "Variable:", choices = c(' ', newlist), selected = NULL),
      selectInput("variable2", "Variable:", choices = c(' ', newlist), selected = NULL, multiple = TRUE),
      selectInput("variable3", "Variable:", choices = c(' ', newlist), selected = NULL),
      selectInput("variable4", "Variable:", choices = c(' ', newlist), selected = NULL, multiple = TRUE)
    ),
    
    mainPanel(
      verbatimTextOutput("listout1"),
      verbatimTextOutput("listout2"),
      verbatimTextOutput("listout3"),
      verbatimTextOutput("listout4"),
      verbatimTextOutput("listout5")
    )
  )
)

server <- function(input, output) {
  
  output$listout1 <- renderText({
    input$variable1
  })
  
  output$listout2 <- renderText({
    input$variable2
  })
  
  output$listout3 <- renderText({
    #a <- 
    lvar(lefts = input$variable1, rights = input$variable2)
  })
  
  output$listout4 <- renderText({
    #b <-
    regs(lefts = input$variable3, rights = input$variable4)
  })
  
  output$listout5 <- renderText({
    paste(lvar(lefts = input$variable1, rights = input$variable2), regs(lefts = input$variable3, rights = input$variable4), sep = "\n") # changed /n to \n
  })
}
shinyApp(ui = ui, server = server)

如果您想要a并且b可以使用其他功能,您应该使用响应式值


推荐阅读