【问题标题】:Display Tables on Tabs在选项卡上显示表格
【发布时间】:2016-08-24 19:00:02
【问题描述】:

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

服务器.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"))
  )
)
 )
  ))

【问题讨论】:

    标签: r tabs shiny dt


    【解决方案1】:

    您似乎调用了两次表(一次在 DT::dataTableOutput() 中,一次在 tableOutput() 中)。表名(table 和 table2)有 ID,在有效的 HTML 语法中只能调用一次。你给他们打了两次电话,这可能是你的桌子没有出现的原因。为什么要给他们打两次电话?

    【讨论】:

      【解决方案2】:

      正如 Bogdan 提到的,您正尝试在 UI 中两次呈现您的表格。这似乎是您的主要问题。

      您是那里的大多数方式,但是您两次渲染表格的双重调用让我认为您希望 pitching 位于一个选项卡上,而 batting 位于另一个选项卡上。

      查看下面更新的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")
            )
            )
          )
        )))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多