2017-05-25 45 views
1

所以我的闪亮的应用程序应该把一些文本作为输入,然后给出一个单词文本作为输出。但显然我得到错误 - “参数不是一个字符向量”。这是我的代码:r shiny的textAreaInput返回什么类型?

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 
} 

我能做些什么,以打印出预测的输出?

感谢 enter image description here

+0

它返回一个字符串。正如你可以用打印语句确定的那样。我认为正常工作,你认为这是错误来自哪里? –

+1

我试图运行它,但我没有看到你定义'dtm.train'的地方,它正在死亡那里形成我。 –

+0

哦,对了,我删除了。现在我得到一个新的错误:$运算符对原子向量无效。在这一点上,我真的不知道。我也第一次使用闪亮。这是整个代码:http://www.r-fiddle.org/#/fiddle?id=uq2FrPgE&version=2 你可以运行它吗?谢谢你的帮助。 – 0x1

回答

1

有一对夫妇在你的服务器代码的问题。

  • 它有助于把req(input$caption)声明在那里保持闪亮从初始化值
  • 您与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") 
    ) 
) 
) 

和输出:enter image description here

+0

明白了。非常感谢! – 0x1