r - shinyapp 中的绘图对输入没有反应
问题描述
我正在尝试使用对输入做出反应的曲线图来设置闪亮应用程序。我正在使用 plot_ly 作为图表,我的理想图表将具有与 checkboxGroupInput 中选择的一样多的行。我的问题是图表没有对输入做出反应,它正在绘制所有选择或者不会绘制多个选项,但我无法弄清楚以我想要的方式对其进行编码。它已与 ggplot 一起使用,但由于其他原因我必须使用 plotly,所以我想坚持下去。我试图过滤我的数据或创建一个反应变量 ->col(),但这没有用。另一个问题是带有日期变量的滑块输入不起作用(或者图形没有相应地做出反应)。
如果您有任何建议或提示,我将非常感谢!
到目前为止,这是我的代码:
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)
使用此代码,如果我选择多个选项,则会收到错误消息:“警告:错误: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)
推荐阅读
- latex - 为什么在背面我错过了表格中的垂直边缘?
- jenkins - Jenkins 在运行作业之前运行 gcloud 命令
- mongodb - 如何关闭虚拟机的网卡
- ms-access - word MailMerge(来自 MS Access 数据源)动态表
- kibana - Kibana 7.7.1 - 保存的搜索不包括搜索字符串引号;用 %22 转义不起作用
- python - 在 Qt Designer (PyQt5) 生成的 html 中插入 Python 变量
- scala - 如何将 Protobuf 数据从 Flink 转发到 Kafka 和 stdout?
- crash - swiftui 这个崩溃报告是什么意思?
- wordpress - 类别永久链接中的类别 ID,woocommerce
- r - 数据帧中条目的灵活规范化