简体   繁体   中英

Reactive ggplot with Shiny-

Hi community Im try to make a reactive Shiny app.

When I select the name "Lugar" appear the plot. Im have the code of the each plot that in need but dont run well in shiny.

library(readxl)
library(tidyverse)
library(shiny)

server <- function(input, output, session) {
  
  precip = read_excel("meses2.xlsx", sheet="F")
  #Summarize Data and then Plot
  data <- reactive({
    req(input$sel_Lugar)
    df <- precip %>% filter(Lugar %in% input$sel_Lugar) %>%  group_by(Mes) %>% summarise(precip = sum(Medicion))
  })
  
  #Update SelectInput Dynamically
  observe({
    updateSelectInput(session, "sel_Lugar", choices = precip$Lugar)
  })
  
  #Plot 
  output$plot <- renderPlot({
    ggplot(data(),aes(x=sel_Lugar, y=Medicion, fill=Mes,na.rm = TRUE)) +
      geom_col() +
      scale_fill_manual(values=c("#E23D2C","#BAA512","#512B9A","#21AB3F")) + 
      labs(title = "Distribución general de la precipitación",
           caption= "Fuente: propia")+
      ylab("Precipitación (mm)") +
      theme_light()
    
  })
}

ui <- basicPage(
  h1("R Shiny Dynamically create Drop Down List"),
  selectInput(inputId = "sel_Lugar",
              label = "Choose Sales Rep",
              "Names"),
  plotOutput("plot")
)

shinyApp(ui = ui, server = server)

在此处输入图像描述

-

Im need that in one sheet appear this not reactive plot

In other sheet/tabs Im need put the reactive plot. This is the code. Im want that when select the "lugar" appear the plot. Are 4 "Lugar"

d_New<-read_excel("meses2.xlsx", sheet="F")

ggplot(d_New, aes(x=Lugar, y=Medicion, fill=Mes,na.rm = TRUE)) +
  geom_col() +
  scale_fill_manual(values=c("#E23D2C","#BAA512","#512B9A","#21AB3F")) + 
  labs(title = "Distribución general de la precipitación",
       caption= "Fuente: propia")+
  ylab("Precipitación (mm)") +
  theme_light()

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

The app run and show this. 在此处输入图像描述

Example data

structure(list(Mes = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L), .Label = c("Marzo", "Abril", "Mayo"), class = "factor"), 
    Dia = c(17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 
    17, 18, 19, 29, 30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 17, 18, 19, 
    20, 21, 22, 23, 24, 25, 26, 27, 16, 17, 18, 19, 20, 21, 22, 
    23, 24, 25, 26, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), Medicion = c(50, 
    17, 13, 12, 3, 6, 0.3, NA, 13, 1, 4, 20, 3, NA, 0, 0, 0, 
    0, 20, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 0, 0, 5, 9, 
    0, 1, 0, 0, 0, 0, 2, 17, 58, 32, 0, 9, 0.5, 1, 25, 1, 0.5, 
    4, 10, 3, 3, 8, 36, 13, 1, 0.5, 0.5), Lugar = c("UNIAGRARIA", 
    "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", 
    "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", 
    "UNIAGRARIA", "UNIAGRARIA", "GRANADA NORTE", "GRANADA NORTE", 
    "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", 
    "RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO", 
    "RIO FRIO", "RIO FRIO", "RIO FRIO", "CHICU", "CHICU", "CHICU", 
    "CHICU", "CHICU", "CHICU", "CHICU", "CHICU", "CHICU", "CHICU", 
    "CHICU", "RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO", 
    "RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO", 
    "RIO FRIO", "RIO FRIO", "GRANADA NORTE", "GRANADA NORTE", 
    "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", 
    "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", 
    "GRANADA NORTE")), row.names = c(NA, -60L), class = c("tbl_df", 
"tbl", "data.frame"))

Thanks

Since you want to have 2 tabs (1 for the fixed plot and 1 for the reactive plot) you need to use navbarPage .

"sel_Lugar" was not found and it couldn't be used for the plot because data() doesn't have this column. The way that you tried to get data it was giving you a column with "Mes" and another column with "precip".

图 1

I don't know why you were trying to summarise , but if you want to draw the plots, you need to remove this part.

在此处输入图像描述

Therefore, you will be able to use the columns "Lugar", "Medicion" and "Mes" in the plot.

On the other hand, the code that you were using to draw the reactive plot is wrong.

ggplot(precip, aes(x=Lugar, y=Medicion, fill=Mes,na.rm = TRUE)) +
  geom_col() +
  scale_fill_manual(values=c("#E23D2C","#BAA512","#512B9A","#21AB3F")) + 
  labs(title = "Distribución general de la precipitación",
       caption= "Fuente: propia")+
  ylab("Precipitación (mm)") +
  theme_light()

If you want to get the same output that you attached in the photos (for the reactive plot), you need to change the x (instead of "Lugar", it has to be "Dia"). Besides, if you want a scale that contains "Dias" I recommend you to use scale_x_continuous . In my case, I have used scale_x_continuous(breaks=seq(from=0, to = max(precip$Dia), by=1))

seq(from=0, to = max(precip$Dia), by=1)
1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3

I have changed the way that you show the places ( selectInput ), since I have saved the choices into a function. However, if for any case you have a huge list of options, I recommend you to use selectizeInput . Here you have one example .

After all the changes that I made, this is how your app looks.

