添加到闪亮

时间:2017-07-13 00:23:14

标签: r shiny

我试图将两个新列添加到闪亮的数据框中。我目前收到错误invalid 'envir' argument of type 'closure',但我不确定原因。

我的server.R代码是:

server <- function(input, output) {

  inFile <- reactive({input$file1})

  data <- reactive({
    if (is.null(inFile())) return(NULL)
    read.xlsx2(inFile()$datapath,
               1,
               sheetName=NULL,
               colIndex = c(1:7),
               header = TRUE)
  })

  z_scores <- reactive({
    if (is.null(inFile())) return(NULL)
    with(data, ave(as.numeric(data$Raw.Score), data$Reader.Name, FUN=scale))
  })

  percentile <- reactive({
    if (is.null(inFile())) return(NULL)
    format( pnorm(data$z_scores) * 100, scientific = FALSE)
  })

  processedData <- reactive({
    if (is.null(inFile())) return(NULL)
    cbind(
      data(),
      z_score = z_scores(),
      percentile = percentile()
    )
  })

  output$view <- renderDataTable(
    processedData(),
    options = list(pageLength = 10)
  )

}

我的ui.R代码是:

ui <- shinyUI( fluidPage(
    sidebarLayout(
      sidebarPanel(
           fileInput("file1", "Choose XLSX File", accept = ".xlsx"),
           checkboxInput("header", "Header", TRUE)
      ),
      mainPanel(
        dataTableOutput("view")
      )
    )
) )

我需要做些什么才能避免此错误?我甚至不确定它试图告诉我什么。

由于

1 个答案:

答案 0 :(得分:1)

以下代码适用于此数据集

myDat <- data.frame(
  z_scores = rnorm(10), Raw.Score = rnorm(10), 
  Reader.Name = sample(letters,10) )
file <- paste("inputfile", "xlsx", sep=".")
write.xlsx(myDat, file = file)

您似乎使用了反应值data错误。如果要访问被动的当前值,则需要使用data()data()$column_name。我还强烈建议您重新考虑变量命名。 data是来自utils的函数,用于从库中加载数据集。覆盖此功能有时会导致非常奇怪的行为。

server <- function(input, output) {

  inFile <- reactive({input$file1})

  data <- reactive({
    if (is.null(inFile())) return(NULL)
    read.xlsx2(inFile()$datapath, 1, sheetName = NULL, 
               colIndex = c(1:7), header = TRUE)
  })

  z_scores <- reactive({
    if (is.null(inFile())) return(NULL)
    with(data(), ave(as.numeric(data()$Raw.Score), data()$Reader.Name, FUN = scale))
  })

  percentile <- reactive({
    if (is.null(inFile())) return(NULL)
    format( pnorm(as.numeric(data()$z_scores)) * 100, scientific = FALSE)
  })

  processedData <- reactive({
    if (is.null(inFile())) return(NULL)
    cbind(
      data(),
      z_score = z_scores(),
      percentile = percentile()
    )
  })

  output$view <- renderDataTable(
    { processedData() },
    options = list(pageLength = 10)
  )

}

ui <- shinyUI( fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose XLSX File", accept = ".xlsx"),
      checkboxInput("header", "Header", TRUE)
    ),
    mainPanel(dataTableOutput("view"))
  )
))

shinyApp(ui, server)