2016-08-24 62 views
0

我有兩個包含不同數據集的表。我希望這些表格在單獨的選項卡上顯示。我的代碼完美地工作,當我只顯示一個表,但每當我嘗試顯示兩個表的值不再顯示,只是各種標題。任何幫助深表感謝。以下是我的代碼。我使用拉曼數據庫和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( ))。表名(table和table2)被賦予了ID,這些ID在有效的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") 
    ) 
    ) 
    ) 
)))