【问题标题】:R Shiny: Go to the previous conditionalPanel using back action buttonR Shiny:使用后退操作按钮转到上一个条件面板
【发布时间】:2016-06-12 23:03:37
【问题描述】:

我正在尝试使用操作按钮返回上一个条件面板。到目前为止,我已经编写了以下代码来浏览一系列条件面板,但由于无法更新输入值,我无法返回前一个案例,这给了我以下错误:

警告:$

代码如下:

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

})

【问题讨论】:

    标签: r shiny


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-04
      • 2016-08-14
      • 1970-01-01
      • 2015-07-19
      • 2014-11-18
      • 2021-09-19
      • 2021-11-12
      • 1970-01-01
      相关资源
      最近更新 更多