2016-08-24 44 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") 
    ) 
    ) 
    ) 
))) 
相关问题