1

Please help!

I'm trying to build a Shiny App with the intent to classify data loaded from a CSV file. How do I successfully create a DataFrame from a CSV file (that is uploaded) so that I can move forward and clean/analyze it.

Please see code:

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")
  )
))

Please advise whether to use 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)

1 Answers1

1

You are mixing reactive blocks. Your filedata should end with something that outputs your data, likely the output from unnest_tokens(word, text). (It should put out all data you are interested in, I think that that line does.) From there, your output$filetable needs to be outside of filedata's reactive block, on its own. And it should be using filedata(), not tidy_text_df (which isn't available outside of the first reactive block).

Try this:

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()
  })
})
r2evans
  • 141,215
  • 6
  • 77
  • 149