user3443063
user3443063

Reputation: 1615

R shinyapps plot according to choice in SelectInput

in Shinyapp I have a selectInput where I can choose some values. Then I want to plot y~selected value. I can plot a defined plot like plot(mtcars$mpg~mtcars$wt) but I wannt to plot plot(mtcars$mpg~selected value)

Can anybody help me. My code is this:

 library(shiny)

 ui <- fluidPage(   
 titlePanel("MyPLot"),   
    sidebarLayout(
       sidebarPanel(
         selectInput("variable", "Variable:", c("Cylinders" = "cyl", "Transmission" = "am", "Gears" = "gear"))
          ),

  mainPanel(
    plotOutput("distPlot"),
    plotOutput("secPlot")
       )
    )
 )

 server <- function(input, output) {
   output$distPlot <- renderPlot({plot(mtcars$mpg~mtcars$wt) })  
   output$secPlot <- renderPlot({ plot(mtcars$mpg~input$variable)   })
 }

 shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 827

Answers (1)

MLavoie
MLavoie

Reputation: 9836

Maybe you could create a reactive data frame where you can subset the mtcars and then use renderPlot:

server <- function(input, output) {
  output$distPlot <- renderPlot({plot(mtcars$mpg~mtcars$wt) })  

  df <- reactive({ 
    df <- mtcars %>% select(mpg, input$variable)
  })

  output$secPlot <- renderPlot({ 
    dfb <- df()
    plot(dfb[, 1]~ dfb[, 2])   
    })
}

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions