-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
115 lines (93 loc) · 2.63 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#
library(shiny)
library(quanteda)
library(dplyr)
library(DT)
getCorpus <- function(path){
if(is.null(path)){
return(NULL)
}
tmpPath <- tempdir
fs <- path$datapath
textsvec = c()
for (i in 1:length(fs)){
textsvec[i] <- paste(readLines(fs[i]), collapse = " ")
}
names(textsvec) <- basename(path$name)
qc <- corpus(textsvec)
return(qc)
}
getCorpusFromZip <- function(path, fixedZip = FALSE){
if(is.null(path)){
return(NULL)
}
if(fixedZip){
qc <- (textfile(path))
}
else{
td <- tempdir()
fs <- unzip(path$datapath, exdir = td)
textsvec = c()
for (i in 1:length(fs)){
textsvec[i] <- paste(readLines(fs[i]), collapse = " ")
}
names(textsvec) <- basename(fs)
qc <- corpus(textsvec)
}
return(qc)
}
shinyServer(function(input, output) {
myCorpus <- reactive({
inFile <- input$file1
print('reading in corpus')
if (is.null(inFile))
return(NULL)
if(input$uploadTypeRadio == 1){
mc <- getCorpusFromZip(inFile)
}
else{
mc <- getCorpus(inFile)
}
return(mc)
})
make_dfm <- eventReactive(input$dfm_button,{
curCorpus <- myCorpus()
curDf <- dfm(curCorpus) %>% dfm_trim(as.numeric(input$minFreq), as.numeric(input$minDoc))
if(input$weightingRadio != "count"){
curDf <- weight(curDf, input$weightingRadio)
}
curDf
# toRender <- cbind(filenames = row.names(curDf[,1:showN]), curDf[,1:showN])
} )
make_scale <- eventReactive(input$scale_button,{
print('scale button called')
curCorpus <- myCorpus()
curDf <- make_dfm()
ca_fitted <- textmodel_ca(curDf, nd=2)
coldf <- as.data.frame(ca_fitted$colcoord)
rownames(coldf) <- ca_fitted$colnames
coldf
})
output$fileTable <- DT::renderDataTable({
curCorpus <- myCorpus()
validate(
need(curCorpus != "", "Please select a data set")
)
summary(curCorpus, n=ndoc(curCorpus))
}, options = list(searching = FALSE))
output$dfmTable <- DT::renderDataTable({
disp <- as.data.frame(make_dfm())
disp[,1:input$showN]
})
output$scale_table <- DT::renderDataTable({make_scale()})
output$kwicTable <- renderDataTable({
curCorpus <- myCorpus()
kwic(curCorpus, input$keyword, window = input$contextSize,
valuetype = input$kwicValueType,
case_insensitive = !(input$caseSensitive))
}, options = list(searching = FALSE))
})