R语言 按下闪亮的重置按钮后删除Map

vaj7vani  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(134)

我按下闪亮的重置按钮后无法删除生成的Map,你能帮我插入代码来删除按下按钮后生成的Map吗?对于两个selectInput工作正常,只有Map没有从屏幕上删除。

library(shiny)
  library(shinythemes)
  library(lubridate)
  library(googleway)
  
  set_key("API KEY")
  
  
  df1<- structure(
    list(
      Marketname = c("Market1","Market1", "Market2","Market2", "Market3", "Market3", "Market4", "Market4"),
      Latitude = c(-22.900200453490385, -22.900200453490385,-22.89279876292728,-22.89279876292728,-22.89107669207457,-22.89107669207457,-22.91668421655409,-22.91668421655409),
      Longitude = c(-48.448779371935494,-48.448779371935494, -48.45043377250408,-48.45043377250408,-48.44108027972275,-48.44108027972275,-48.43786997555729,-48.43786997555729)),
    row.names = c(NA, 8L), class = "data.frame")
  
  
  ui <- fluidPage(  
    shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
                      br(),
                      tabPanel("Rota",
                               sidebarLayout(
                                 sidebarPanel(
                                   selectizeInput("market1", label = h5("Choose starting point:"), choices = NULL, 
                                                  multiple = TRUE,
                                                  options = list(maxItems = 1)),
                                   
                                   selectizeInput("market2", label = h5("Choose destination point:"), choices = NULL, 
                                                  multiple = TRUE,
                                                  options = list(maxItems = 1)),
                                   
                                   actionButton(inputId = "getRoute", label = "Get route"),
                                   actionButton(inputId = "reset", label = "Reset")),
  
                                 mainPanel(
                                   tabsetPanel(      
                                     tabPanel("Route",google_mapOutput(outputId = "mapWarsaw"),
                                     )
                                   ))
                               ))))
  
  server <- function(input, output,session) {
    
    
    observe({
      updateSelectizeInput(session, "market1",
                           choices = unique(df1$Marketname)
      )
    })
    
    
    observe({
      excludeOption <- NULL
      if (!is.null(input$market1)) {
        excludeOption <- input$market1
      }
      
      updateSelectizeInput(session, "market2",
                           choices = unique(df1$Marketname[df1$Marketname != excludeOption])
      )
    })
    
    observeEvent(input$getRoute, {
      origin <- df1[df1$Marketname == input$market1, c("Latitude", "Longitude")][1, ]
      
      destination <- df1[df1$Marketname == input$market2, c("Latitude", "Longitude")][1, ]
    
      route <- google_directions(origin = origin, 
                                 destination = destination,
                                 mode = "driving")
      
      df_routes <- data.frame(polyline = direction_polyline(route))
      
      df_way <- cbind(
        route$routes$legs[[1]]$end_location,
        data.frame(address = route$routes$legs[[1]]$end_address)
      )
      
      
      m3<-google_map() %>%
        add_polylines(data = df_routes, polyline = "polyline", stroke_weight = 4)
      
      output$mapWarsaw <- renderGoogle_map({
        m3
      })
    })
    
     observeEvent(input$reset, {
       updateSelectInput(session, "market1", selected = "")
       updateSelectInput(session, "market2", selected = "")
                
  })
    
  }
  
  shinyApp(ui = ui, server = server)

这里我按了reset,selectInput被清除了,但是Map没有,所以我想插入一些清除屏幕的代码,当然,再次插入selectInput选项后,Map在屏幕上正常生成。

zujrkrfu

zujrkrfu1#

我建议使用reactiveVal来存储是否要绘图,然后在绘图中使用req(.)该值。
举个简单的例子:

library(shiny)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      numericInput("n", "N", value = 2),
      actionButton("toggle", "Toggle")
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)
server <- function(input, output, session) {
  doplot <- reactiveVal(TRUE)
  observeEvent(input$toggle, {
    doplot(!isTRUE(doplot()))
  })
  output$plot <- renderPlot({
    req(doplot())
    plot(runif(input$n))
  })
}
shinyApp(ui, server)
  • doplot默认为TRUE,因此立即开始出图(假设input$n有值),并且每次input$n改变时,渲染新的出图;
  • 当您点击toggle时,doplot被反转,依赖output$plot将"失败" req(doplot())要求并清除绘图;
  • 这一点可以改进,以便更清楚地表明空图是因为用户切换了按钮......在这种情况下,我可能会更改按钮的样式,以清楚地表明状态,但这是美观问题,可能适用于您的应用程序,也可能不适用

另一个类似的方法是将 * data * 存储在reactiveVal中,然后req对其进行处理,可能如下所示:

library(shiny)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      numericInput("n", "N", value = 2),
      actionButton("toggle", "Toggle")
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)
server <- function(input, output, session) {
  mydat <- reactiveVal(NULL)
  observeEvent(input$toggle, {
    req(input$n)
    if (is.null(mydat())) {
      mydat(runif(input$n))
    } else mydat(NULL)
  })
  output$plot <- renderPlot({
    plot(req(mydat()))
  })
}
shinyApp(ui, server)

如果您需要保留数据,则第二种方法效果不佳。

相关问题