2016-08-24 10 views
0

У меня есть две таблицы, содержащие разные наборы данных. Я хочу, чтобы таблицы отображались на отдельных вкладках. Мой код отлично работает, когда я показываю только одну таблицу, но всякий раз, когда я пытаюсь отобразить оба значения таблиц, больше не отображаются, просто разные заголовки. Буду признателен за любую оказанную помощь. Ниже мой код. Я использую базу данных 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")) 
) 
) 
) 
)) 

ответ

0

Похоже, вы вызываете таблицы дважды (один в DT :: dataTableOutput(), и снова в tableOutput ()). Названия таблиц (таблица и таблица2) - это идентификаторы, которые в действительном синтаксисе HTML могут быть вызваны только один раз. Вы вызываете их дважды, что может быть причиной того, что ваши таблицы не отображаются. Зачем вам звонить дважды?

0

Как Богдан упоминает, что вы пытаетесь сделать ваши таблицы дважды в 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") 
    ) 
    ) 
    ) 
)))