简体   繁体   English

shiny 中 shinyDashboard 中的 infoBox/valueBox

[英]infoBox/valueBox from shinyDashboard in shiny

I have a simple shiny-app with just a dropdown listing districts of Afghanistan and a leaflet map of the same.我有一个简单的闪亮应用程序,只有一个下拉列表列出阿富汗地区和一个 leaflet map。 在此处输入图像描述

The shape file can be accessed at this link - using AFG_adm2.shp from http://www.gadm.org/download可以通过此链接访问形状文件 - 使用 http 中的AFG_adm2.shp://www.gadm.org/download

here's the app code:这是应用程序代码:

library(shiny)
library(leaflet)
library(rgdal)
library(sp)

afg <- readOGR(dsn = "data", layer ="AFG_adm2", verbose = FALSE, stringsAsFactors = FALSE)

ui <- fluidPage(
  titlePanel("Test App"),
  selectInput("yours", choices = c("",afg$NAME_2), label = "Select Country:"),
  leafletOutput("mymap")

)

server <- function(input, output){
  output$mymap <- renderLeaflet({
    leaflet(afg) %>% addTiles() %>%
      addPolylines(stroke=TRUE, color = "#00000", weight = 1) 
  })
  proxy <- leafletProxy("mymap")

  observe({
    if(input$yours!=""){
      #get the selected polygon and extract the label point 
      selected_polygon <- subset(afg,afg$NAME_2==input$yours)
      polygon_labelPt <- selected_polygon@polygons[[1]]@labpt

      #remove any previously highlighted polygon
      proxy %>% removeShape("highlighted_polygon")

      #center the view on the polygon 
      proxy %>% setView(lng=polygon_labelPt[1],lat=polygon_labelPt[2],zoom=7)

      #add a slightly thicker red polygon on top of the selected one
      proxy %>% addPolylines(stroke=TRUE, weight = 2,color="red",data=selected_polygon,layerId="highlighted_polygon")
    }
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

I want a infoBox or valueBox like widget from shinyDashboard to display some data(like district population) below the map based on user selection.我想要一个来自shinyDashboard的类似infoBoxvalueBox的小部件,以根据用户选择在 map 下方显示一些数据(如地区人口)。 How can I do this?我怎样才能做到这一点?

You can mimic the shinydashboard::infoBox with your own function:你可以用你自己的 function 模仿shinydashboard::infoBox infoBox:

  1. create function创建 function
myInfoBox <- function(title, value)
{
 div(
  div(class='myinfobox-title', title),
  div(class='myinfobox-value', value)
 )
}
  1. use uiOutput() whenever you want to place it eg uiOutput('idOfInfoBox')每当你想放置它时使用uiOutput()例如uiOutput('idOfInfoBox')
  2. in server part use eg output$idOfInfoBox <- renderUI(myInfoBox(title, value)server部分使用例如output$idOfInfoBox <- renderUI(myInfoBox(title, value)
  3. add.css file in www/ directory and add some properties for classes myinfobox-title and myinfobox-valuewww/目录中添加.css 文件并为类myinfobox-titlemyinfobox-value添加一些属性

I hope this helps.我希望这有帮助。

You need to change the structure of the program and need to add dashboard page in UI. 您需要更改程序的结构,并需要在UI中添加仪表板页面。

Here are some reference just have a look. 这里有一些参考只是看看。 you will get to know!!! 您将了解!!!

https://rstudio.github.io/shinydashboard/structure.html https://rstudio.github.io/shinydashboard/structure.html

https://rdrr.io/cran/shinydashboard/man/valueBox.html https://rdrr.io/cran/shinydashboard/man/valueBox.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM