R Shiny-包含用户填充的输入的数据表返回null

时间:2018-09-13 13:24:19

标签: r datatable shiny

我用这篇文章用numeric inputs创建了一个表。用户应填写/更改此表的默认值以适合他:

How to have table in Shiny filled by user?

它工作正常,但是当我随后尝试在服务器功能中使用这些inputs时,这些输入为NULL。我不明白为什么会这样。

functions.R

profile_df <- function(from_date, to_date){
  m <- get_months(from_date = from_date, to_date = to_date)
  Injection <- c()
  Withdrawal <- c()
  for(i in 1:length(m)){
    Injection <- c(Injection, as.character(numericInput(paste0("x", i, "1"), "", value = 100, step = 10, width = '100%')))
    Withdrawal <- c(Withdrawal, as.character(numericInput(paste0("x", i, "2"), "", value = 100, step = 10,width = '100%')))
  }
  dt <- data.frame(Injection, Withdrawal)
  dt <- t(dt)
  colnames(dt) <- m
  row.names(dt) <- c("Injection [MWh/h]", "Withdrawal [MWh/h]")
  return(dt)
}

ui.R:

library(shiny)
library(shinydashboard)
library(DT)

dashboardPage(
  dashboardHeader(title = "Valuation Tool",
                  tags$li(class = "dropdown", actionButton("pdf", "Manual", icon = icon("book"),
                                                           onclick = "window.open('valuation-tool-manual.pdf')"))),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Storage Valuation", tabName = "storage_valuation", icon = icon("dashboard"))
    )
  ),
  dashboardBody(
    tags$head(tags$style(HTML('
      .main-header .logo {
        font-family: "Georgia", Times, "Times New Roman", serif;
        font-weight: bold;
        font-size: 24px;
      }
    '))),
    tabItems(

  #===============
  # Tab Item Data
  #===============

  tabItem(tabName = "storage_valuation",
          fluidRow(box(title = "Inputs", solidHeader = TRUE, status = "warning", width = 12,
          column(4, dateRangeInput("storage_rng", "Storage Period From/To", startview = 'year', weekstart = 1,
                         start = paste(as.character(year(Sys.Date())+1), "04", "01", sep = "-"),
                         end = paste(as.character(year(Sys.Date())+2), "03", "31", sep = "-")
                         )),
          column(2, selectInput("hub", "Gas Hub",choices = c("TTF" = "ICE/TFM", "NCG" = "ICE/GNM", "GSPL" = "ICE/GER"))),
          column(2, numericInput("working_gas", "Working Gas [MWh]", min = 0, value = 1000)),
          column(2, numericInput("inj_fees", "Injection Fees [/MWh]", min = 0, value = 0.1)),
          column(2, numericInput("wit_fees", "Withdrawal Fees [/MWh]", min = 0, value = 0)),br(),
          strong("Storage Injection/Withdrawal Profile:"), br(),
          fluidRow(column(12,DT::dataTableOutput('profile_table'))),
          fluidRow(column(12,DT::dataTableOutput('prof_table')))
          )

  )
  )))

server.R:

Sys.setlocale("LC_TIME", "C")
library(shiny)
library(shinydashboard)
library(DT)
source("functions.R", local = T)

function(input, output, session) {

  pr_dt <- reactive({profile_df(input$storage_rng[1], input$storage_rng[2])})

  output$profile_table <- renderDT({
    DT::datatable(pr_dt(), extensions=list("FixedColumns" = list(leftColumns = 1)),
                  options = list(dom = 't', ordering= FALSE,
                                 fixedColumns = list(leftColumns = 1), scrollX = T), rownames = T,
                  escape = F,
      callback = JS("table.rows().every(function(i, tab, row) {
                  var $this = $(this.node());
                  $this.attr('id', this.data()[0]);
                  $this.addClass('shiny-input-container');
                  });
                  Shiny.unbindAll(table.table().node());
                  Shiny.bindAll(table.table().node());"))
})

  m_pr_dt <- reactive({
  inj_m <- c()
  wit_m <- c()
  for(i in 1:length(colnames(pr_dt()))){
    print(is.null(input$x11))
}
})

结果是:

TRUETRUETRUE,...已经12次,因为我在print上循环了12次

0 个答案:

没有答案