... Hi, I am new in shiny and trying to print a vector of selected columns from an uploaded dataframe. Below is the code. From the selectInput, I can select multiple variables to be outputed in the data panel, and also return a vector of column names. So far, it gives me only a vector of variable one at the time. For the sake of simplicity, I used the mtcars dataset to print a vector of selected columns. So when I select mpg it prints [1] 2 and when I added disp it prints only [1] 4, even thought two variables were entered.
What I would like to do is to be able to print a list of all selected columns in one line, e.i. for the example above, to get a vector of [1] 2 4. Any help is appreciated
Thanks in advance
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("filedata", "Choose File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
uiOutput("y"),
),
mainPanel(
tabsetPanel( id="Tables",
tabPanel("Data",tableOutput("contents"))
)
)
)
)
server <- function(input, output) {
data <- reactive({
req(input$filedata)
inData <- input$filedata
if (is.null(inData)){ return(NULL) }
mydata <- read.csv(inData$datapath, header = TRUE, sep=",")
})
output$y <- renderUI({
selectInput("y", "Issue:", choices= names(data()),multiple=TRUE)
})
work = reactive({
req(input$filedata)
inData <- input$filedata
if (is.null(inData)){ return(NULL) }
df <- read.csv(inData$datapath, header = TRUE, sep=",")
req(data(),input$y)
vars =which(names(df) == input$y)
###Print a vector of selected variables##
print(vars)
return(df)
})
output$contents <- renderTable({
df = work()[input$y]
return(df)
})
}
shinyApp(ui, server)