Reputation: 65
I'm trying to prepare a Shinyapp with pre-loaded data. Among other question that I have (I'm a nooby right now), I want to kindly ask why is this error keep occurring. Here is my code, I have no idea what else should I have to say, but I really hope your expertise will resolve this problem. This is my script:
UI.
min<-min(reportesFGE2$Fecha_Registro)
max<-max(reportesFGE2$Fecha_Registro)
ui <- fluidPage(
titlePanel("Delitos"),
sidebarPanel(
dateRangeInput(inputId = "fecha_r",
label= "Fecha de registro",
min = Sys.Date() - 5, max = Sys.Date() + 5),
selectInput(inputId = "nivel",
label= "Nivel",
multiple = F,
choices = c("Nacional"=1, "Provincial"=2)),
selectizeInput(inputId = "provincia",
label= "provincia",
multiple = T,
choices = as.character(unique(reportesFGE2$pro_descripcion))),
selectInput(inputId = "tipo_delito1",
label= "Presunto delito",
multiple = T,
choices = as.character(unique(reportesFGE2$Presunto_Delito)))),
mainPanel( plotOutput("plot1"))
)
and this is SERVER.
server <- function(input, output) {
output$plot1 <- renderPlot({
if (req(output$nivel)==1) {
reportesFGE2$anio<-substring(reportesFGE2$Fecha_Registro,1,4)
x<- subset(reportesFGE2,Presunto_Delito==input$tipo_delito
&Fecha_Registro>=input$fecha_r[1]&Fecha_Registro<=input$fecha_r[2])
y <- x %>% count(x$Presunto_Delito, x$anio)
p1<-plot(y$n~y$`x$anio`,
type="o",
main="Número de delitos por año\n(hasta septiembre de 2019)",
xlab=input$tipo_delito,
ylab="Número de delitos")
} else if (req(output$nivel)==2) {
selectedData <- reactive({
req(input$fecha_r[1])
req(input$fecha_r[2])
req(input$tipo_delito)
req(input$provincia)
reportesFGE2 %>%
dplyr::filter( Fecha_Registro >= input$fecha_R[1]
& Fecha_Registro <= input$fecha_R[2]
&pro_descripcion %in% input$provincia
& Presunto_Delito %in% input$tipo_delito)
})
output$plot1 <-renderPlot({
x==selectedData()
x$anio<-substring(x$Fecha_Registro,1,4)
y <- x %>% count(x$Presunto_Delito, x$anio)
p1<-plot(y$n~y$`x$anio`,
type="o",
main="Número de delitos por año\n(hasta septiembre de 2019)",
xlab=input$tipo_delito,
ylab="Número de delitos")
})
}
})
}
shinyApp(ui = ui, server = server)
Upvotes: 1
Views: 355
Reputation: 7689
I cannot test it because your code sample is not reproducible. But what the error states is that you are trying to access an UI output variable directly in your server. Of course you can access the generated output
in your server, but then it becomes an input
, right?
This happens when you are checking if output$nivel
is either 1 or 2. You should access the nivel
variable with input$nivel
in your server.
Upvotes: 1