Using selectInput to get a vector of multiple column names in reactive env.

... 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)

I'm not 100% sure what you're trying to do but if you're just trying to print 'vars', it should be done inside of an observer instead of a reactive. observe and observeEvent are used for side effects which include printing to the console.

I think this should work:

observe({
     req(data(),input$y, work())
     vars <- which(names(work()) == input$y)
     ###Print a vector of selected variables##
     print(vars)
   })

Thank you so much DaveRosenman for the quick reply. Indeed I did not provide the whole picture as I am working on it, so I have more steps to get into. Basically, by printing (vars), I was trying to figurate out if my first code was working as needed. What I want to do is to get column positions (of selected variables) from uploaded dataframe in a vector setting, so I can use them for plots, regression...Hope this make it much clearer. Thanks

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.