似乎是反应性封装变量,所以推文里面的变量无法访问......那么如何修复呢?或者不使用被动反应?
csv文件tweets_davin并且只使用两者进行2次上传
ui.r
library(shiny)
shinyUI(fluidPage(
titlePanel("Text Mining"),
br(),br(),
sidebarLayout(
sidebarPanel(
h2("Davin", align = "center", style = "color:blue"),
fileInput('file1', 'Choose CSV File', accept=c('.csv')),
fileInput('file2', 'Choose CSV File', accept=c('.csv')),
tags$hr()
),
mainPanel(
plotOutput("ditribusi")
))))
server.r
library(shiny)
shinyServer(function(input, output) {
library(lubridate)
library(ggplot2)
library(dplyr)
library(readr)
tweets_1 <- reactive({
req(input$file1)
read.csv(input$file1$datapath)
})
tweets_2 <- reactive({
req(input$file2)
read.csv(input$file1$datapath)
})
tweets <- reactive (
as(bind_rows(tweets_1 %>%
mutate(person = "satu"),
tweets_2 %>%
mutate(person = "dua")) %>%
mutate(timestamp = ymd_hms(timestamp))))
output$ditribusi <- renderPlot(
ggplot(tweets, aes(x = timestamp, fill = person)) +
geom_histogram(alpha = 0.5, position = "identity", bins = 20)
)
})
RStudio中的错误
警告:错误:ggplot2不知道如何处理类被动的数据
堆栈跟踪(最里面的第一个):
105: fortify.default
104: fortify
103: structure
102: ggplot.data.frame
101: ggplot.default
100: ggplot
99: renderPlot
89: <reactive:plotObj>
78: plotObj
77: origRenderFunc
76: output$ditribusi
1: runApp
答案 0 :(得分:2)
reactive
返回反应式表达式,而不是值。基本上它意味着可以在反应上下文(内部函数,如reactive
,render*
等)中访问它的当前值并调用它。我认为应该使用以下代码解决这个特殊问题:
shinyServer(function(input, output) {
library(lubridate)
library(ggplot2)
library(dplyr)
library(readr)
tweets_1 <- reactive({
req(input$file1)
read.csv(input$file1$datapath)
})
tweets_2 <- reactive({
req(input$file2)
read.csv(input$file1$datapath)
})
tweets <- reactive(
bind_rows(tweets_1() %>%
mutate(person = "satu"),
tweets_2() %>%
mutate(person = "dua")) %>%
mutate(timestamp = ymd_hms(timestamp))
)
output$ditribusi <- renderPlot(
ggplot(tweets(), aes(x = timestamp, fill = person)) +
geom_histogram(alpha = 0.5, position = "identity", bins = 20)
)
})
有关使用反应式表达式的更多信息,您可以转到https://shiny.rstudio.com/tutorial/lesson6/