R 错误:无法强制类型';环境';到类型为'的向量;字符';

R 错误:无法强制类型';环境';到类型为'的向量;字符';,r,shiny,shiny-server,R,Shiny,Shiny Server,我正在R中开发一个闪亮的应用程序来预测键入文本的下一个单词。每次运行应用程序时,我都会收到此错误 倾听 警告:as.character中出错:无法将类型“environment”强制为的向量 键入“字符” 堆栈跟踪(最里面的第一个): 1:runApp 错误:无法将“环境”类型强制为“字符”类型的向量 我尝试了一切我似乎不理解的问题 suppressWarnings(library(shiny)) shinyUI(fluidPage( # Application ti

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

倾听 警告:as.character中出错:无法将类型“environment”强制为的向量

键入“字符”

堆栈跟踪(最里面的第一个): 1:runApp


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

我尝试了一切我似乎不理解的问题

    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)
suppressWarnings(库(闪亮))
shinyUI(fluidPage)(
#申请名称
navbarPage(“Coursera Word Prediction最终项目”,
选项卡面板(“主页”),
导航栏菜单(“方法”,
tabPanel(“说明”,p(“此应用程序使用ngram回退
预测句子中下一个单词的模型。“))
)),
#边栏布局
侧边栏布局(
侧栏面板(
textInput(“句子”,“继续下面的句子”,value=“此
是由于,
滑块输入(“obs”,“最大预测:”,
最小值=0,最大值=30,值=10
)
),
主面板(
h4(“判决”),
逐字输出(“文本”),
h4(“预测”),
逐字输出(“预测”)
)
)
)
)

pred_words
shinyUI
shinyServer
是来自shinny的函数。您希望将实际的ui和服务器函数传递到
shinyApp()
,如:

library(shiny)

ui <- fluidPage(

)

server <- function(input, output) {

}

shinyApp(ui = ui, server = server)
库(闪亮)

ui错误:无法将类型“environment”强制为类型“character”的向量,这在我们使用observe()而不是服务器函数内部的反应时最为常见

非常感谢,它很有效。现在我只需要修复我遇到的新错误。我真的很感激