Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle through objects (as tables) using R shiny

Tags:

r

shiny

I'm trying to make my first shiny app and failing miserably. I want to be able to toggle through 3 different dataframes (and present them as tables). The idea is that I render each table (A, B, C) in the main panel by selecting them in the sidebar panel.

I am part of the way there, but struggling at the "output" stage...

library(shiny)
library(dplyr)

A <- data.frame(ID = c("A", "B", "C","D"),
                      colour = c("red", "white", "blue", "orange"),
                      age =c("8", "3", "4", "5"))

B <- data.frame(ID = c("Bob", "Bill", "Jack","Dave"),
                shape = c("star", "square", "circle", "triangle"),
                age =c("15", "12", "13", "14"))

C <- data.frame(ID = c("Jane", "Jenny", "Bex","Sarah"),
                animal = c("cat", "dog", "fish", "tiger"),
                food =c("chips", "burger", "sushi", "chocolate"))

ui <- fluidPage(
  titlePanel("Failing at my first shiny app"),
  sidebarLayout(
    sidebarPanel(
      selectInput("tableInput", "Table",
                  choices = c("A", "B", "C"))
    ),
    mainPanel(
      tableOutput("results")
    )
  )
)

server <- function(input, output) {
  
  output$results <- renderTable({
    #want to toggle through objects here
    #how do I do it?!
  })
}

shinyApp(ui = ui, server = server)

If anyone can point me in the right direction, I'd be ever grateful! Thanks!

like image 763
lecb Avatar asked Mar 11 '26 21:03

lecb


1 Answers

Inside the renderTable, get the value of the object

get(input$tableInput)

i.e. input$tableInput returns the objects names as 'A', 'B', 'or 'C' as per the user selection. input is the argument to the function and tableInput is the user defined name for the Table input choice list that store the 'A', 'B', 'C' options

get returns the value of the object i.e. a data.frame

-full code

library(shiny)
library(dplyr)

A <- data.frame(ID = c("A", "B", "C","D"),
                colour = c("red", "white", "blue", "orange"),
                age =c("8", "3", "4", "5"))

B <- data.frame(ID = c("Bob", "Bill", "Jack","Dave"),
                shape = c("star", "square", "circle", "triangle"),
                age =c("15", "12", "13", "14"))

C <- data.frame(ID = c("Jane", "Jenny", "Bex","Sarah"),
                animal = c("cat", "dog", "fish", "tiger"),
                food =c("chips", "burger", "sushi", "chocolate"))

ui <- fluidPage(
  titlePanel("Failing at my first shiny app"),
  sidebarLayout(
    sidebarPanel(
      selectInput("tableInput", "Table",
                  choices = c("A", "B", "C"))
    ),
    mainPanel(
      tableOutput("results")
    )
  )
)

server <- function(input, output) {
  
  output$results <- renderTable({
    get(input$tableInput)
  })
}

shinyApp(ui = ui, server = server)

-output

enter image description here

like image 127
akrun Avatar answered Mar 14 '26 10:03

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!