因此,我尝试创建一个漂亮的应用程序,在其中使用sf数据绘制印度Map。我上传了一个包含多列的数据集。因此,我要求用户选择列。但是,当我将该输入提供给geom_sf(fill = input$select_topic)
时,它首先认为列名是一个字符值,因此我使用geom_sf(fill = .data$input$select_topic)
或geom_sf(fill = ,data[[input$select_topic]] )
,这会给我**error : Can't subset .data outside of a data mask context.
**
下面是代码:
library(shiny)
library(tidyverse)
library(sf)
# joining the main datset and the sf dataset
map_data <- read.csv("D:\\R Projects\\R experimental\\dashboard\\app-1\\app-1\\data\\edu_data.csv")
map_data <- as.data.frame(map_data)
map_data_2 <- left_join(India, map_data, by = "state_ut")
# Define UI ----
ui <- fluidPage(
titlePanel("My Shiny App"),
sidebarLayout(position = "right",
sidebarPanel(
selectInput("select_year", label = "Select Year",
choices = list("2012-13" = 2012,
"2013-14" = 2013,
"2014-15" = 2014,
"2015-16" = 2015), selected = 2013),
selectInput("select_topic", label = "Select Category",
choices = list("Drinking Water Facility" = 'water_faci',
"Girl's Toilet Facility" = 'gtoi_faci',
"Boy's Toilet Facility" = 'btoi_faci',
"Electricity Connection" = 'elec_faci',
"Computer availability" = 'comp_faci',
"Overall Facilities" = 'all_faci',
"Gross Enrollment" = 'gross_enroll',
"Drop-out Rate" = 'drop_rate'
), selected = "Drinking Water Facility")
mainPanel(
h1("Maps"),
textOutput("selected_year"),
textOutput("selected_category"),
plotOutput("map")
)
)
)
# Define server logic ----
server <- function(input, output) {
output$selected_year <- renderText({
paste("You have selected the year:", input$select_year)
})
output$selected_category <- renderText({
paste("You have selected category:", input$select_topic)
})
})
output$map <- renderPlot({
x <- map_data_2 %>% filter(., year == input$select_year)
ggplot(x, aes(label = state_ut)) +
geom_sf( fill = .data$input$select_topic ) +
scale_fill_distiller(palette = "GnBu",
direction= 1,
na.value="grey") +
theme_map() +
theme( plot.title = element_text(size=22),
legend.position = "left",
legend.justification ='left')
})
}
# Run the app ----
shinyApp(ui = ui, server = server)
- 这是我需要选择其列的数据集**
| 状态|年份|水相|组成|btoi_相|gtoi_面|电_面|全相|总入组|下降率|
| - ------|- ------|- ------|- ------|- ------|- ------|- ------|- ------|- ------|- ------|
| 全印度|二〇一三年|九十五点四|二十四时零八分|八十六点五六分|九十一点二三分|五十六点七八分|七十块八一|七十九点八九|六点八八|
| 安达曼和尼科巴群岛|二〇一三年|九十八点六九|五十三点零六分|九十四点五二分|九十三点四四分|八十八点八六分|八十五七百一十四|九十六点八|六、二|
| 安得拉邦|二〇一三年|九十点三五分|二十九点五十七分|五十六点八八|八十一点三一分|九十点三四分|六十九点六九|七十八点九五分|八点一四|
| 阿鲁纳恰尔邦|二〇一三年|七十九点七九|二十四点二十八分|四十八点七三分|七十六点九|三十六点三十五分|五十三点二一|九十八块五八||
| 阿萨姆邦|二〇一三年|八十点三|八点八五分|六十点一七分|七十五点二八|十九点三十九分|四十八七百九十八|七十七点六八|十三时零四分|
| 比哈尔邦|二〇一三年|九十二点五三分|五点四十七分|七十三点四二|七十五点四一|九点九六分|五十一三五八|六十七点二四分||
| 昌迪加尔|二〇一三年|一百|九十五点八三|一百|一百|一百|九十九点一六六|九十四点一八分||
| 恰蒂斯加尔邦|二〇一三年|九十五点四十七分|九点八|八十二点八五分|九十四点一|五十七点二八|六十七点九|八十九点九八分||
output$map <- renderPlot({
x <- map_data_2 %>% filter(., year == input$select_year)
ggplot(x, aes(label = state_ut)) +
geom_sf( fill = input$select_topic ) +
scale_fill_distiller(palette = "GnBu",
direction= 1,
na.value="grey") +
theme_map() +
theme( plot.title = element_text(size=22),
legend.position = "left",
legend.justification ='left')
})
将产生一个Map,该Map将填充值作为数据集中存在的列。
1条答案
按热度按时间disbfnqx1#
通过两种方式更改
fill=
:aes(.)
中,并.data[[ input$select_year ]]
。(使用.data$input$select_year
无效。)有关https://ggplot2.tidyverse.org/articles/ggplot2-in-packages.html#using-aes-and-vars-in-a-package-function-1如何间接引用列名,请参见www.example.com。