Monelisa
Monelisa

Reputation: 95

Passing SelectInput as variable name to ggplot graph on ShinyR

This is a simplified version of the problem I have. I want the SelectInput to allow selection of different columns, which will then be the y axis on the plot.I have used the aes_string function as suggested in similar question but it did not work. Thankful for help

df <-data.frame(name = c("john", "mary"), age = c(9, 10), pets =  c(20, 30))
library(ggplot2)

ui <- fluidPage(
    
    # Application title
    titlePanel("name and number of pets"),
    
    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            box(width = 15,
                selectInput(inputId = "select_variable", label = "Selecione a variável para consulta", 
                            choices = c("age", "pets"), 
                            selected = 1))
        ),
        
        # Show a plot of the generated distribution
        mainPanel(
            plotOutput(outputId = "a")
        )
    )
)

server <- function(input, output) {
    
    output$a <- renderPlot({
        
        
        ggplot(df)+
            geom_bar(mapping = aes_string(x="name", y = input$select_variable), stat = "identity")
        
    })
}

shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 200

Answers (1)

Limey
Limey

Reputation: 12461

You're trying to mix shinydashboard (box) with standard shiny without loading the shinydashboardpackage. Hence, the call to box tries to run graphics::box(). [It would have been helpful to provide the error message rather than simply saying "It did not work".]

The solution is either to delete the call to box or to load shinydashboard.

Upvotes: 1

Related Questions