shinyapp 中的 plotly graph 对输入没有反应
plotly graph in shinyapp isn't reactive to input
我正在尝试设置一个带有对输入有反应的绘图线图的 shinyapp。我正在为图表使用 plot_ly,我理想的图表将具有与 checkboxGroupInput 中 select 一样多的行。我的问题是图表没有对输入做出反应,它正在绘制所有选择或者不会绘制超过一个,但我无法按照我想要的方式对其进行编码。它与 ggplot 一起使用,但由于其他原因我不得不使用 plotly,所以我想坚持使用它。我试图过滤我的数据或创建一个反应变量 ->col(),但这没有用。另一个问题是带有日期变量的 sliderInput 不起作用(或者图表没有做出相应的反应)。
如有任何建议或建议,将不胜感激!
到目前为止,这是我的代码:
library(shiny)
library(shinydashboard)
library(plotly)
# data frame
land <- c("BW", "BW", "BW",
"MV", "MV", "MV", "MV",
"SH", "SH", "SH")
total <- c(1, 5, 3,
7, 4, 2, 4,
7, 2, 6)
gewalt <- c(1, 1, 2,
2, 2, 0, 1,
4, 0, 3)
sonst <- c(0, 4, 1,
5, 2, 2, 3,
3, 2, 3)
date <- c("2001-12-31", "2003-06-30", "2006-11-30",
"2001-12-31", "2006-11-30", "2008-09-30", "2010-02-28",
"2001-12-31", "2003-06-30", "2006-11-30")
data <- data.frame(cbind(land, total, gewalt, sonst, date))
data$land <- as.factor(data$land)
data$total <- as.numeric(data$total)
data$gewalt <- as.numeric(data$gewalt)
data$sonst <- as.numeric(data$sonst)
data$date <- as.Date(data$date)
# user interface
ui <- dashboardPage(
dashboardBody(
fluidRow(
box(
selectInput("art3", "select what kind of crime:",
choices = c("Insgesamt"= "total",
"Gewalttaten"= "gewalt",
"Straftaten"= "sonst")),
sliderInput("time3", "select the time frame",
min(data$date), max(data$date),
value = c(min(data$date), max(data$date)), timeFormat = "%b %Y"),
checkboxGroupInput("bl3", "select the state:",
choices= levels(data$land)),
width = 4),
box(plotlyOutput("plot3"),
width = 8)
)))
# server
server <- function(input, output, session) {
y <- reactive({data[,input$art3]})
# col <- reactive({data[input$bl3,]}) # i tried to make the land-variable reactive but that didn't work
output$plot3 <- renderPlotly({
validate(
need(input$bl3,
message = "select something first."
))
data_filtered <- filter(data, date >= input$time3[1],
date <= input$time3[2])
plot_ly(data_filtered,
x=~date, color = ~input$bl3, mode= "lines+markers") %>%
add_lines(y= y())
})
}
shinyApp(ui, server)
使用此代码,如果我 select 有多个选择,我会收到错误消息:“警告:错误:Tibble 列必须具有兼容的大小”。
library(dplyr)
library(shiny)
library(shinydashboard)
library(plotly)
# data frame
land <- c("BW", "BW", "BW",
"MV", "MV", "MV", "MV",
"SH", "SH", "SH")
total <- c(1, 5, 3,
7, 4, 2, 4,
7, 2, 6)
gewalt <- c(1, 1, 2,
2, 2, 0, 1,
4, 0, 3)
sonst <- c(0, 4, 1,
5, 2, 2, 3,
3, 2, 3)
date <- c("2001-12-31", "2003-06-30", "2006-11-30",
"2001-12-31", "2006-11-30", "2008-09-30", "2010-02-28",
"2001-12-31", "2003-06-30", "2006-11-30")
data <- data.frame(cbind(land, total, gewalt, sonst, date))
data$land <- as.factor(data$land)
data$total <- as.numeric(data$total)
data$gewalt <- as.numeric(data$gewalt)
data$sonst <- as.numeric(data$sonst)
data$date <- as.Date(data$date)
# user interface
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
fluidRow(
box(
selectInput("art3", "select what kind of crime:",
choices = c("Insgesamt"= "total",
"Gewalttaten"= "gewalt",
"Straftaten"= "sonst")),
sliderInput("time3", "select the time frame",
min(data$date), max(data$date),
value = c(min(data$date), max(data$date)), timeFormat = "%b %Y"),
checkboxGroupInput("bl3", "select the state:",
choices= levels(data$land)),
width = 4),
box(plotlyOutput("plot3"),
width = 8)
)))
# server
server <- function(input, output, session) {
output$plot3 <- renderPlotly({
validate(
need(input$bl3,
message = "select something first."
))
data_filtered <- filter(
data,
date >= input$time3[1],
date <= input$time3[2],
land %in% input$bl3) # filter land as well
plot_ly(data_filtered,
x=~date, color = ~land, mode= "lines+markers") %>%
add_lines(y= ~ .data[[input$art3]])
})
}
shinyApp(ui, server)
我正在尝试设置一个带有对输入有反应的绘图线图的 shinyapp。我正在为图表使用 plot_ly,我理想的图表将具有与 checkboxGroupInput 中 select 一样多的行。我的问题是图表没有对输入做出反应,它正在绘制所有选择或者不会绘制超过一个,但我无法按照我想要的方式对其进行编码。它与 ggplot 一起使用,但由于其他原因我不得不使用 plotly,所以我想坚持使用它。我试图过滤我的数据或创建一个反应变量 ->col(),但这没有用。另一个问题是带有日期变量的 sliderInput 不起作用(或者图表没有做出相应的反应)。
如有任何建议或建议,将不胜感激!
到目前为止,这是我的代码:
library(shiny)
library(shinydashboard)
library(plotly)
# data frame
land <- c("BW", "BW", "BW",
"MV", "MV", "MV", "MV",
"SH", "SH", "SH")
total <- c(1, 5, 3,
7, 4, 2, 4,
7, 2, 6)
gewalt <- c(1, 1, 2,
2, 2, 0, 1,
4, 0, 3)
sonst <- c(0, 4, 1,
5, 2, 2, 3,
3, 2, 3)
date <- c("2001-12-31", "2003-06-30", "2006-11-30",
"2001-12-31", "2006-11-30", "2008-09-30", "2010-02-28",
"2001-12-31", "2003-06-30", "2006-11-30")
data <- data.frame(cbind(land, total, gewalt, sonst, date))
data$land <- as.factor(data$land)
data$total <- as.numeric(data$total)
data$gewalt <- as.numeric(data$gewalt)
data$sonst <- as.numeric(data$sonst)
data$date <- as.Date(data$date)
# user interface
ui <- dashboardPage(
dashboardBody(
fluidRow(
box(
selectInput("art3", "select what kind of crime:",
choices = c("Insgesamt"= "total",
"Gewalttaten"= "gewalt",
"Straftaten"= "sonst")),
sliderInput("time3", "select the time frame",
min(data$date), max(data$date),
value = c(min(data$date), max(data$date)), timeFormat = "%b %Y"),
checkboxGroupInput("bl3", "select the state:",
choices= levels(data$land)),
width = 4),
box(plotlyOutput("plot3"),
width = 8)
)))
# server
server <- function(input, output, session) {
y <- reactive({data[,input$art3]})
# col <- reactive({data[input$bl3,]}) # i tried to make the land-variable reactive but that didn't work
output$plot3 <- renderPlotly({
validate(
need(input$bl3,
message = "select something first."
))
data_filtered <- filter(data, date >= input$time3[1],
date <= input$time3[2])
plot_ly(data_filtered,
x=~date, color = ~input$bl3, mode= "lines+markers") %>%
add_lines(y= y())
})
}
shinyApp(ui, server)
使用此代码,如果我 select 有多个选择,我会收到错误消息:“警告:错误:Tibble 列必须具有兼容的大小”。
library(dplyr)
library(shiny)
library(shinydashboard)
library(plotly)
# data frame
land <- c("BW", "BW", "BW",
"MV", "MV", "MV", "MV",
"SH", "SH", "SH")
total <- c(1, 5, 3,
7, 4, 2, 4,
7, 2, 6)
gewalt <- c(1, 1, 2,
2, 2, 0, 1,
4, 0, 3)
sonst <- c(0, 4, 1,
5, 2, 2, 3,
3, 2, 3)
date <- c("2001-12-31", "2003-06-30", "2006-11-30",
"2001-12-31", "2006-11-30", "2008-09-30", "2010-02-28",
"2001-12-31", "2003-06-30", "2006-11-30")
data <- data.frame(cbind(land, total, gewalt, sonst, date))
data$land <- as.factor(data$land)
data$total <- as.numeric(data$total)
data$gewalt <- as.numeric(data$gewalt)
data$sonst <- as.numeric(data$sonst)
data$date <- as.Date(data$date)
# user interface
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
fluidRow(
box(
selectInput("art3", "select what kind of crime:",
choices = c("Insgesamt"= "total",
"Gewalttaten"= "gewalt",
"Straftaten"= "sonst")),
sliderInput("time3", "select the time frame",
min(data$date), max(data$date),
value = c(min(data$date), max(data$date)), timeFormat = "%b %Y"),
checkboxGroupInput("bl3", "select the state:",
choices= levels(data$land)),
width = 4),
box(plotlyOutput("plot3"),
width = 8)
)))
# server
server <- function(input, output, session) {
output$plot3 <- renderPlotly({
validate(
need(input$bl3,
message = "select something first."
))
data_filtered <- filter(
data,
date >= input$time3[1],
date <= input$time3[2],
land %in% input$bl3) # filter land as well
plot_ly(data_filtered,
x=~date, color = ~land, mode= "lines+markers") %>%
add_lines(y= ~ .data[[input$art3]])
})
}
shinyApp(ui, server)