在选项卡上显示表格

时间:2016-08-24 19:00:03

标签: r tabs shiny dt

我有两个包含不同数据集的表。我希望表格显示在单独的选项卡上。当我只显示一个表时,我的代码工作正常,但每当我尝试显示两个表值时,不再显示各种标题。任何帮助深表感谢。以下是我的代码。我使用Lahman数据库和DT包。

server.R:

library(shiny)
library(ggplot2)


# Define a server for the Shiny app
shinyServer(function(input, output) {

 # Filter data based on selections
output$table <- DT::renderDataTable(DT::datatable({
data <- Pitching
if (input$yearID != "All") {
  data <- data[data$yearID == input$yearID,]
}
if (input$lgID != "All") {
  data <- data[data$lgID == input$lgID,]
}
if (input$teamID != "All") {
  data <- data[data$teamID == input$teamID,]
}
data
}))

output$table2 <- DT::renderDataTable(DT::datatable({
data <- Batting
if (input$yearID != "All") {
  data <- data[data$yearID == input$yearID,]
}
if (input$lgID != "All") {
  data <- data[data$lgID == input$lgID,]
}
if (input$teamID != "All") {
  data <- data[data$teamID == input$teamID,]
}
data
}))


})

ui.R:

library(shiny)

# Load the ggplot2 package which provides
# the 'mpg' dataset.
library(ggplot2)

# Define the overall UI
shinyUI(
  fluidPage(
   titlePanel("Pitching"),

   # Create a new Row in the UI for selectInputs
   fluidRow(
    column(4,
         selectInput("yearID",
                     "Year:",
                     c("All",
                       unique(as.integer(Pitching$yearID))))
  ),
  column(4,
         selectInput("lgID",
                     "League:",
                     c("All",
                       unique(as.character(Pitching$lgID))))
  ),
  column(4,
         selectInput("teamID",
                     "Team:",
                     c("All",
                       unique(as.character(Pitching$teamID)))))
),

# Create a new row for the table.
fluidRow(
  DT::dataTableOutput("table")

),

fluidPage(
  titlePanel("Batting"),

  # Create a new Row in the UI for selectInputs
  fluidRow(
    column(4,
           selectInput("yearID",
                       "Year:",
                       c("All",
                         unique(as.integer(Batting$yearID))))
    ),
    column(4,
           selectInput("lgID",
                       "League:",
                       c("All",
                         unique(as.character(Batting$lgID))))
    ),
    column(4,
           selectInput("teamID",
                       "Team:",
                       c("All",
                         unique(as.character(Batting$teamID)))))
  ),

  # Create a new row for the table.
  fluidRow(
    DT::dataTableOutput("table2")

  ),

  mainPanel(
    tabsetPanel(type = "tabs",
    tabPanel("Pitching",tableOutput("table")),
    tabPanel("Batting", tableOutput("table2"))
  )
)
 )
  ))

2 个答案:

答案 0 :(得分:0)

看起来你正在调用表两次(一个在DT :: dataTableOutput()中,另一个在tableOutput()中)。表名(table和table2)是给定的ID,在有效的HTML语法中只能调用一次。您正在调用它们两次,这可能是您的表未显示的原因。你为什么要打电话给他们两次?

答案 1 :(得分:0)

正如波格丹提到的那样,你试图在UI中两次渲染你的牌桌。这似乎是您的主要问题。

你最喜欢那里,但是你要两次渲染表的双重调用让我觉得你想要pitching在一个标签页上而batting在另一个标签页上。< / p>

请参阅下面更新的ui.R文件:

library(shiny)

# Load the ggplot2 package which provides
# the 'mpg' dataset.
library(ggplot2)


# Define the overall UI
shinyUI(
  fluidPage(
    mainPanel(
        tabsetPanel(type = "tabs",
                    tabPanel("Pitching",
    titlePanel("Pitching"),

    # Create a new Row in the UI for selectInputs
    fluidRow(
      column(4,selectInput("yearID","Year:",
                         c("All", unique(as.integer(Pitching$yearID))))
      ),
      column(4,selectInput("lgID","League:",
                         c("All",unique(as.character(Pitching$lgID))))
      ),
      column(4,selectInput("teamID","Team:",
                         c("All",unique(as.character(Pitching$teamID)))))
    ),

    # Create a new row for the table.
    fluidRow(
      DT::dataTableOutput("table")

    )),
    tabPanel("Batting",
      titlePanel("Batting"),

      # Create a new Row in the UI for selectInputs
      fluidRow(
        column(4,selectInput("yearID","Year:",
                           c("All",unique(as.integer(Batting$yearID))))
        ),
        column(4,selectInput("lgID","League:",
                           c("All",unique(as.character(Batting$lgID))))
        ),
        column(4,selectInput("teamID","Team:",
                           c("All",unique(as.character(Batting$teamID)))))
      ),

      # Create a new row for the table.
      fluidRow(
        DT::dataTableOutput("table2")
      )
      )
    )
  )))