使用带有navbarMenu的多个tabPanel时,fileInput会中断

时间:2014-08-13 14:44:07

标签: r shiny

我正在尝试在一个有光泽的navbarMenu中创建多个tabPanel。当我在navbarMenu中只有一个带有sidebarPanel和mainPanel的tabPanel时,一切正常,但是当我在navbarMenu中的第二个tabPanel下添加另一个sidebarPanel和mainPanel时,fileInput不起作用。我想也许我有一个缺少的括号或逗号,但这通常会给出错误。

这是server.r

shinyServer(function(input, output, session) {
  # reactive function to upload data
  get_data <- reactive({
    if (!is.null(input$manages_path)){
      switch(input$file_type, 
             ".csv" = from_csv(input$manages_path$datapath),
             ".xls" = from_excel(input$manages_path$datapath))
    }      
  })

  # return a list of well names
  output$wells <- renderUI({
    if (!is.null(input$manages_path)){
      data <- get_data()
      well_names <- as.character(get_well_names(data))
      selectInput("well", "Monitoring Wells", well_names, multiple = TRUE,
                  selected = well_names[1])
    }
  })

  # return a list of constituents
  output$analytes <- renderUI({
    if (!is.null(input$manages_path)){
      data <- get_data()
      analyte_names <- as.character(get_analytes(data))
      selectInput("analyte", "Constituents", analyte_names, multiple = TRUE,
                  selected = analyte_names[1])
    }
  })

  # return start and end date of background data
  output$date_ranges <- renderUI({
    if (!is.null(input$manages_path)){
      data <- get_data()
      tagList(
        dateRangeInput("back_date_range", "Background Date Range", 
                       start = min(data$sample_date, na.rm = TRUE),
                       end = max(data$sample_date, na.rm = TRUE)),
        dateRangeInput("comp_date_range", "Compliance Date Range", 
                       start = max(data$sample_date, na.rm = TRUE),
                       end = max(data$sample_date, na.rm = TRUE))  
      )
    }
  })

  # Output a googleTable of the data
  output$well_table <- renderDataTable({
    if (!is.null(input$manages_path)){
      data <- get_data()
      return(data)
    }
  }, options = list(sScrollY = "100%", sScrollX = "100%", 
                    aLengthMenu = c(5, 10, 15, 25, 50, 100), 
                    iDisplayLength = 10)
  ) 
}) 

global.r带有一些辅助函数来加载数据

get_well_names <- function(df){
  wells <- unique(df$location_id)
  return(wells)
}

get_analytes <- function(df){
  analytes <- unique(df$param_name)
  return(analytes)
}

from_csv <- function(path){
  csv_data <- read.csv(path, header = TRUE)
  csv_data$sample_date <- lubridate::ymd(csv_data$sample_date)
  csv_data$analysis_result <- as.numeric(csv_data$analysis_result)
  csv_data$lt_measure <- factor(csv_data$lt_measure, exclude = NULL)
  return(csv_data)
}

from_excel <- function(path){
  excel_data <- XLConnect::readWorksheet(XLConnect::loadWorkbook(path), 
                                         sheet = "Sheet1",
                                         forceConversion = TRUE, 
                                         dateTimeFormat = "%Y-%m-%d %H:%M:%S")
  excel_data$analysis_result <- as.numeric(excel_data$analysis_result)
  excel_data$lt_measure <- factor(excel_data$lt_measure, exclude = NULL)
  excel_data$param_name <- as.factor(excel_data$param_name)
  return(excel_data)
}

输入数据如下所示

  location_id sample_date analysis_result lt_measure
1        MW-2  2008-10-28           36.30           
2        MW-2  2008-10-28           18.90           
3        MW-2  2008-10-28            0.50           
4        MW-2  2008-10-28           24.90           
5        MW-2  2008-10-28          170.00           
6        MW-2  2008-10-28            0.29           
  default_unit           param_name
1         mg/L Magnesium, dissolved
2         mg/L    Sodium, dissolved
3         mg/L Potassium, dissolved 
4         mg/L      Chloride, total
5         mg/L       Sulfate, total
6         mg/L      Fluoride, total

当我使用以下ui.r

时,一切正常
shinyUI(navbarPage("Groundwater Monitoring Statistics",
      tabPanel("Data",
        sidebarLayout(
          sidebarPanel(
            fileInput(inputId = "manages_path", 
                      label = "Browse to  file",
                      accept = c(".csv", ".xls")),
            conditionalPanel(
              condition = "input.data == FALSE",
              radioButtons(inputId = "file_type", 
                           label = "File Extension", 
                           choices = c(".csv", ".xls")))
          ),
          mainPanel(
            dataTableOutput("well_table")
          )
        )          
      ),
      navbarMenu("Plots",
        tabPanel("Boxplots"),
        tabPanel("Time series",
          sidebarLayout(
            sidebarPanel(
              uiOutput("wells"),
              uiOutput("analytes"),
              uiOutput("date_ranges"),
              checkboxInput("scale_plot","Scale Free Plot"),
              checkboxInput("date_lines", "Show Date Ranges")
            ),
            mainPanel(
             plotOutput("time_plot")
            )
          )       
        ),
        tabPanel("Piper Diagram")
      ),
      navbarMenu("Statistics",
        tabPanel("Prediction Limit"),
        tabPanel("Tolerance Interval")
      )
))

但是,当我将sidebarLayout和mainPanel添加到navbarMenu中的另一个tabPanel时,fileInput将停止工作。下面是带有更改的ui.r文件。

shinyUI(navbarPage("Groundwater Monitoring Statistics",
      tabPanel("Data",
        sidebarLayout(
          sidebarPanel(
            fileInput(inputId = "manages_path", 
                      label = "Browse to  file",
                      accept = c(".csv", ".xls")),
            conditionalPanel(
              condition = "input.data == FALSE",
              radioButtons(inputId = "file_type", 
                           label = "File Extension", 
                           choices = c(".csv", ".xls")))
          ),
          mainPanel(
            dataTableOutput("well_table")
          )
        )          
      ),
      navbarMenu("Plots",
        tabPanel("Boxplots",
         sidebarLayout(
          sidebarPanel(
            uiOutput("wells"),
            uiOutput("analytes"),
            uiOutput("date_ranges"),
            checkboxInput("scale_plot", "Scale Free Plot")
          ),
          mainPanel(
           plotOutput("box_plot")
          )
         )
        ),
        tabPanel("Time series",
          sidebarLayout(
            sidebarPanel(
              uiOutput("wells"),
              uiOutput("analytes"),
              uiOutput("date_ranges"),
              checkboxInput("scale_plot","Scale Free Plot"),
              checkboxInput("date_lines", "Show Date Ranges")
            ),
            mainPanel(
             plotOutput("time_plot")
            )
          )       
        ),
        tabPanel("Piper Diagram")
      ),
      navbarMenu("Statistics",
        tabPanel("Prediction Limit"),
        tabPanel("Tolerance Interval")
      )
))

尝试在navbarMenu中创建多个tabPanel时,我是否遗漏了某些内容?任何帮助将不胜感激。

0 个答案:

没有答案