0
votes

I have a problem about the Radiobuttons in my small shiny app. Using the database of mtcars as the example, I want to plot "mpg" as a function of the other variables (here just two of cyl and disp) which could be selected.

My code is:

## Only run examples in interactive R sessions
if (interactive()) {

ui <- fluidPage(
   radioButtons("dist", label = "Distribution type:",
             choices = list ("cyl" = "cyl",
               "disp" = "disp")
               ),
plotOutput("distPlot")
)

server <- function(input, output) {
  output$distPlot <- renderPlot({

  #dist <- switch(input$dist,
  #               cyldata1 = cyl,
  #               dispdata1 = disp
  #               )
  var<- input$dist
   par(mar=c(1,1,1,1))
  plot(mtcars$mpg,mtcars$var)
})
}

shinyApp(ui, server)
}

But the problem is that the plot will not change accordingly when I select two different variables of cyl and disp. It means that the plot has not reacted to my selection.

1
Replace plot(mtcars$mpg,mtcars$var) with plot(mtcars$mpg,mtcars[[var]])Sri
@Sri It works. Thank you very much. YanYan Li
Accept this as AnswerSri

1 Answers

0
votes

Replace plot(mtcars$mpg,mtcars$var) with plot(mtcars$mpg,mtcars[[var]])