【问题标题】:How to add function to a dataframe when using reactiveFileReader function使用 reactiveFileReader 函数时如何向数据框添加函数
【发布时间】:2019-08-26 03:20:15
【问题描述】:

我使用 reactiveFileReader 自动更新我的 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)

【问题讨论】:

  • 您可以创建要应用于数据的函数(例如fun),然后将renderTable 调用替换为output$data &lt;- renderTable({ fun(fileData() ) })
  • 你能不能更具体一些,你有什么例子吗? @teofil

标签: r shiny shinydashboard


【解决方案1】:

这就是我的想法。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    相关资源
    最近更新 更多