在此处输入图像描述 在此处输入图像描述

Here I attach you the entire code.

Note that I added a table where you can see how you are subsetting your data according to the selectInput .

library(shinythemes)
library(shiny)
library(readxl)
library(shiny)
library(dplyr)
library(DT)
library(ggplot2)

# Since you want to have 1 tab for the "not reactive plot" and another tab for the "reactive plot", you will need to use navbarPage
ui <-  fluidPage(
  theme = shinytheme("flatly"), # this is to put some colors/themes
  navbarPage(
    title = "R Shiny Dynamically create Drop Down List", 
    collapsible = TRUE,
    fluid = TRUE,
    
    tabPanel(
      title = "Not reactive plot", 
      
      sidebarLayout(
        sidebarPanel = sidebarPanel(
         
        ), 
        
        mainPanel = mainPanel(
          plotOutput("plot1")
        )
      )
    ),
    tabPanel(
      title = "Reactive plot", 
      
      sidebarLayout(
        sidebarPanel = sidebarPanel(
          uiOutput('selectUI'), # this is to have the list of places
        ), 
        
        mainPanel = mainPanel(
          plotOutput("plot2"),
          dataTableOutput("table")
        )
      )
    ),
  )
)

server <- function(input, output, session) {
  
  # this is your data without filtering
  data <- reactive({
    return(precip)
  })
  
  # this gives you the a vector of the choices of your original data
  lugar_choices <- reactive({
    choices <- levels(data()$Lugar)
    return(choices)
  })
  
  # this will give you your choices.
  output$selectUI <- renderUI({
    selectInput(inputId = 'sel_Lugar', "Choose Sales Rep", choices = lugar_choices())  
  })
  
  # this your new data according to your select Input.
  data_filtered <- reactive({
    req(input$sel_Lugar)
    df <-  data() %>%
      filter(Lugar %in% input$sel_Lugar) %>%
      group_by(Mes)
    return(df)
  })
  
  # first plot (not reactive)
  output$plot1 <- renderPlot({
    ggplot(data(), aes(x=Lugar, y=Medicion, fill=Mes,na.rm = TRUE)) +
      
      geom_col() +
      scale_fill_manual(values=c("#E23D2C","#BAA512","#512B9A","#21AB3F")) + 
      labs(title = "Distribución general de la precipitación",
           caption= "Fuente: propia")+
      ylab("Precipitación (mm)") +
      theme_light()
  })
  
  # second plot (reactive)
  output$plot2 <- renderPlot({
    ggplot(data_filtered(), aes(x=Dia, y=Medicion, fill=Mes,na.rm = TRUE)) +
      geom_col() +
      scale_fill_manual(values=c("#E23D2C","#BAA512","#512B9A","#21AB3F")) + 
      labs(title = "Distribución general de la precipitación",
           caption= "Fuente: propia")+
      ylab("Precipitación (mm)") +
      scale_x_continuous(breaks=seq(from=0, to = max(data_filtered()$Dia), by=1))+
      theme_light()
  })
  
  # this is the info that you are plotting 
  output$table <- renderDataTable(data_filtered())
  
  
}

shinyApp(ui, server)

Data:

precip = structure(list(Mes = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                          2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                          1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
                                          2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                          3L, 3L, 3L, 3L, 3L), .Label = c("Marzo", "Abril", "Mayo"), class = "factor"), 
                        Dia = c(17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 
                                17, 18, 19, 29, 30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 17, 18, 19, 
                                20, 21, 22, 23, 24, 25, 26, 27, 16, 17, 18, 19, 20, 21, 22, 
                                23, 24, 25, 26, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), Medicion = c(50, 
                                                                                                     17, 13, 12, 3, 6, 0.3, NA, 13, 1, 4, 20, 3, NA, 0, 0, 0, 
                                                                                                     0, 20, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 0, 0, 5, 9, 
                                                                                                     0, 1, 0, 0, 0, 0, 2, 17, 58, 32, 0, 9, 0.5, 1, 25, 1, 0.5, 
                                                                                                     4, 10, 3, 3, 8, 36, 13, 1, 0.5, 0.5), Lugar = c("UNIAGRARIA", 
                                                                                                                                                     "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", 
                                                                                                                                                     "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", "UNIAGRARIA", 
                                                                                                                                                     "UNIAGRARIA", "UNIAGRARIA", "GRANADA NORTE", "GRANADA NORTE", 
                                                                                                                                                     "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", 
                                                                                                                                                     "RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO", 
                                                                                                                                                     "RIO FRIO", "RIO FRIO", "RIO FRIO", "CHICU", "CHICU", "CHICU", 
                                                                                                                                                     "CHICU", "CHICU", "CHICU", "CHICU", "CHICU", "CHICU", "CHICU", 
                                                                                                                                                     "CHICU", "RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO", 
                                                                                                                                                     "RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO", "RIO FRIO", 
                                                                                                                                                     "RIO FRIO", "RIO FRIO", "GRANADA NORTE", "GRANADA NORTE", 
                                                                                                                                                     "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", 
                                                                                                                                                     "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", "GRANADA NORTE", 
                                                                                                                                                     "GRANADA NORTE")), row.names = c(NA, -60L), class = c("tbl_df", 
                                                                                                                                                                                                           "tbl", "data.frame"))

# we transform the column into factors because we will need it for the choices (I will need the different levels)
precip$Lugar <- as.factor(precip$Lugar)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM