In R/Shiny forcing to re-render the plot after clicking the menuItem

Viewed 44

This is my code.

My question is: Is it possible to make shiny update/refresh the chart displayed in item 3 panel (Menu Item 3) once I click on it? I know that I could use a button, for example. But I need to click on Item 3 and force the page to refresh once I click.

I was wondering if observeEvent would do this for me.

library("shiny")
library("shinyWidgets")
library("gapminder")

hc_data_1 <- gapminder %>% filter(country == 'Chile') 



ui <- dashboardPage(dark = FALSE,fullscreen = TRUE,
                    dashboardHeader(title = h4("Title")),
                    
                    dashboardSidebar(skin = 'light',
                                     sidebarMenu( id = "sidebarMenu",
                                                  menuItem(text = "Item 1", 
                                                           tabName = "panel_1"), 
                                                  menuItem(text = "Item 2", 
                                                           tabName = "panel_2"), 
                                                  menuItem(text = "Item 3", 
                                                           tabName = "panel_3")                                       )
                                     
                    ),
                    dashboardBody(
                    
                      tabItems(  
                        tabItem(tabName = 'panel_1',
                                tabPanel("Panel 1")),
                        tabItem(tabName = 'panel_2',
                                tabPanel("Panel 2")),
                        tabItem(tabName = 'panel_3',
                                tabPanel("Panel 3",
                                         htmlOutput('chart_1',width = '100px')
                                ))
                      )
                      
                      
                      
                    ))

server <- function(input, output, session) {
  
  output$chart_1<- renderUI({
    
    hc_data_1 %>%
      
      hchart(
        "line",
        hcaes(x = year, y = pop),
        showInLegend = TRUE,
        color = "#63696b",
        name = "chile-argentina"
      )
    
  })
}

shinyApp(ui = ui, server = server)
1 Answers

I am not sure if I understand the "force the page to refresh"-part correctly. If this is referring to the same function as a browser reloading its page please check the following:

library("shiny")
library("shinyWidgets")
library("gapminder")
library("dplyr")
library("bs4Dash")
library("highcharter")

hc_data_1 <- gapminder %>% filter(country == 'Chile')

ui <- bs4Dash::dashboardPage(
  dark = FALSE,
  fullscreen = TRUE,
  dashboardHeader(title = h4("Title")),
  dashboardSidebar(
    skin = 'light',
    sidebarMenu(
      id = "sidebarMenu",
      menuItem(text = "Item 1",
               tabName = "panel_1"),
      menuItem(text = "Item 2",
               tabName = "panel_2"),
      menuItem(text = "Item 3",
               tabName = "panel_3")
    )
  ),
  dashboardBody(tabItems(
    tabItem(tabName = 'panel_1',
            tabPanel("Panel 1")),
    tabItem(tabName = 'panel_2',
            tabPanel("Panel 2")),
    tabItem(tabName = 'panel_3',
            tabPanel(
              "Panel 3",
              htmlOutput('chart_1', width = '100px')
            ))
  ))
)

server <- function(input, output, session) {
  # observeEvent(input$sidebarMenu, {
  #   if (input$sidebarMenu == "panel_3") {
  #     print("reloading shiny session")
  #     session$reload()
  #   }
  # })
  
  output$chart_1 <- renderUI({
    input$sidebarMenu # take a reactive dependency on input$sidebarMenu
    hc_data_1 %>%
      hchart(
        "line",
        hcaes(x = year, y = pop),
        showInLegend = TRUE,
        color = "#63696b",
        name = "chile-argentina"
      )
  })
}

shinyApp(ui = ui, server = server)
Related