我已经花了好几个小时才发现我的闪亮应用程序有什么问题。服务器和ui在下面,以及对服务器进行预测的功能。
当我在文本框中写入时,反应性确实有效,但会出现相同的错误。我不是Shiny的专家。这是我的第二个应用程序。
请帮忙。
功能:
library(tidyr)
library(dplyr)
library(quanteda)
library(stringr)
## Read-in frequencies
df1 <- readRDS("./App/nextword/g1.rds")
df2 <- readRDS("./App/nextword/g2.rds")
df3 <- readRDS("./App/nextword/g3.rds")
df4 <- readRDS("./App/nextword/g4.rds")
texti <- "i, Don't think"
texti <- corpus(texti)
texti <- corpus(tolower(texti))
## Clean input
texti <- tokenize(texti,
remove_numbers = TRUE,
remove_punct = TRUE,
remove_symbols = TRUE,
remove_separators = TRUE,
remove_twitter = TRUE,
remove_hyphens = TRUE,
remove_url = TRUE,
concatenator = " ",
verbose = FALSE)
texti <- dfm(texti)
texti <- data.frame(word = featnames(texti), row.names = NULL, stringsAsFactors = FALSE)
p <- function(texti) {
if(texti[1, ] == "" & texti[2, ] == "" & texti[3, ] == "") {
prediction = df1 %>%
select(word, Frequency)
} else if(texti[1, ] %in% df4$w1 & texti[2, ] %in% df4$w2 & texti[3, ] %in% df4$w2) {
prediction = df4 %>%
filter(w1 %in% texti[1, ] & w2 %in% texti[2, ] & w3 %in% texti[3, ]) %>%
select(w4, Frequency)
} else if(texti[1, ] %in% df3$w1 & texti[2, ] %in% df3$w2) {
prediction = df3 %>%
filter(w1 %in% texti[1, ] & w2 %in% texti[2, ]) %>%
select(w3, Frequency)
} else if(texti[1, ] %in% df2$w1) {
prediction = df2 %>%
filter(w1 %in% texti[1, ]) %>%
select(w2, Frequency)
} else{
prediction = df1 %>%
select(word, Frequency)
}
return(prediction)
}
服务器:
library(shiny)
library(shinythemes)
library(markdown)
shinyServer(function(input, output, session) {
pt1 <- reactive(p(input$texti)[1])
output$texti1 <- pt1
observeEvent(input$b1, {
updateTextInput(session, "texti",
value = paste(input$texti, pt1()))
})
pt2 <- reactive(p(input$texti)[2])
output$texti2 <- pt2
observeEvent(input$b2, {
updateTextInput(session, "texti",
value = paste(input$texti, pt2()))
})
pt3 <- reactive(p(input$texti)[3])
output$texti3 <- pt3
observeEvent(input$b3, {
updateTextInput(session, "texti",
value = paste(input$texti, pt3()))
})
})
UI:
library(shinythemes)
shinyUI(fluidPage(
theme = shinytheme("darkly"),
tags$hr(),
titlePanel("Next Word Prediction Application"),
tags$hr(),
mainPanel(tabsetPanel(
tabPanel("Prediction",
sidebarLayout(
sidebarPanel(
width = 3,
tags$p(""),
tags$h5("Predicted next word:"),
flowLayout(
actionButton("b1", label = textOutput("texti1")),
actionButton("b2", label = textOutput("texti2")),
actionButton("b3", label = textOutput("texti3"))
)
),
mainPanel(
tags$p(""),
tags$h5("Please, enter your text:"),
h4(tags$textarea(id = "texti", rows = 1, cols = 30, "")))
)),
tabPanel("About", includeMarkdown("README.md"))
))
))