0

I'm trying to create a dashboard using Shiny. Here is some sample data:

###Creating Data
name <- c("Sharon", "Megan", "Kevin")
x <- c(5, 7,3)
y <- c(3,6,2)
z <- c(2,3,7)
jobForm = data.frame(name, x, y, z)

What I'm trying to figure out is, for every row of names how do I create their own TABLE? I believe there is a way to create a reactive for-loop but I've been at this for a long time and have given up.

Here is the full code of what the dashboard should look like for each name. This code only shows Sharon's scores, and it should run. If there are any issues on getting the code to run completely let me know.

I am using

packages shiny, shinydashboard and tidyverse

##Dashboard Header
header <- dashboardHeader(
  title = "My Project")

##Dashboard Sidebar
sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Dashboard", icon = icon("dashboard"),tabName = "dashboard"),
    menuItem("Job Positions", icon = icon("address-card"), tabName = "jobposition",
             menuSubItem('Sales',
                         tabName = 'sales',
                         icon = icon('line-chart'))
    )
  )
)

##Dashboard Body
body <- dashboardBody(
  tabItems(
    # Dashboard Tab Content
    tabItem(tabName = "dashboard",
            fluidRow(
              #Random Plot
              box( )
            )
    ),

    # Associate Tab Content
    tabItem(tabName = "sales",
            fluidRow(
              #Main Box for Candidate
              box(
                width = 8,
                title = "Candidate 001",
                status = "primary",
                #Box for Table
                box(
                  title = "Table",
                  status = "info",
                  tableOutput("stat1")
                )
              )
            )
    )
  )
)

##User Interface Using Dashboard Function
ui <- dashboardPage(
  skin = "yellow",
  header,
  sidebar,
  body
)

##Server: Instructions
server <- function(input, output) { 
  temp <- data.frame(jobForm %>% 
                       slice(1) %>% 
                       select(x:z))
  temp <- as.data.frame(t(temp))
  output$stat1 <-renderTable({
    temp
  },
  include.rownames=TRUE,
  colnames(temp)<-c("Score")
  )
}

##Create Shiny App Object
shinyApp(ui, server)

Thank you for any help

skk
  • 175
  • 3
  • 10
  • 1
    Possible duplicate of [dynamically add plots to web page using shiny](https://stackoverflow.com/questions/15875786/dynamically-add-plots-to-web-page-using-shiny) – divibisan Jun 19 '18 at 17:38

1 Answers1

0

You better solve these kibnd of problems with an renderUI and since you never really know when shiny will evaluate an expression you are much better of using lapply then for loops.

name <- c("Sharon", "Megan", "Kevin")
x <- c(5, 7,3)
y <- c(3,6,2)
z <- c(2,3,7)
jobForm = data.frame(name, x, y, z)


##Dashboard Header
header <- dashboardHeader(
  title = "My Project")

##Dashboard Sidebar
sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Dashboard", icon = icon("dashboard"),tabName = "dashboard"),
    menuItem("Job Positions", icon = icon("address-card"), tabName = "jobposition",
             menuSubItem('Sales',
                         tabName = 'sales',
                         icon = icon('line-chart'))
    )
  )
)

##Dashboard Body
body <- dashboardBody(
  tabItems(
    # Dashboard Tab Content
    tabItem(tabName = "dashboard",
            fluidRow(
              #Random Plot
              box( )
            )
    ),

    # Associate Tab Content
    tabItem(tabName = "sales",
            fluidRow(
              #Main Box for Candidate
              uiOutput("candidates")
            )
    )
  )
)

##User Interface Using Dashboard Function
ui <- dashboardPage(
  skin = "yellow",
  header,
  sidebar,
  body
)

##Server: Instructions
server <- function(input, output) { 
  temp <- data.frame(jobForm %>% 
                       slice(1) %>% 
                       select(x:z))
  temp <- as.data.frame(t(temp))
  output$stat1 <-renderTable({
    temp
  },
  include.rownames=TRUE,
  colnames(temp)<-c("Score")
  )

  output$candidates <- renderUI(
    tagList(
      lapply(1:nrow(jobForm), function(idx){
        output[[paste0("stat",idx)]] <- renderTable(
          jobForm[idx,-1]
        )
        box(
          width = 8,
          title = paste0("Candidate: ",jobForm$name[idx]),
          status = "primary",
          #Box for Table
          box(
            title = "Table",
            status = "info",
            tableOutput(paste0("stat",idx))
          )
        )
      })
    )
  )
}

##Create Shiny App Object
shinyApp(ui, server)

Hope this helps!!

Bertil Baron
  • 4,923
  • 1
  • 15
  • 24