0

I am trying to use action button to return the previous conditional panel. Till now I have written the following code to navigate through series of conditional panel but I am not able go back to the previous case as I am not able to update input values it gives me the following error:

Warning: Error in $<-.reactivevalues: Attempted to assign value to a read-only reactivevalues object+

Code is as follows:

ui.R

shinyUI(
  fluidPage(
    conditionalPanel(
      condition <- "typeof input.link_click  === 'undefined'",
      leafletOutput("Map", width = 1000, height = 500)

    ),


    conditionalPanel(
      condition <- "typeof input.link_click_Site  === 'undefined' && typeof input.link_click  !== 'undefined'",


      leafletOutput("CountryMap", width = 1000, height = 500)

    ),
    conditionalPanel(
      condition <- "typeof input.link_click_Site  !== 'undefined'",

      h3("Plots related to site chosen"),
      textOutput(outputId = "Check"),
      actionButton("Back", "Back")
    )
  )  
)

server.R

library(shiny)
library(leaflet)
library(maps)

shinyServer(function(input, output, session) {

Country = map("world", fill = TRUE, plot = FALSE, regions="USA")
output$Map <- renderLeaflet({
  leaflet(Country) %>% addTiles() %>%  setView(0, 0,  zoom = 2)%>%
    #leaflet(target) %>% addTiles() %>%
    addPolygons(fillOpacity = 0.6,
                fillColor = 'blue',
                smoothFactor = 0.5, stroke = TRUE, weight = 1, popup =  paste("<b>", "USA", "</b><br>",
                                                                              actionLink(inputId = "View", 
                                                                                         label = "View Details", 
                                                                                         onclick = 'Shiny.onInputChange(\"link_click\",  Math.random())')))
})

output$CountryMap <- renderLeaflet({

  leaflet(Country) %>% addTiles() %>%   
    fitBounds(Country$range[1], Country$range[3], Country$range[2], Country$range[4])%>%
    addMarkers(lng = -71.03 , lat = 42.37, popup = paste("<b>", "Boston", "</b><br>",
                                                         actionLink(inputId = "View", 
                                                                    label = "View Details", 
                                                                    onclick = 'Shiny.onInputChange(\"link_click_Site\",  Math.random())')))
})


observeEvent(input$link_click_Site, {
  output$Check <- renderText("Success")

})

observeEvent(input$Back, {
  input$link_click_Site <- 0
})

})

SBista
  • 7,479
  • 1
  • 27
  • 58

1 Answers1

1

I suggest you add a type to the input values, which your conditionalPanels are testing, that you can control.

You are currently checking if the inputs link_click and link_click_Site are undefined. Once they are set, you can reset them with a customMessageHandler, but of course not to undefined. I would pick another unique value like the JavaScript null.

So basically change all conditions to is undefined or null or similarly is not undefined and not null. Then you can reset the inputs via setting them to null.

See code below:

library(shiny)
library(leaflet)    
library(maps)

ui <- shinyUI(
  fluidPage(
    tags$script("
      Shiny.addCustomMessageHandler('resetInputValue', function(variableName){
        Shiny.onInputChange(variableName, null);
      });
    "),

    conditionalPanel(
      condition <- "input.link_click  === undefined || input.link_click === null",
      leafletOutput("Map", width = 1000, height = 500)

    ),


    conditionalPanel(
      condition <- "(input.link_click_Site  === undefined || input.link_click_Site === null) && (input.link_click  !== undefined && input.link_click !== null)",


      leafletOutput("CountryMap", width = 1000, height = 500)

    ),
    conditionalPanel(
      condition <- "(input.link_click_Site  !== undefined && input.link_click_Site !== null)",

      h3("Plots related to site chosen"),
      textOutput(outputId = "Check"),
      actionButton("Back", "Back")
    )
  )  
)

server <- function(input, output, session) {

  Country = map("world", fill = TRUE, plot = FALSE, regions="USA")
  output$Map <- renderLeaflet({
    leaflet(Country) %>% addTiles() %>%  setView(0, 0,  zoom = 2)%>%
      #leaflet(target) %>% addTiles() %>%
      addPolygons(fillOpacity = 0.6,
                  fillColor = 'blue',
                  smoothFactor = 0.5, stroke = TRUE, weight = 1, popup =  paste("<b>", "USA", "</b><br>",
                                                                                actionLink(inputId = "View", 
                                                                                           label = "View Details", 
                                                                                           onclick = 'Shiny.onInputChange(\"link_click\",  Math.random())')))
  })

  output$CountryMap <- renderLeaflet({

    leaflet(Country) %>% addTiles() %>%   
      fitBounds(Country$range[1], Country$range[3], Country$range[2], Country$range[4])%>%
      addMarkers(lng = -71.03 , lat = 42.37, popup = paste("<b>", "Boston", "</b><br>",
                                                         actionLink(inputId = "View", 
                                                                    label = "View Details", 
                                                                    onclick = 'Shiny.onInputChange(\"link_click_Site\",  Math.random())')))
  })


  observeEvent(input$link_click_Site, {
    output$Check <- renderText("Success")

  })

  observeEvent(input$Back, {
    session$sendCustomMessage(type = 'resetInputValue', message = "link_click_Site")
    session$sendCustomMessage(type = 'resetInputValue', message = "link_click")
  })
}

shinyApp(ui, server)
K. Rohde
  • 9,439
  • 1
  • 31
  • 51