如何在Shiny R中添加反应式for循环?

时间:2018-06-19 17:26:25

标签: r shiny shiny-server shinydashboard

我正在尝试使用Shiny创建仪表板。这是一些示例数据:

###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)

我要弄清楚的是,对于每一行名称,我如何创建自己的表?我相信有一种方法可以创建反应式的for循环,但我从事此工作已经很长时间了,已经放弃了。

以下是每个名称的仪表板外观的完整代码。此代码仅显示Sharon的得分,并且应该运行。如果在使代码完全运行方面有任何问题,请告诉我。

我正在使用

  

打包有光泽,有光泽的仪表板和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)

谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

您最好使用renderUI解决这些问题,并且由于您永远不知道何时闪亮会评估表达式,因此最好使用lapply而不是for循环。

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)

希望这会有所帮助!