I am currently writing my first Shiny app and it runs locally on my machine but after being uploaded to
shinyapps.io I get the following error: "No lines available in input." I have done some research and this error seems to be a result of the read.table function evaluating the uploads as empty. However, using the same files locally I am not receiving any errors at all. Here is the code:
library(shiny)
shinyUI(fluidPage(
titlePanel("Sentiment Analysis Tool"),
sidebarLayout(
sidebarPanel(
h3("Upload .csv to get it analyzed!"),
fileInput('file1', 'Choose CSV File',
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
),
tags$hr(),
numericInput("num", label = "Indicate which column to analyze", value = 1, min = 1, max = 100),
checkboxInput('header', 'My Data Has Headers', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"'),
actionButton("button","Submit",
style="color: #fff; background-color: #337ab7; border-color: #2e6da4"),
br(),
br(),
downloadButton('downloadData', 'Download Raw Data'),
downloadButton('downloadPlot', 'Download Plot')
),
mainPanel(
plotOutput("histOutput")
)
)
))
library(shiny)
library(tm)
library(SnowballC)
library(stringr)
library(Rstem)
library(sentiment)
library(ggplot2)
library(plyr)
source("polarity.R")
shinyServer(function(input, output) {
observeEvent(input$button, {
output$histOutput <- renderPlot({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
RR <- try(read.table(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote), TRUE)
validate(
need(class(RR) != 'try-error', "Upload has empty rows, please fix and try again.")
)
withProgress(message = 'Plot is Loading',
detail = 'This may take a while...', value = 0, {
for (i in 1:10) {
validate(
need(input$num <= ncol(RR), "Something has gone wrong, please double check your selections.")
)
sent_df <<- polarity(RR[,input$num])
incProgress(1/10)
Sys.sleep(0.25)
}
})
rawdf <<- c(RR[input$num],polarity(RR[,input$num]))
class_pol <- sent_df[,4]
histplot <<- plotInput(sent_df,class_pol)
histplot
})
})
output$value <- reactive({
input$num
})
output$downloadPlot <- downloadHandler(
filename = function() { paste("SentimentPlot", '.png', sep='') },
content = function(file) {
ggsave(file, plot = histplot, device = "png")
}
)
output$downloadData <- downloadHandler(
filename = function() { paste("RawData", '.csv', sep='') },
content = function(file) {
write.csv(rawdf, file)
}
)
})
And the functions from the polarity file:
polarity = function(x){
#Convert text to matrix
pol = as.matrix(x)
#Remove punctuation, numbers, unnecessary spaces
gsub("[[:punct:]]|[[:digit:]]|[ \t]{2,}|^\\s+|\\s+$", "", pol)
#Error handling using tolower
pol = sapply(pol, try.error)
#Polarity classification
pol = classify_polarity(pol, algorithm = "bayes")
#Use best fit from class_polarity function from column 4
class_pol = pol[,4]
#Create dataframe
data.frame(text=pol, polarity=class_pol, stringsAsFactors=FALSE)
}
try.error = function(x)
{
# create missing value
y = NA
# tryCatch error
try_error = tryCatch(tolower(x), error=function(e) e)
# if not an error
if (!inherits(try_error, "error"))
y = tolower(x)
# result
return(y)
}
plotInput <- function(df,x) {
ggplot(df, aes(x)) +
geom_bar(aes(y=..count.., fill=x)) +
scale_fill_brewer(palette="RdGy") +
labs(x="Polarity Categories", y="Number of Comments") +
ggtitle("Sentiment Analysis of Comments\n(Classification by Polarity)")
}
Any help is appreciated.