shiny - 对反应事件的数据操作
问题描述
我正在创建一个数据框,它是在用户输入搜索词时动态创建的,为此我有一个操作按钮,并且在按下“开始”按钮时创建了数据框。
完成此操作后,我需要对 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 不存在的错误消息,这不应该存在,直到用户输入搜索词并单击“开始”
解决这个问题的最佳方法是什么,这甚至是正确的地方吗
解决方案
它告诉你 tweetdf 不存在,因为 eventReactive (tweetdf) 的结果被分配给变量 tweet,这使得 tweet 成为你的实际反应变量,并带有 tweetdf 的结果。
此外,您的代码中的问题是您将经典变量与反应变量混合在一起。
您可以通过在 variable() 末尾添加括号来访问反应变量
这是一个工作示例:
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)
推荐阅读
- php - 在 Wordpress 中将自定义字符串添加到 URL
- git - 是否可以将来自 AWS Codecommit 的 Git 存储库“分叉”到 GitHub 中?
- php - 根据复选框(选中/未选中)获取mysql记录并导出到ms excel
- javascript - 使用php将ajax数据发送到mysql
- android - 无法在 chrome 远程调试中检查 android 应用程序
- r - 根据两个条件过滤行
- php - 将 HTML 表单连接到数据库
- javascript - 复选框上的 onChange 不会在反应中触发
- c# - MS Bot Framework 发送 EventActivity 服务器端
- php - 得到一个空白图像文件我做错了吗