writer_typer
writer_typer

Reputation: 798

How to concatenate a list of strings in shiny?

I would like to generate a list of strings based on user input. But when I use the paste function, I'm getting this error: object 'b' not found

I'm looking for a way to return all the input values to be displayed or used further in the server as --

for example:

LV =~ x1 + x2 + x3
LV ~ LV1

and so on.

Maybe I need to store the user inputted values as reactive values. But I'm unsure as to how to do it.

Here's the full code:

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)

Upvotes: 0

Views: 476

Answers (1)

Waldi
Waldi

Reputation: 41260

a and b variables assignements inside renderText aren't available for other functions in the server.
Try:

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)

If you want a and b to be available for other functions, you should use reactive values.

Upvotes: 1

Related Questions