r - 错误: cannot coerce type 'environment' to vector of type 'character'

标签 r shiny shiny-server

我正在 R 中开发一个 Shiny 应用程序来预测键入文本的下一个单词。每次运行该应用程序时,我都会收到此错误

收听http://127.0.0.1:5714 警告:as.character 中的错误:无法将“环境”类型强制转换为

的向量

输入“字符”

堆栈跟踪(最里面的第一个): 1:运行应用程序

错误:无法将“环境”类型强制为“字符”类型的向量

我已经尝试了所有我似乎无法理解的问题

    suppressWarnings(library(shiny))

    shinyUI(fluidPage(

    # Application title
    navbarPage("Coursera Word Predection Final Project",
         tabPanel("Home"),
         navbarMenu("Method",
                    tabPanel("Description", p("This app uses a ngram backoff 
    model to predict the next word in a sentence."))
                    )),

    # Sidebar layout
    sidebarLayout(

    sidebarPanel(
    textInput("sentence", "Continue the sentence here below", value = "this 
    is a result of the"),
   sliderInput("obs", "maximum predictions:",
              min = 0, max = 30, value = 10
   )

   ),

  mainPanel(
  h4("Sentence"),
  verbatimTextOutput("text"),

  h4("Prediction"),
  verbatimTextOutput("prediction")
  )
  )
  )
  )


 pred_words <- function(sentence, n = 10){
 sentence <- removeNumbers(sentence)
 sentence <- removePunctuation(sentence)
 sentence <- tolower(sentence)
 words <- unlist(strsplit(sentence, split = " " ))
 words <- tail(words, 5)
 word1 <- words[1];word2 <- words[2];word3 <- words[3];word4 <- 
 words[4];word5 <- words[5];
 datasub <- data.table()
 if (nrow(datasub)==0 & !is.na(word5)) {
  if(nrow(datasub) == 0) datasub <- subset(ngram6, w1==word1 & w2==word2 & 
  w3==word3 & w4==word4 & w5==word5)
  if(nrow(datasub) == 0) datasub <- subset(ngram5, w1==word2 & w2==word3 & 
  w3==word4 & w4==word5)
  if(nrow(datasub) == 0) datasub <- subset(ngram4, w1==word3 & w2==word4 & 
  w3==word5)
  if(nrow(datasub) == 0) datasub <- subset(ngram3, w1==word4 & w2==word5)
  if(nrow(datasub) == 0) datasub <- subset(ngram2, w1==word5)
   }

  if (nrow(datasub)==0 & !is.na(word4)) {
  if(nrow(datasub) == 0) datasub <- subset(ngram5, w1==word1 & w2==word2 & 
  w3==word3 & w4==word4)
  if(nrow(datasub) == 0) datasub <- subset(ngram4, w1==word2 & w2==word3 & 
  w3==word4)
  if(nrow(datasub) == 0) datasub <- subset(ngram3, w1==word3 & w2==word4)
  if(nrow(datasub) == 0) datasub <- subset(ngram2, w1==word4)
  }

  if (nrow(datasub)==0 & !is.na(word3)) {
  if(nrow(datasub) == 0) datasub <- subset(ngram4, w1==word1 & w2==word2 & 
  w3==word3)
  if(nrow(datasub) == 0) datasub <- subset(ngram3, w1==word2 & w2==word3)
  if(nrow(datasub) == 0) datasub <- subset(ngram2, w1==word3)
  }

  if (nrow(datasub)==0 & !is.na(word2)) {
  if(nrow(datasub) == 0) datasub <- subset(ngram3, w1==word1 & w2==word2)
  if(nrow(datasub) == 0) datasub <- subset(ngram2, w1==word2)
  }

  if (nrow(datasub)==0 & !is.na(word1)) {
  if(nrow(datasub) == 0) datasub <- subset(ngram2, w1==word1)
  if(nrow(datasub) == 0) datasub <- head(ngram1)
  }

  if(nrow(datasub) > 0){
  }

  }


  # Define server logic for the Word Prediction application
  shinyServer(function(input, output) {
reactive({
  pred_words(input$sentence, input$obs);
})
output$prediction <- renderPrint({
  ds <- data_prediction()
  if(nrow(ds)>0) {
    head(subset(ds, freq==max(ds$freq))[,ncol(ds)-1],3)
    cat( 
      paste( head(ds[,ncol(ds)-1]), collapse=', ' )
    )  
  }
})
})

 # Run the application 
 shinyApp(ui = shinyUI, server = shinyServer)

最佳答案

shinyUIshinyServer 是 Shiny 的函数。您想要将实际的 ui 和服务器函数传递给 shinyApp(),例如:

library(shiny)

ui <- fluidPage(

)

server <- function(input, output) {

}

shinyApp(ui = ui, server = server)

关于r - 错误: cannot coerce type 'environment' to vector of type 'character' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47482757/

相关文章:

r - 在 R 中按组汇总数据帧的所有数字列

r - 使用 gcIntermediate 在 R 传单/Shiny 中映射跨越日期变更线的最短飞行路径

r - 如何在 R 中使用 plotly

r - 如何使用 dplyr 汇总与组不匹配的值

r - R 中的 list.files() 并检查哪些是不同的/新的

r - 通过操作按钮打开一个新的空 Shiny 用户界面

r - 防止导航栏在flexdashboard R中重叠内容

ssl - 为在 CentOS 上运行的 Shiny 服务器创建 SSL 证书

r - navbarPage、Shiny R 中不同 tabPanel 上的相同输入

r - 如何测试字符数据框中的数值并将其转换为数值?