所以我的闪亮应用程序应该将一些文本作为输入,然后将一个单词文本作为输出。但显然我得到错误 - “参数不是字符向量”。这些是我的代码:
app.R
library(shiny)
server <- function(input, output) {
text1 <- eventReactive(input$actionButton,{
getPrediction(input$caption)
})
output$text1 <- renderUI({
text1()
})
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textAreaInput(inputId="caption", label="Put your text here", width="100%", height="400px", value="", placeholder = "Placeholder"),
actionButton("actionButton", label = "Submit")
),
mainPanel(
h3("Name"),
textOutput("text1")
)
)
)
shinyApp(ui = ui, server = server)
helper.R
library(doMC)
registerDoMC(cores=detectCores())
getPrediction <- function(ptest){
corpus <- Corpus(VectorSource(ptest))
corpus.clean <- corpus %>%
tm_map(content_transformer(tolower)) %>%
tm_map(removePunctuation) %>%
tm_map(removeNumbers) %>%
tm_map(removeWords, stopwords(kind="en")) %>%
tm_map(stripWhitespace)
corpus.clean.test <- corpus.clean
fivefreq <- findFreqTerms(dtm.train, 5)
dtm.test.nb <- DocumentTermMatrix(corpus.clean.test, control=list(dictionary = fivefreq))
convert_count <- function(x) {
y <- ifelse(x > 0, 1,0)
y <- factor(y, levels=c(0,1), labels=c("No", "Yes"))
y
}
testNB <- apply(dtm.test.nb, 2, convert_count)
pred <- predict(classifier, newdata=testNB)
pred
}
如何将预测打印为输出?
答案 0 :(得分:0)
您的服务器代码存在一些问题。
req(input$caption)
语句,以防止Shiny在未初始化值上运行数据renderUI
呈现文字。这是为了在服务器中动态创建输入控件,然后在UI部分中使用。renderText
。 renderPrint
。如果您在UI端与verbatimPrintOutput
配对,则可以在UI中看到所有打印状态,这些规则可以帮助您进行大量调试。以下是更改的代码 - 使用verbatimPrintOutput
进行调试:
getPrediction <- function(ptest){
print(sprintf("getPrediction - ptest:%s",ptest))
corpus <- Corpus(VectorSource(ptest))
corpus.clean <- corpus %>%
tm_map(content_transformer(tolower)) %>%
tm_map(removePunctuation) %>%
tm_map(removeNumbers) %>%
tm_map(removeWords, stopwords(kind="en")) %>%
tm_map(stripWhitespace)
corpus.clean.test <- corpus.clean
dtm.test.nb <- DocumentTermMatrix(corpus.clean.test)
convert_count <- function(x) {
y <- ifelse(x > 0, 1,0)
y <- factor(y, levels=c(0,1), labels=c("No", "Yes"))
y
}
testNB <- apply(dtm.test.nb, 2, convert_count)
pred <- predict(classifier, newdata=as.matrix(testNB))
pred
}
# app.R
library(shiny)
server <- function(input, output) {
text1 <- eventReactive(input$actionButton,{
req(input$caption)
getPrediction(input$caption)
})
output$text1 <- renderPrint({
text1()
})
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textAreaInput(inputId="caption", label="Put your text here", width="100%", height="400px", value="", placeholder = "Placeholder"),
actionButton("actionButton", label = "Submit")
),
mainPanel(
h3("Name"),
verbatimTextOutput("text1")
)
)
)