通过Shiny UI读取.csv文件后应用函数

时间:2018-01-08 15:01:00

标签: r shiny

我有一个闪亮的应用程序,它有一个如下所示的server.R文件:

setwd("C:/Users/Marc/Dropbox/PROJECTEN/Lopend/bank_mining/test_data")
source("functions.R")

library(dplyr)
library(rhandsontable)
library(ggplot2)
options(shiny.maxRequestSize = 9*1024^2)


function(input, output) {

  values <- reactiveValues()
  output$contents <- renderRHandsontable({

    items <- c("Boodschappen", "Noodzakelijk")
    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    df <- read.csv(inFile$datapath, header = input$header,
             sep = input$sep, quote = input$quote)

    #Set dir and load functions.R file
    setwd("C:/Users/Marc/Dropbox/PROJECTEN/Lopend/bank_mining/test_data")
    source("functions.R")

    df <- predict_post(df)  
    rhandsontable(df, width = 700, height = 300) %>%    
      hot_col(col = "Post", type = "dropdown", source = items) %>%
      hot_col(col="Omschrijving", width = 200) %>%
      hot_col(col="Post", width = 150) %>%
      hot_col(col="Transactiebedrag", format = "0.00 $")
      #columns
  })
  saveData <- eventReactive({input$saveBtn},{
    finalDF <- hot_to_r(input$contents)
    finalDF$Post <- ifelse(finalDF$Post =="",NA,finalDF$Post)
    newDF <- finalDF[complete.cases(finalDF),]
    return(newDF)
  })

  saveDataFinalTab <- function(){
    setwd("C:/Users/Marc/Dropbox/PROJECTEN/Lopend/bank_mining/test_data")
    finalDF <- hot_to_r(input$contentFinal) 
    write.csv(finalDF, "total_expenses_personal.csv")
  }

  output$contentFinal <- renderRHandsontable(
    rhandsontable(saveData(), width = 700, height = 300) %>%
      hot_col(col="Omschrijving", width = 200) %>%
      hot_col(col="Post", width = 150)
  )
  output$dashboard1 <- renderPlot(

    #finalDF <- hot_to_r(input$contentFinal),
    ggplot(input$contentsFinal, aes(x = Post, y = Transactiebedrag )) +
      geom_bar(stat = "identity")
  )
  observeEvent(input$saveFinalBtn, saveDataFinalTab())

}

我将此与以下UI.R文件结合使用:

library(rhandsontable)
fluidPage(

  # App title ----
  titlePanel("Tabsets"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Tabset w/ plot, summary, and table ----
      tabsetPanel(type = "tabs",
                  tabPanel("Data uploading", 

                           fileInput('file1', 'Choose file to upload',
                                     accept = c(
                                       'text/csv',
                                       'text/comma-separated-values',
                                       'text/tab-separated-values',
                                       'text/plain',
                                       '.csv',
                                       '.tsv'
                                     )
                           ),
                           tags$hr(),
                           checkboxInput('header', 'Header', TRUE),
                           radioButtons('sep', 'Separator',
                                        c(Comma=',',
                                          Semicolon=';',
                                          Tab='\t'),
                                        ','),
                           tags$hr(),
                           p()),
                  tabPanel("Data editing", rHandsontableOutput('contents'),
                           actionButton("saveBtn", "Save changes"),
                           textOutput('text')
                           ),
                  tabPanel("Tab",
                           rHandsontableOutput('contentFinal'),
                           actionButton("saveFinalBtn", "Write away")),
                  tabPanel("Dashboard",
                           plotOutput("dashboard1")
                           )



      )

    )
  )
)

我的问题是我有一个我存储在同一文件夹中的functions.R文件。它包含以下代码:

predict_post <- function(df){
  df$Post <- ifelse(df$Omschrijving %in% c("ALBERT HEIJN", "AH"), "", "Boodschappen")
  return(df)
}

但是当我尝试运行它时,无法识别该功能。所以相关部分似乎是server.R文件中的这一部分:

df <- read.csv(inFile$datapath, header = input$header,
         sep = input$sep, quote = input$quote)

#Set dir and load functions.R file
setwd("C:/Users/Marc/Dropbox/PROJECTEN/Lopend/bank_mining/test_data")
source("functions.R")
df <- predict_post(df)

由于某种原因,无法读取predict_post()函数。

关于如何在读取.csv文件后从另一个R文件调用函数的想法?

0 个答案:

没有答案