r - 闪亮的问题:图表不显示何时更新输入(加上一些错误)
问题描述
作为我之前的问题的延续,现在我被我的图表困住了。我真的认为这会很简单,因为我的输入已正确更新,但是当浏览器打开以显示应用程序时我收到此错误:
Error: no applicable method for 'filter' applied to an object of class "shiny.tag"
我真的很困惑和筋疲力尽,我只是看不出问题出在哪里:在为图形生成数据库的代码中(我使用了很多dplyr::filter
,因为我认为问题就在那里,但这没有区别)或代码图形本身或两者兼而有之。
感谢您的所有反馈和评论。
以下是您需要复制的所有内容:
虚拟数据库:
x <- structure(list(año = c("2019", "2019", "2019", "2019", "2019",
"2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019",
"2019", "2019", "2019", "2019", "2019", "2020", "2020", "2020",
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2020",
"2020", "2020", "2020", "2020", "2020", "2020", "2020", "2021",
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021",
"2021", "2021", "2021", "2021", "2021", "2021", "2021", "2021",
"2021"), opciones = c("L1", "L1", "L1", "L2", "L2", "L2", "L3",
"L3", "L3", "LA", "LA", "LA", "LB", "LB", "LB", "LC", "LC", "LC",
"L1", "L1", "L1", "L2", "L2", "L2", "L3", "L3", "L3", "LA", "LA",
"LA", "LB", "LB", "LB", "LC", "LC", "LC", "L1", "L1", "L1", "L2",
"L2", "L2", "L3", "L3", "L3", "LA", "LA", "LA", "LB", "LB", "LB",
"LC", "LC", "LC"), lugar = c("EXT", "INT", "LOC", "NOM", "KHA",
"POC", "TMO", "MCR", "GNR", "APV", "HOT", "ROR", "GRU", "BOY",
"POK", "NOT", "LOX", "TAK", "EXT", "INT", "LOC", "NOM", "KHA",
"POC", "TMO", "MCR", "GNR", "APV", "HOT", "ROR", "GRU", "BOY",
"POK", "NOT", "LOX", "TAK", "EXT", "INT", "LOC", "NOM", "KHA",
"POC", "TMO", "MCR", "GNR", "APV", "HOT", "ROR", "GRU", "BOY",
"POK", "NOT", "LOX", "TAK"), numeros = c(4011L, 18564L, 24325L,
9798L, 18621L, 11165L, 6071L, 23466L, 22472L, 23990L, 3980L,
2976L, 24142L, 3140L, 20317L, 11884L, 9427L, 20704L, 609L, 23428L,
19853L, 10450L, 577L, 14153L, 4757L, 6393L, 18461L, 1988L, 20044L,
8371L, 21371L, 12312L, 17368L, 16045L, 11492L, 7903L, 21409L,
12547L, 19926L, 17857L, 6728L, 5584L, 14837L, 10269L, 20168L,
13092L, 22233L, 20663L, 11975L, 14672L, 19389L, 7329L, 14062L,
167L), mes = c("Agosto", "Julio", "Agosto", "Febrero", "Julio",
"Agosto", "Mayo", "Noviembre", "Abril", "Diciembre", "Julio",
"Febrero", "Mayo", "Octubre", "Septiembre", "Febrero", "Enero",
"Febrero", "Noviembre", "Julio", "Septiembre", "Marzo", "Diciembre",
"Octubre", "Enero", "Agosto", "Septiembre", "Abril", "Julio",
"Enero", "Febrero", "Febrero", "Abril", "Septiembre", "Agosto",
"Septiembre", "Abril", "Septiembre", "Abril", "Febrero", "Marzo",
"Febrero", "Abril", "Noviembre", "Noviembre", "Septiembre", "Abril",
"Enero", "Marzo", "Julio", "Mayo", "Febrero", "Febrero", "Agosto"
)), class = "data.frame", row.names = c(NA, -54L))
该应用程序的代码:
# Options for tabs
años <- c("2019", "2020", "2021")
meses <- c("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Juno", "Julio",
"Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre")
opciones <- c("L1","L2","L3","LA","LB","LC")
lugar <- list(L1=c("EXT","INT","LOC"), L2=c("NOM","KHA","POC"),
L3= c("TMO", "MCR", "GNR"), LA=c("APV", "HOT", "ROR"),
LB= c("GRU", "BOY", "POK"), LC=c("NOT", "LOX", "TAK"))
## App
test_lugar <- tabPanel(
titlePanel= "Test",
titlePanel(h5("Test")),
sidebarLayout(
sidebarPanel(
selectInput("año", "Año", choices=años,
selected= "2019"),
selectInput("mes", "Mes", choices=meses,
selected="Enero", multiple=TRUE),
selectInput("opcion", "Opción",
choices=opciones, selected="L1"),
selectInput("lugar", "Lugar", c(), multiple=TRUE)
),
mainPanel(
#plotlyOutput("afluencia_lugar", height = "400px", width="1000px")
)
)
)
ui <- navbarPage(title = h5(strong("XXXX")),
theme = shinytheme("paper"),
test_lugar)
server <- function(input, output, session) {
seleccionar_opcion_lugar <- eventReactive(input$opcion, {
get("lugar")[[input$opcion]]
})
observe({print(seleccionar_opcion_lugar())})
lugar_seleccionado <-
observeEvent(input$opcion, {
req(seleccionar_opcion_lugar())
choices <- unlist(seleccionar_opcion_lugar())
updateSelectInput(session,"lugar", choices=choices,
selected=choices[1])
}, ignoreNULL = FALSE)
### CODE FOR THE GRAPH
lugar_mensual <- reactive({
x%>%
dplyr::filter(año==input$año)%>%
dplyr::filter(mes%in%input$mes)%>%
dplyr::filter(opcion==input$opcion)%>%
dplyr::filter(lugar%in%input$lugar())
})
output$afluencia_lugar <- renderPlotly({
ggplot(data= lugar_mensual(),
aes(x=mes, y=numeros)) +
labs(x="Fecha", y="Afluencia")+
geom_col() +
theme_minimal() +
facet_wrap(~input$lugar)
})
}
shinyApp(ui, server, options = list(launch.browser = TRUE))
解决方案
你有几个问题。有一个错字,你不需要lugar_seleccionado <-
,因为你不需要分配observeEvent
. 此外,您需要使用plotly
object in ,只需通过 usingrenderPlotly
转换您的对象。最后,在 中,您应该使用数据框中存在的变量(例如,),这些变量已经使用用户输入变量来对所需信息进行子集化。试试这个ggplot
ggplotly
facet_wrap()
lugar
input$lugar
# Options for tabs
años <- c("2019", "2020", "2021")
meses <- c("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Juno", "Julio",
"Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre")
opciones <- c("L1","L2","L3","LA","LB","LC")
lugar <- list(L1=c("EXT","INT","LOC"), L2=c("NOM","KHA","POC"),
L3= c("TMO", "MCR", "GNR"), LA=c("APV", "HOT", "ROR"),
LB= c("GRU", "BOY", "POK"), LC=c("NOT", "LOX", "TAK"))
## App
test_lugar <- tabPanel(
titlePanel= "Test",
titlePanel(h5("Test")),
sidebarLayout(
sidebarPanel(
selectInput("año", "Año", choices=años,
selected= "2019"),
selectInput("mes", "Mes", choices=meses,
selected="Enero", multiple=TRUE),
selectInput("opcion", "Opción",
choices=opciones, selected="L1"),
selectInput("lugar", "Lugar", c(), multiple=TRUE)
),
mainPanel( DTOutput("t1"),
plotlyOutput("afluencia_lugar", height = "400px")
)
)
)
ui <- navbarPage(title = h5(strong("XXXX")),
theme = shinytheme("paper"),
test_lugar)
server <- function(input, output, session) {
seleccionar_opcion_lugar <- eventReactive(input$opcion, {
get("lugar")[[input$opcion]]
})
observe({print(as.character(input$año))})
## lugar_seleccionado <-
observeEvent(input$opcion, {
req(seleccionar_opcion_lugar())
choices <- unlist(seleccionar_opcion_lugar())
updateSelectInput(session,"lugar", choices=choices, selected=choices[1])
}, ignoreNULL = FALSE)
### CODE FOR THE GRAPH
lugar_mensual <- reactive({
x %>%
dplyr::filter(año %in% input$año)%>%
dplyr::filter(mes %in% input$mes)%>%
dplyr::filter(opciones %in% input$opcion)%>%
dplyr::filter(lugar %in% input$lugar )
})
output$t1 <- renderDT(lugar_mensual())
output$afluencia_lugar <- renderPlotly({
p <- ggplot(data= lugar_mensual(),
aes(x=mes, y=numeros)) +
geom_col() +
labs(x="Fecha", y="Afluencia")+
theme_minimal() +
facet_wrap(~lugar)
ggplotly(p)
})
}
shinyApp(ui, server, options = list(launch.browser = TRUE))
推荐阅读
- java - java Windows 服务如何“知道”它已完成初始化?
- r - 将数据从单个观察重新排列为多个
- xml - DBMS_XMLQUERY 与 DBMS_XMLGEN:任何替代 DBMS_XMLQUERY.seterrortag(sqlcontext, 'Any_Tag_Name')
- python - tensorflow 对象检测 API train.py 自身错误
- ibm-mq - IBM MQ - 能够在维护期间对消息进行排队,而无需停止通道
- ios - 在 Xcode 项目中存储大量静态本地 mp4 视频文件的最佳方式
- bash - BASH如何从文件中获取文件名并zcat它们
- javascript - 如何在 SVG 上“单击”时解析和获取 XML 数据?
- javascript - OpenLayers:如何将自定义 id 分配给集群以在点击时检索
- kotlin - 覆盖运算符 Int RangeTo 不起作用