如何创建闪亮的函数以在lapply中使用?

时间:2019-06-25 14:17:34

标签: r shiny shinyapps

我正在尝试编写一个函数以在lapply中使用。但是,在闪亮的R中这似乎并不简单,而且我不断收到错误消息

我是Shiny的新手,但对R却老,无法获得有关如何在Shiny中编写自定义函数的清晰信息。

library(shiny)
library(DT)
library(data.table)

#function that tacks on filename column when file is read
fread_with_file <- function(file){
  read_data <- fread(file)
  data_with_name <- read_data %>% mutate(`File_Name` = file)
  return(data_with_name)
}

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("File Selection and Join"),

    # Sidebar with a places to input multiple files 
    sidebarLayout(
        sidebarPanel(
           fileInput("oldcsvs", "Upload the Older files", multiple = TRUE),
           fileInput("newcsvs", "Upload the newer files", multiple = TRUE)
            ),
        # Creating tabs for each data set
        mainPanel(
          tabsetPanel(
            type = "tabs",
            tabPanel("Old Data", DT::dataTableOutput("oldcsvs")),
            tabPanel("New Data", DT::dataTableOutput("newcsvs")),
            tabPanel("Joined Data", DT::dataTableOutput("joinedcsvs"))
            )
          )
        )
)


# Define server logic required to draw a histogram
server <- function(input, output) {


  oldCSVData <- reactive({rbindlist(lapply(input$oldcsvs$datapath, fread_with_file))})
  newCSVData <- reactive({rbindlist(lapply(input$newcsvs$datapath, fread_with_file))})

  output$oldcsvs <- DT::renderDataTable({oldCSVData()})
  output$newcsvs <- DT::renderDataTable({newCSVData()})

}

# Run the application 
shinyApp(ui = ui, server = server)

编译时的错误消息:

Listening on http://127.0.0.1:7822
Warning: Error in makeFunction: argument "expr" is missing, with no default
  51: eval
  50: makeFunction
  49: exprToFunction
  48: reactive
  47: server ********************
Error in makeFunction(body = expr, env = env) : 
  argument "expr" is missing, with no default

0 个答案:

没有答案