在R/Shiny中,单击菜单项后强制重新渲染绘图



这是我的代码。

我的问题是:一旦我点击项目3面板(菜单项目3(,是否可以对其进行闪亮的更新/刷新?例如,我知道我可以使用按钮。但我需要点击第3项,并在点击后强制刷新页面。

我想知道observeEvent是否会为我做这件事。

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)

我不确定我是否理解强制页面刷新"-零件正确。如果这是指与浏览器重新加载页面相同的功能,请检查以下内容:

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)

最新更新