0

I want to use the input to select the variable in the dataframe and unique it to get a vector.

But it's not work.

library(shiny)

ui <- fluidPage(
    
    sidebarLayout(
        sidebarPanel(
            selectInput("variable", "Variable:",colnames(iris))
        ),
        
        mainPanel(
            textOutput("table")
        )
    )
)

server <- function(input, output) {
    
    output$table <- renderText({
        
        a <- unique(iris$input$variable])
        
        
    })
}

# Run the application 
shinyApp(ui = ui, server = server)
leewan
  • 93
  • 7

1 Answers1

1

You can use [[ to subset the dataframe based on user input.

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("variable", "Variable:",colnames(iris))
    ),
    mainPanel(
      textOutput("table")
    )
  )
)

server <- function(input, output) {
  output$table <- renderText({
    a <- unique(iris[[input$variable]])
  })
}

# Run the application 
shinyApp(ui = ui, server = server)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213