【问题标题】:How to clean up CSV data after uploading to Shiny App上传到 Shiny App 后如何清理 CSV 数据
【发布时间】:2019-05-02 03:48:03
【问题描述】:

请帮忙!

我正在尝试构建一个闪亮的应用程序,旨在对从 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")
  )
))

请告知是否使用 Reactive

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)

【问题讨论】:

    标签: r shiny tidytext


    【解决方案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()
      })
    })
    

    【讨论】:

    • Kristiaan Oord,这是否解决了您的问题?习惯上提供一些关于它是否有效的反馈(如果有效,"accept it")(请提供它是如何失败的,并且通常需要编辑原始问题,通常添加一行额外的输入来驱动失败)。
    猜你喜欢
    • 2018-01-02
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多