Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading