E50M
E50M

Reputation: 87

dynamically add inputs to vector in shiny which can be used to create dynamic text report

Hi I am trying to make a shiny app which will:

Accept several inputs, which are used to create a text report.

I am trying to collect the values of the inputs into a vector, then collapse the vector using paste, to create a text document.

I would like the document to be responsive to the inputs.

The value of input$indication will always be first,
Followed by whichever multiple choice input$sections have been selected.

Here is the app which seems not to be working very well.

Thanks very much,

library(shiny)


line_double <- paste(rep("═", 54), collapse = "")
line_single <- paste(rep("─", 54), collapse = "")
line_short_dash <- paste(rep("-", 125), collapse = "")
bullet <- "•"
r_arrow <- "→"



ui <- fluidPage(
  
  titlePanel("test app"), 
  
  sidebarLayout(
    
    
    sidebarPanel(
      
      checkboxGroupInput(inputId = "sections", 
                         label = "Select Text Sections", 
                         choices = list(
                           "favorite pets", "favorite colors", "favorite cars"
                         ))
      
    ),
    
    mainPanel(
      
      tabsetPanel(
        
        tabPanel("Inputs",   
                 textAreaInput("indication", 
                               "Tell us about yourself", 
                               width = "600px", 
                               height = "70px", 
                               value = "")
        ),
        
        tabPanel("Report",
                 htmlOutput("text", width = '50px')
        )
      )
      
    )
  )
  
)



server <- function(input, output, session) {
  
  text_df <- reactive({
    
    d <- data.frame(text = c(""))
    
    # the indication must always go in the first spot (top of the report)
    d$text[1] <- paste(line_double,
                       input$indication,
                       line_double,
                       sep = "</br>")
    
    
    for (i in 1:length(input$sections)){
      
      d$text[1+i] <- paste(line_double, 
                           input$sections[i], 
                           line_double, 
                           sep = "</br>")
      
    }
    
    
    return(d)
    
  })
  
  
  
  
  output$text  <- renderUI({
    
    
    HTML(
      paste(
        
        text_df()$text, 
        collapse = "</br></br>"

        
      )
    )
    
    
    
  })
  
}
shinyApp(ui, server)






Can anyone help

Upvotes: 1

Views: 123

Answers (1)

Matt Cowgill
Matt Cowgill

Reputation: 679

How about something like this?

library(shiny)


line_double <- paste(rep("═", 54), collapse = "")
line_single <- paste(rep("─", 54), collapse = "")
line_short_dash <- paste(rep("-", 125), collapse = "")
bullet <- "•"
r_arrow <- "→"



ui <- fluidPage(
  
  titlePanel("test app"), 
  
  sidebarLayout(
    
    
    sidebarPanel(
      
      checkboxGroupInput(inputId = "sections", 
                         label = "Select Text Sections", 
                         choices = list(
                           "favorite pets", "favorite colors", "favorite cars"
                         ))
      
    ),
    
    mainPanel(
      
      tabsetPanel(
        
        tabPanel("Inputs",   
                 textAreaInput("indication", 
                               "Tell us about yourself", 
                               width = "600px", 
                               height = "70px", 
                               value = "")
        ),
        
        tabPanel("Report",
                 htmlOutput("text", width = '50px')
        )
      )
      
    )
  )
  
)



server <- function(input, output, session) {
  
  text_df <- reactive({
    
    d <- data.frame(text = c(""))
    
    # the indication must always go in the first spot (top of the report)
    text_indication <- paste(line_double,
                    input$indication,
                    line_double,
                    sep = "</br>")
    
    text_sections <- paste(line_double, 
                           input$sections, 
                           line_double, 
                           sep = "</br>")
      
    d$text <- paste(text_indication, 
                    text_sections, 
                    collapse = "</br>")
    
    
    return(d)
    
  })
  
  
  
  output$text  <- renderUI({
   
    
    HTML(
      paste(
        
        text_df()$text, 
        collapse = "</br></br>"
        
        
      )
    )
    
    
    
  })
  
}
shinyApp(ui, server)

Upvotes: 1

Related Questions