Home > other >  Add columns to a dataframe based on the selection of inputs from widget
Add columns to a dataframe based on the selection of inputs from widget

Time:04-07

In the dataframe below I want when I select more values from the selectInput() more columns to be added in the dataframe like price,price2,price3 etc instead of having all selected values in one column with many rows.

library(shiny)
library(dplyr)
library(shinydashboard)
library(DT)

### user inter phase
ui <- fluidPage(
  
  ### App title ----
  
  
  titlePanel(title="SD")
  ,
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      ### Input files ----
      selectInput("Price","Select the price for analysis", c("List price","End consumer price without VAT", "End consumer price with VAT", "Reimbursed price"),multiple = T),     
       width = 2,
      
    ),
    
    ### Main panel for displaying outputs ----
    
    mainPanel(
      
      tabsetPanel(
        
        tabPanel("Export report", 
                 dataTableOutput("tab7"))
        
      )
      
    )
  )
)


#### Server 
server <- function(input, output, session) {
  
  output$tab7<-renderDataTable({
    Price<-input$Price
    df<-as.data.frame(list('price' = Price))
  })
  
  
  
}

shinyApp(ui = ui, server = server)

CodePudding user response:

You can replace your output$tab7 with this:

output$tab7<-renderDataTable({
  if(!is.null(input$Price)) {
    setNames(
      data.frame(t(input$Price)),
      paste0("price",seq_along(input$Price))
    )
  } 
})

shiny_price_screenshot

  • Related