r&x27;s文本区域输入返回?
所以我的闪亮应用程序应该将一些文本作为输入,然后给出一个单词的文本作为输出。但显然我得到了错误——“参数不是字符向量”。这是我的密码: app.Rr&x27;s文本区域输入返回?,r,rstudio,shiny,shiny-server,R,Rstudio,Shiny,Shiny Server,所以我的闪亮应用程序应该将一些文本作为输入,然后给出一个单词的文本作为输出。但显然我得到了错误——“参数不是字符向量”。这是我的密码: app.R library(shiny) server <- function(input, output) { text1 <- eventReactive(input$actionButton,{ getPrediction(input$caption) }) output$text1 <- renderUI({
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)
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)
语句,以防止在具有未初始化值的数据上运行
- 您正在使用
renderUI
呈现文本。这是为了在服务器中动态创建输入控件,然后在UI部分中使用这些控件
- 而不是使用
renderText
李>
<>你也可以考虑使用<代码> RealDebug < /代码>。如果将其与UI端的逐字打印输出
配对,您可以看到UI中的所有打印语句,这对调试非常有帮助
以下是更改后的代码-使用逐字打印输出
进行调试:
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")
)
)
)
getPrediction%
tm_地图(删除编号)%>%
tm_地图(删除单词、停止单词(kind=“en”))%>%
tm_映射(条带空白)
corpus.clean.test服务器代码中有几个问题
- 它有助于在其中放入
req(input$caption)
语句,以防止在具有未初始化值的数据上运行
- 您正在使用
renderUI
呈现文本。这是为了在服务器中动态创建输入控件,然后在UI部分中使用这些控件
- 而不是使用
renderText
李>
<>你也可以考虑使用<代码> RealDebug < /代码>。如果将其与UI端的逐字打印输出
配对,您可以看到UI中的所有打印语句,这对调试非常有帮助
以下是更改后的代码-使用逐字打印输出
进行调试:
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")
)
)
)
getPrediction%
tm_地图(删除编号)%>%
tm_地图(删除单词、停止单词(kind=“en”))%>%
tm_映射(条带空白)
返回一个字符串。你可以通过打印报表来确定。很好,我想,你认为错误是从哪里来的?我试着运行它,但我看不出你在哪里定义dtm.train
,它正在从我这里消失。哦,对了,我删除了它。现在我得到一个新的错误:$运算符对原子向量无效。在这一点上,我真的不知道。我也是第一次使用闪亮。这就是全部代码:你能运行它吗?谢谢你的帮助。多酷啊。我还以为它只适用于Javascript。现在将查看。它返回一个字符串。你可以通过打印报表来确定。很好,我想,你认为错误是从哪里来的?我试着运行它,但我看不出你在哪里定义dtm.train
,它正在从我这里消失。哦,对了,我删除了它。现在我得到一个新的错误:$运算符对原子向量无效。在这一点上,我真的不知道。我也是第一次使用闪亮。这就是全部代码:你能运行它吗?谢谢你的帮助。多酷啊。我还以为它只适用于Javascript。我现在就去看看,明白了。谢谢!知道了。谢谢!