上载到Shiny App后如何清理CSV数据

时间:2018-11-30 01:19:12

标签: r shiny tidytext

请帮助!

我正在尝试构建一个Shiny App,目的是对从CSV文件加载的数据进行分类。如何从CSV文件(已上传)成功创建DataFrame,以便可以继续进行和清理/分析它。

请参见代码:

library(shiny)
library(lubridate)
library(utils)
library(dplyr)
library(tidytext) 

ui <- (pageWithSidebar(
  headerPanel("CSV File Upload Demo"),

  sidebarPanel(
    #Selector for file upload
    fileInput('datafile', 'Choose CSV file',
              accept=c('text/csv', 'text/comma-separated-values,text/plain')),
    #These column selectors are dynamically created when the file is loaded
    uiOutput("fromCol"),
    uiOutput("toCol"),
    uiOutput("amountflag"),
    #The conditional panel is triggered by the preceding checkbox
    conditionalPanel(
      condition="input.amountflag==true",
      uiOutput("amountCol")
    )

  ),
  mainPanel(
    tableOutput("filetable")
  )
))

请告知是否使用反应式

server <- (function(input, output) {

  #This function is repsonsible for loading in the selected file
  filedata <- reactive({
    infile <- input$datafile
    if (is.null(infile)) {
      # User has not uploaded a file yet
      return(NULL)
    }

    dataframe <- reactive({
      readr::read_csv(infile()$datapath)
    })

  # Clean data by whole-case removal of missing cells (either NAs or "nan")
  # Remove the rows which have NAs
  myDataClean2 = dataframe[complete.cases(dataframe),]

  # In order to turn it into a tidy text dataset, we first put the data into a data frame:
  text_df <- data_frame(myDataClean2$text,myDataClean2$title,myDataClean2$author,myDataClean2$id,myDataClean2$label)
  names(text_df) <- c("text","title","author","id","label")

  # Within the tidy text framework, we break both the text into individual tokens and transform 
  # it to a tidy data structure. To do this, we use tidytextâs unnest_tokens() function.
  tidy_text_df <- text_df %>%
    unnest_tokens(word, text)

     #This previews the CSV data file
  output$filetable <- renderText({
    tidy_text_df()

  })
  })
    })



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

1 个答案:

答案 0 :(得分:1)

您正在混合反应块。 filedata的结尾应该是可以输出数据的内容,可能是unnest_tokens(word, text)的输出。 (它应该列出您感兴趣的所有数据,我认为那行确实如此。)从此处开始,您的output$filetable必须位于filedata的反应式块的外部 , 在其自己的。而且应该使用filedata(),而不是tidy_text_df(在第一个反应式块之外不可用)。

尝试一下:

server <- (function(input, output) {

  #This function is repsonsible for loading in the selected file
  filedata <- reactive({
    infile <- input$datafile
    if (is.null(infile)) {
      # User has not uploaded a file yet
      return(NULL)
    }

    dataframe <- reactive({
      readr::read_csv(infile()$datapath)
    })

    # Clean data by whole-case removal of missing cells (either NAs or "nan")
    # Remove the rows which have NAs
    myDataClean2 = dataframe[complete.cases(dataframe),]

    # In order to turn it into a tidy text dataset, we first put the data into a data frame:
    text_df <- data_frame(myDataClean2$text,myDataClean2$title,myDataClean2$author,myDataClean2$id,myDataClean2$label)
    names(text_df) <- c("text","title","author","id","label")

    # Within the tidy text framework, we break both the text into individual tokens and transform 
    # it to a tidy data structure. To do this, we use tidytextâs unnest_tokens() function.
    text_df %>%
      unnest_tokens(word, text)
  })

  #This previews the CSV data file
  output$filetable <- renderText({
    filedata()
  })
})