对反应事件的数据操作

Data manipulation on a reactive event

我正在创建一个数据框,它是在用户输入搜索词时动态创建的,为此我有一个操作按钮,当 "go" 按钮为按下。

一旦完成,我需要在 DF 上执行各种数据操作,并输出不同的图表。

我正在努力理解如何在 Shiny 中进行数据操作,我在下面有一些简化的示例代码:

library(shiny)
library(sp)
library(stringr)
library(tidyr)
library(tidyverse)
library(tm)
library(ggplot2)
library(stringi)
library(plyr)
library(dplyr) 

ui <- fluidPage(

  fluidRow( 
    column( 4, titlePanel("Twitter Analytics")),
    column( 3),
    column( 4, 
            textInput("searchstring", 
                      label = "",
                      value = "")),
    column(1, 
           br(),
           actionButton("action", "go"))
  ),
  fluidRow(
    column( 12, tabsetPanel(
      tabPanel("one",
               fluidRow(
                 column(3 ),
                 column(9,  plotOutput("ttext"))
               )
      ),    
      tabPanel("two"),
      tabPanel("three")
    )
    )
  )
)

server <- function(input, output) {

  tweet <- eventReactive(input$action,{
    num <- c(1,2,3,4,50)
    text <- c("this is love love something", "this is not hate hate hate something", "@something islove  rethched this not", " Shiny is love confusing me", "this is hate also somthing difficult")
    letter<- c("a", "b", "c", "D", "e")
    tweetdf <- data.frame(num, text, letter)
  })

  tdm <- if( is.null(tweetdf) ){return()}
  else{
    tweetdf$text <- tolower(tweetdf$text)
    # tweetdf @UserName
    tweetdf$text <- gsub("@\w+", "", tweetdf$text)
    #remove punctuation
    tweetdf$text <- gsub("[[:punct:]]", "", tweetdf$text)
    #remove links
    tweetdf$text <- gsub("http\w+", "", tweetdf$text)
    # Remove tabs
    tweetdf$text <- gsub("[ |\t]{2,}", "", tweetdf$text)
    # Remove blank spaces at the beginning
    tweetdf$text <- gsub("^ ", "", tweetdf$text)
    # Remove blank spaces at the end
    corpus <- iconv(tweetdf$text, to = "ASCII")
    corpus <- Corpus(VectorSource(corpus))
    corpus <- tm_map(corpus, removePunctuation)
    corpus <- tm_map(corpus, removeNumbers)
    cleanset <- tm_map(corpus, removeWords, stopwords('english'))
    tdm <- TermDocumentMatrix(cleanset)
    tdm <- as.matrix(tdm)
    w <- rowSums(tdm)
  }

    output$ttext <- renderPlot({ 
        library(RColorBrewer)
        barplot(w)

    })
    output$wordCl <- renderPlot({
      library(wordcloud2)
      w <- data.frame(names(w), w)
      colnames(w) <- c('word', 'freq')
      wordcloud2(w,
                 color = 'random-dark',
                 size = 0.7,
                 shape = 'circle',
                 rotateRatio = 0.5,
                 minSize = 1)
    })
  }

  shinyApp(ui, server)

我一直收到 tweetdf 不存在的错误消息,这不应该存在,直到用户输入搜索词并单击 "go"

解决这个问题的最佳方法是什么,这是否是正确的选择

它告诉你 tweetdf 不存在,因为 eventReactive (tweetdf) 的结果被分配给变量 tweet,这使得 tweet 成为你实际的反应变量,其中包含 tweetdf 的结果。

您代码中的问题还在于您将经典变量与反应变量混合在一起。

您可以通过在变量末尾添加括号来访问反应变量()

这是一个工作示例:

library(shiny)
library(sp)
library(stringr)
library(tidyr)
library(tidyverse)
library(tm)
library(ggplot2)
library(stringi)
library(plyr)
library(dplyr) 
library(RColorBrewer)
library(wordcloud2)

ui <- fluidPage(

  fluidRow( 
    column( 4, titlePanel("Twitter Analytics")),
    column( 3),
    column( 4, 
            textInput("searchstring", 
                      label = "",
                      value = "")),
    column(1, 
           br(),
           actionButton("action", "go"))
  ),
  fluidRow(
    column( 12, tabsetPanel(
      tabPanel("one",
               fluidRow(
                 column(3 ),
                 column(9,  plotOutput("ttext"))
               )
               # ,fluidRow(wordcloud2Output("wordCl"))
      ),    
      tabPanel("two"),
      tabPanel("three")
    )
    )
  )
)

server <- function(input, output) {

  w <- eventReactive(input$action,{
    num <- c(1,2,3,4,50)
    text <- c("this is love love something", "this is not hate hate hate something", "@something islove  rethched this not", " Shiny is love confusing me", "this is hate also somthing difficult")
    letter<- c("a", "b", "c", "D", "e")
    tweetdf <- data.frame(num, text, letter)

    tdm <- if( is.null(tweetdf) ){return()} ## should not use return here as this is not a function
    else{
      print(tweetdf)
      tweetdf$text <- tolower(tweetdf$text)
      # tweetdf @UserName
      tweetdf$text <- gsub("@\w+", "", tweetdf$text)
      #remove punctuation
      tweetdf$text <- gsub("[[:punct:]]", "", tweetdf$text)
      #remove links
      tweetdf$text <- gsub("http\w+", "", tweetdf$text)
      # Remove tabs
      tweetdf$text <- gsub("[ |\t]{2,}", "", tweetdf$text)
      # Remove blank spaces at the beginning
      tweetdf$text <- gsub("^ ", "", tweetdf$text)
      # Remove blank spaces at the end
      corpus <- iconv(tweetdf$text, to = "ASCII")
      corpus <- Corpus(VectorSource(corpus))
      corpus <- tm_map(corpus, removePunctuation)
      corpus <- tm_map(corpus, removeNumbers)
      cleanset <- tm_map(corpus, removeWords, stopwords('english'))
      tdm <- TermDocumentMatrix(cleanset)
      tdm <- as.matrix(tdm)
      w <- rowSums(tdm)
    }

  })

  output$ttext <- renderPlot({ 
    barplot(w())
  })


  output$wordCl <- renderWordcloud2({
    w <- data.frame(names(w()), w())

    colnames(w) <- c('word', 'freq')

    wordcloud2(w,
               color = 'random-dark',
               size = 0.7,
               shape = 'circle',
               rotateRatio = 0.5,
               minSize = 1)
  })
}

shinyApp(ui, server)