R Shiny Submit按钮不向服务器提供输入

时间:2018-03-28 06:25:00

标签: r shiny

我在R shiny中创建了一个应用程序,其中包含以下数据框作为输入

    DF <-
           variable   estimatesum
 1              get   0.2468349025
 2              new   0.1676732365
 3              buy   0.1028425291
 4            first   0.0823390287
 5            cover   0.0408254469
 6           perfect  -0.0299153527

这些是来自术语文档矩阵的一组单词。我创建了一个数据框,其中包含一系列与之相关的百分比。接下来我创建一个ui和服务器来处理文本到服务器并产生结果

    ui<- fluidPage(
   textInput("message", label = "message",placeholder = 
          "'"),actionButton("do", "Click Me"),
    numericInput("size", label = "size", value = 
             c(10000000)),
   dateInput("Date",label = "Date"),
   mainPanel(verbatimTextOutput("Output1")))

接下来我正在创建一个服务器脚本。按下上面的actionButton,数据应输入服务器,输出应显示在主面板中

   server<-function(input, output, session) {
     text2<-reactive({input$message
     mycorpus2<-Corpus(VectorSource(text2))
     mycorpus2<-tm_map(mycorpus2, tolower)
    mycorpus2<-tm_map(mycorpus2, removeNumbers)
    mycorpus2<-tm_map(mycorpus2, removeWords, c(stopwords("english")))
    dtm2<-TermDocumentMatrix(mycorpus2)
    m2<-as.matrix(dtm2)
    v2 <- sort(rowSums(m2),decreasing=TRUE)
    d2 <- data.frame(word = names(v2),freq=v2)
    outputtable<-DF4[DF4$variable%in%d2$word,]
    return(output)

     })
   output$Output1=renderPrint({sum(outputtable$estimatesum)})}


   shinyApp(ui, server)

我反复收到以下错误。

 object 'outputtable' not found.

来自输入的数据似乎没有被送入服务器。我无法识别错误并在此处请求帮助。会感恩的。我是r闪亮的新手

1 个答案:

答案 0 :(得分:1)

这有帮助吗?

server <- function(input, output) {
  text2 <- reactive({
    mycorpus2 <- Corpus(VectorSource(input$message))
    mycorpus2 <- tm_map(mycorpus2, tolower)
    mycorpus2 <- tm_map(mycorpus2, removeNumbers)
    mycorpus2 <- tm_map(mycorpus2, removeWords, c(stopwords("english")))
    dtm2 <- TermDocumentMatrix(mycorpus2)
    m2 <- as.matrix(dtm2)
    v2 <- sort(rowSums(m2), decreasing=TRUE)
    d2 <- data.frame(word = names(v2), freq = v2)
    outputtable <- DF[DF$variable %in% d2$word, ]
  })
  output$Output1 <- renderPrint({sum(text2()$estimatesum)})}

我对您的服务器脚本进行了细微更改:

  • renderPrint中,您需要致电reactive函数,即text2()
  • Corpus中,您需要传递input$message


<强>更新

OP参考的完整代码:

df <- structure(list(variable = structure(c(4L, 5L, 1L, 3L, 2L, 6L), .Label = c("buy", 
                                                                                "cover", "first", "get", "new", "perfect"), class = "factor"), 
                     estimatesum = c(0.2468349025, 0.1676732365, 0.1028425291, 
                                     0.0823390287, 0.0408254469, -0.0299153527)), .Names = c("variable", 
                                                                                             "estimatesum"), class = "data.frame", row.names = c("1", "2", 
                                                                                                                                                 "3", "4", "5", "6"))

ui<- fluidPage(
  textInput("message", label = "message",placeholder = 
              "'"),actionButton("do", "Click Me"),
  numericInput("size", label = "size", value = 
                 c(10000000)),
  dateInput("Date",label = "Date"),
  mainPanel(verbatimTextOutput("Output1")))

server <- function(input, output) {
  text2 <- reactive({
    mycorpus2 <- Corpus(VectorSource(input$message))
    mycorpus2 <- tm_map(mycorpus2, tolower)
    mycorpus2 <- tm_map(mycorpus2, removeNumbers)
    mycorpus2 <- tm_map(mycorpus2, removeWords, c(stopwords("english")))
    dtm2 <- TermDocumentMatrix(mycorpus2)
    m2 <- as.matrix(dtm2)
    v2 <- sort(rowSums(m2), decreasing=TRUE)
    d2 <- data.frame(word = names(v2), freq = v2)
    outputtable <- df[df$variable %in% d2$word, ]
  })
  output$Output1 <- renderPrint({sum(text2()$estimatesum)})
  }

shinyApp(ui, server)