r shiny 的 textAreaInput returns 是什么类型?
What type does the r shiny's textAreaInput returns?
所以我闪亮的应用程序应该将一些文本作为输入,然后给出一个单词文本作为输出。但显然我得到了错误-"argument is not a character vector"。这些是我的代码:
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
}
如何将预测结果打印出来?
谢谢
您的服务器代码中存在一些问题。
- 它有助于在其中放置
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")
)
)
)
并且输出:
所以我闪亮的应用程序应该将一些文本作为输入,然后给出一个单词文本作为输出。但显然我得到了错误-"argument is not a character vector"。这些是我的代码:
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
}
如何将预测结果打印出来?
谢谢
您的服务器代码中存在一些问题。
- 它有助于在其中放置
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")
)
)
)
并且输出: