使用reactFileReader函数时如何向数据框添加函数

时间:2019-08-26 03:20:15

标签: r shiny shinydashboard

我使用reactFileReader从我的wd自动更新数据。但是,我需要向原始数据添加一些功能以使其可以使用。

fileData<-reactiveFileReader(intervalMillis = 1000,
                           session = session,
                           filePath = "data.xlsx",
                           readFunc = read_excel)

  output$data <- renderTable({
     fileData()
   })

I want to add somefunction like this
  fileData<-fileData %>%
    select(-2,-4:-6)
  names(fileData)[3:4]<-c("overall","city")
  fileData$city<-as.character(fileData$city)

1 个答案:

答案 0 :(得分:0)

这就是我的想法。

library(shiny)
library(dplyr)

# test file (to replace the file you are monitoring)
write.csv(iris, file="iris.csv")

# function you want applied to your data
# this can be defined here, in global.R (if you have that) or inside the server function
process_data <- function(data_from_file) {
  result <- data_from_file %>% 
    select(Species, starts_with("Sepal")) %>% 
    group_by(Species) %>% summarise_all("mean") %>% 
    rename(mean_sepal_width="Sepal.Width", mean_sepal_length="Sepal.Length")
  return(result)
}

# the user interface, with the first few rows of the original data
# and the data processed by the function

ui <- basicPage(
  h3("original data"),
  tableOutput("original_data"),
  tags$hr(),
  h3("processed data"),
  tableOutput("processed_data")
)

server <- function(input, output, session) {

  fileData<-reactiveFileReader(intervalMillis = 1000,
                               session = session,
                               filePath = "iris.csv",
                               readFunc = read.csv)

  output$original_data <- renderTable({
    fileData() %>% head
  })

  # custom function applied to your data
  output$processed_data <- renderTable({
    process_data( data_from_file = fileData() )
  })

}

shinyApp(ui, server)