2016-06-10 67 views
0

我正在嘗試使用操作按鈕返回以前的條件面板。直到現在我已經寫了下面的代碼來瀏覽一系列有條件的面板,但我不能回到以前的情況,因爲我無法更新輸入值,它給了我以下錯誤:R Shiny:使用返回操作按鈕轉到上一個conditionalPanel

Warning:Error在$ < -.reactivevalues:試圖值分配給一個只讀reactivevalues對象+

代碼如下:

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 
}) 

})

回答

1

我建議你一個類型添加到輸入值,你的conditionalPanels正在測試,你可以控制的。

您正在檢查輸入link_clicklink_click_Site是否爲undefined。一旦設置完畢,您可以使用customMessageHandler來重置它們,但當然不是undefined。我會選擇另一個獨特的值,如JavaScript null

所以基本上改變所有條件未定義或空或類似不是不確定的,而不是空。然後,您可以通過將輸入設置爲null來重置輸入。

見下面的代碼:

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) 
相關問題