【问题标题】:How to concatenate a list of strings in shiny?如何连接闪亮的字符串列表?
【发布时间】:2020-08-12 16:29:32
【问题描述】:

我想根据用户输入生成一个字符串列表。但是当我使用粘贴功能时,我收到了这个错误: 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)

【问题讨论】:

    标签: r shiny concatenation


    【解决方案1】:

    ab 内的变量赋值 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)
    

    如果您希望ab 可用于其他功能,则应使用reactive values

    【讨论】:

    • 有没有办法用单独的行显示文本? /n 只是打印出来。相反,我想在单独的行中查看输出。我试过writeLines,但它打印在控制台而不是应用程序中。
    • 你试过用 \n 代替 /n 吗?
    猜你喜欢
    • 2014-08-30
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2018-05-15
    相关资源
    最近更新 更多