Mark R
Mark R

Reputation: 1021

Select dataframe in Shiny

I've had some luck with Shiny and R, but I can't get an selectInput function to change the dataframe. I'm probably missing something obvious, but here's my code

require(shiny)

A <- data.frame(x=c(1,2,3),y=c(3,2,1))
B <- data.frame(x=c(1,1,5),y=c(3,5,0))

ui <- fluidPage(

selectInput("df", "Select dataframe", choices = c('A'='A','B'='B'), selected = 'A'),

plotOutput("Plot")

)

server <- function(input, output)
{

  df <- reactive({
 x <- as.data.frame(input$df)
  })

  output$Plot <- renderPlot({
  df <- df()
  plot(x=df$x, y=df$y)

})
}

shinyApp(ui = ui, server = server)

What am I missing?

Upvotes: 2

Views: 2883

Answers (1)

Batanichek
Batanichek

Reputation: 7871

You cant use as.data.frame and name of df

try to use get

A <- data.frame(x=c(1,2,3),y=c(3,2,1))
B <- data.frame(x=c(1,1,5),y=c(3,5,0))

shinyServer(function(input, output) {

  df <- reactive({
    x <- get(input$df)
  })

  output$Plot <- renderPlot({
    df <- df()
    plot(x=df$x, y=df$y)

  })
})

Upvotes: 4

Related Questions