I deployed an app locally and had no problems at all. But when I deploy it on the web then I get disconnected from server message every time I try to upload a file. My app depends 100% on the ability to upload files. I attached a file to so tests if you need to and I paste the code here. It is a super simple app and it is very annoying that is not working
library(shiny)
library(XLConnect)
# library(readxl)
# library(xlsx)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
# br(),
tags$body(style="background-color:white;"),
h1("Panelist Index",style="color:#A4A4A4"),
# Sidebar with a slider input for number of bins
sidebarLayout(
div( sidebarPanel(
h3("Data Upload"),
fileInput("data.file", "Choose Data File"),
h3("Discrimination Index"),
textInput("DIS.upper", "Upper Bound (default=0.5)"),
textInput("DIS.target", "Target (default=0.01)"),
h3("Repeatability Index"),
textInput("REP.upper", "Upper Bound (default=9)"),
textInput("REP.target", "Target (default=1)"),
h3("Agreement Index"),
textInput("AGR.target", "Target (default=0.95)"),
textInput("AGR.lower", "Lower Bound (default=0.5)"),
h3("Download the outputs"),
textInput("savefile1", "File Name"),
downloadButton("downloadtable1", "Save Data Table to .xlsx")
,style="color:#6E6E6E"
,br()
# ,img(width=200, height=100,src='Capture.png')
)),
# Show a plot of the generated distribution
mainPanel(
div(
fluidRow(
column(8,tableOutput("contents")),
column(4,img(width=200, height=100,src='CaptureW.png')),
column(8,plotOutput("plot")),
style="color:#574c4c "
)
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
#Geometric Mean
gm_mean = function(x, na.rm=TRUE){
x[x==0] <- 10^(-2)
exp(sum(log(x), na.rm=T) / length(x))
}
Infile <- reactive({
inFile <- input$data.file
if (is.null(inFile))
return(NULL)
else{
wb = loadWorkbook(inFile$datapath)
sheet.names <- getSheets(wb)
l <- vector("list")
for(i in 1:length(sheet.names)){
l[[i]] <- readWorksheet(wb, sheet = sheet.names[i])
}
names(l) <- sheet.names
return(l)}
})
## Discrimination Index ##
DIS.index.panel <- reactive({
infile <- Infile()[[2]]
dim <- dim(infile)
data.raw <- infile[2:(dim[1]-1), (-1)]
panelist <- as.character(infile[1, -1]) # panelist
attribute <- as.character(infile[2:(dim[1]-1), 1]) # attribute
data <- matrix(NA, nrow(data.raw), ncol(data.raw))
for(i in 1:nrow(data)){
for(j in 1:ncol(data)){
if(
is.na(as.numeric(data.raw[i, j]))){
if( length(grep("<", data.raw[i, j]))==1 ) data[i, j] <- as.numeric(sub("<", "", data.raw[1, 2]))/2
else data[i, j] <- NA
}
else(data[i, j] <- as.numeric(data.raw[i, j]))
}
}
colnames(data) <- panelist
rownames(data) <- attribute
# discrimination index; lower is better
if(
is.na(as.numeric(input$DIS.upper)))
upper <- 0.5
else upper <- as.numeric(input$DIS.upper)
if(
is.na(as.numeric(input$DIS.target)))
target <- 0.01
else target <- as.numeric(input$DIS.target)
d <- (upper-data)/(upper-target)
d[data>upper] <- 0 # not preferred
d[data<target] <- 1 # most preferred
d
})
DIS.index <- reactive({
apply(DIS.index.panel(), 2, gm_mean, na.rm=T)
})
## Repeatability Index ##
REP.index.panel <- reactive({
infile <- Infile()[[3]]
dim <- dim(infile)
data.raw <- infile[2:(dim[1]-1), (-1)]
panelist <- as.character(infile[1, -1]) # panelist
attribute <- as.character(infile[2:(dim[1]-1), 1]) # attribute
data <- matrix(NA, nrow(data.raw), ncol(data.raw))
for(i in 1:nrow(data)){
for(j in 1:ncol(data)){
if(
is.na(as.numeric(data.raw[i, j]))){
if( length(grep("<", data.raw[i, j]))==1 ) data[i, j] <- as.numeric(sub("<", "", data.raw[1, 2]))/2
else data[i, j] <- NA
}
else(data[i, j] <- as.numeric(data.raw[i, j]))
}
}
colnames(data) <- panelist
rownames(data) <- attribute
# discrimination index; lower is better
if(
is.na(as.numeric(input$REP.upper)))
upper <- 9
else upper <- as.numeric(input$REP.upper)
if(
is.na(as.numeric(input$REP.target)))
target <- 1
else target <- as.numeric(input$REP.target)
d <- (upper-data)/(upper-target)
d[data>upper] <- 0 # not preferred
d[data<target] <- 1 # most preferred
d
})
REP.index <- reactive({
apply(REP.index.panel(), 2, gm_mean, na.rm=T)
})
## Agreement Index ##
AGR.index.panel <- reactive({
infile <- Infile()[[4]]
dim <- dim(infile)
data.raw <- infile[2:(dim[1]-1), (-1)]
panelist <- as.character(infile[1, -1]) # panelist
attribute <- as.character(infile[2:(dim[1]-1), 1]) # attribute
data <- matrix(NA, nrow(data.raw), ncol(data.raw))
for(i in 1:nrow(data)){
for(j in 1:ncol(data)){
if(
is.na(as.numeric(data.raw[i, j]))){
if( length(grep("<", data.raw[i, j]))==1 ) data[i, j] <- as.numeric(sub("<", "", data.raw[1, 2]))/2
else data[i, j] <- NA
}
else(data[i, j] <- as.numeric(data.raw[i, j]))
}
}
colnames(data) <- panelist
rownames(data) <- attribute
# discrimination index; lower is better
if(
is.na(as.numeric(input$AGR.target)))
target <- 0.95
else target <- as.numeric(input$AGR.target)
if(
is.na(as.numeric(input$AGR.lower)))
lower <- 0.5
else lower <- as.numeric(input$AGR.lower)
d <- (data-lower)/(target-lower)
d[data>target] <- 1 # most preferred
d[data<lower] <- 0 # not preferred
d
})
AGR.index <- reactive({
apply(AGR.index.panel(), 2, gm_mean, na.rm=T)
})
final.index <- reactive({
exp( (4*log(DIS.index()) + 3*log(REP.index()) + 3*log(AGR.index())) / 10 )
})
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
if(is.null(input$data.file))
return(NULL)
data.frame(DIS=DIS.index(), REP=REP.index(), AGR=AGR.index(), FINAL=final.index())
}, include.rownames=TRUE)
output$downloadtable1<-downloadHandler(
filename = function() {paste(input$savefile1, ".xlsx", sep="")},
content = function(file) {
final <- data.frame(DIS=DIS.index(), REP=REP.index(), AGR=AGR.index(), FINAL=final.index())
writeWorksheetToFile(file, data=DIS.index.panel(), sheet="DIS desirability", startRow = 1, startCol = 1, rownames = "attribute")
writeWorksheetToFile(file, data=REP.index.panel(), sheet="REP desirability", startRow = 1, startCol = 1, rownames = "attribute")
writeWorksheetToFile(file, data=AGR.index.panel(), sheet="AGR desirability", startRow = 1, startCol = 1, rownames = "attribute")
writeWorksheetToFile(file, data=final, sheet="Summary", startRow = 1, startCol = 1, rownames = "attribute")
# write.xlsx(DIS.index.panel(),file, sheetName="DIS desirability")
# write.xlsx(REP.index.panel(),file, sheetName="REP desirability",append=TRUE)
# write.xlsx(AGR.index.panel(),file, sheetName="AGR desirability",append=TRUE)
# write.xlsx(final,file, sheetName="Summary",append=TRUE)
}
)
output$plot<-renderPlot({
clr<-"#336699"
if(is.null(input$data.file)){
par(bg="white",col.lab=clr)
plot(0,xaxt='n',yaxt='n',bty='n',pch='',ylab='',xlab='')
return(NULL)}
x<-rev(final.index())
par(bg="white",col.lab=clr,mar = c(3.1,10,1.6,2),las=2,cex=1)
infile <- Infile()[[4]]
panelist <- as.character(infile[1, -1]) # panelist
barplot(x,horiz=T,col=clr,col.axis="#574c4c",names.arg = panelist,xlim=c(0,1),cex.axis=1.25)
abline(v=c(0.4,0.6,0.8),col=c("firebrick1","orange1","limegreen"),lwd=3,lty=2)
})
}
# Run the application
shinyApp(ui = ui, server = server)