global.R代码:
library(shiny)
library(ggplot2)
data_1 <- read.csv(file = "data_1.csv")
data_2 <- read.csv(file = "data_2.csv")
data_3 <- read.csv(file = "data_3.csv")
data_1$date <- as.Date(data_1$date)
data_2$date <- as.Date(data_2$date)
data_3$date <- as.Date(data_3$date)
total_data <- list(data_1, data_2, data_3)
date_days_1 <- split(data_1, format(as.Date(data_1$date), "%Y-%m"))
date_days_2 <- split(data_2, format(as.Date(data_2$date), "%Y-%m"))
date_days_3 <- split(data_3, format(as.Date(data_3$date), "%Y-%m"))
server.R代码:
itemInput <- reactive({
switch(input$item,
"data1" = "data_1",
"data2" = "data_2",
"data3" = "data_3")
})
productInput <- reactive({
switch(input$select,
"days" = "total_data",
"week" = "total_data",
"season" = "total_data")
})
output$splitDate <- renderUI({
if (input$select == "days"){
selectInput("date_day", "select a month", names(split(data_1, format(as.Date(data_1$date), "%Y-%m"))))
} else {
return()
}
})
output$chartPlot <- renderPlot({
if(input$select == "days") {
p <- ggplot(data=data_1,aes(x=date,y=point, group = 1)) + scale_color_manual(values=c("red", "green", "blue")) + xlab("Date") + ylab("Number of people")
if(input$item == "data1"){
df <- data.frame(date_days_1[[input$date_day]])
p <- p +geom_line(data = df, size = 1)
} else if (input$item == "data2"){
df <- data.frame(date_days_2[[input$date_day]])
p <- p + geom_line(data = df, size = 1)
} else if (input$item == "data3"){
df <- data.frame(date_days_3[[input$date_day]])
p <- p + geom_line(data = df, size = 1)
} else {
return()
}
} else {
return()
}
print(p)
#plot(as.Date(df$date),df$point)
})
ui.R代码:
shinyUI(fluidPage(
navbarPage("Plot",
tabPanel("lineChart",
sidebarLayout(
sidebarPanel(
#add choices
selectizeInput("item", "select item:",
choices = c("data1", "data2", "data3")
,multiple = TRUE),
selectInput("select", "select time:",
choices = c("days", "week", "season")),
uiOutput("splitDate")
),
mainPanel(
#plotOutput("chartPlot")
plotOutput("chartPlot",width=800,height=600)
)
)
)
)
))
我想在剧情中选择很多数据。
我在绘图中选择了两个数据,但出现此错误消息:
if中的警告(输入$ item ==&#34; data1&#34;){: 条件的长度> 1,只使用第一个元素
如何解决?
答案 0 :(得分:1)
好的,请查看下面的答案
rm(list = ls())
library(shiny)
library(ggplot2)
data_1 <- read.csv(file = "data_1.csv")
data_2 <- read.csv(file = "data_2.csv")
data_3 <- read.csv(file = "data_3.csv")
data_1$date <- as.Date(data_1$date)
data_2$date <- as.Date(data_2$date)
data_3$date <- as.Date(data_3$date)
total_data <- list(data_1, data_2, data_3)
date_days_1 <- split(data_1, format(as.Date(data_1$date), "%Y-%m"))
date_days_2 <- split(data_2, format(as.Date(data_2$date), "%Y-%m"))
date_days_3 <- split(data_3, format(as.Date(data_3$date), "%Y-%m"))
ui <- fluidPage(
navbarPage("Plot",
tabPanel("lineChart",
sidebarLayout(
sidebarPanel(
#add choices
selectizeInput("item", "select item:",choices = c("data1", "data2", "data3"),multiple = TRUE),
selectInput("select", "select time:",choices = c("days", "week", "season")),
uiOutput("splitDate")
),
mainPanel(
#plotOutput("chartPlot")
plotOutput("chartPlot",width=800,height=600)
)
)
)
)
)
server <- function(input, output, session) {
itemInput <- reactive({
switch(input$item,"data1" = "data_1","data2" = "data_2","data3" = "data_3")
})
productInput <- reactive({
switch(input$select,
"days" = "total_data",
"week" = "total_data",
"season" = "total_data")
})
output$splitDate <- renderUI({
if (input$select == "days"){
selectInput("date_day", "select a month", names(split(data_1, format(as.Date(data_1$date), "%Y-%m"))))
} else {
return()
}
})
output$chartPlot <- renderPlot({
if(is.null(input$item)){return(NULL)}
if(input$select %in% "days") {
p <- ggplot(data=data_1,aes(x=date,y=point, group = 1)) + scale_color_manual(values=c("red", "green", "blue")) + xlab("Date") + ylab("Number of people")
df <- data.frame(date_days_1[[input$date_day]])
df2 <- data.frame(date_days_2[[input$date_day]])
df3 <- data.frame(date_days_3[[input$date_day]])
if(any(input$item %in% "data1")){
p <- p +geom_line(data = df, size = 1, color='green')
}
if (any(input$item %in% "data2")){
p <- p +geom_line(data = df2, size = 1)
}
if (any(input$item %in% "data3")){
p <- p +geom_line(data = df3, size = 1, color='red')
}
} else {
return(NULL)
}
print(p)
})
}
runApp(list(ui = ui, server = server))