'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 1:[1]
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))
)
}
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | langtang |

