我有一个数据框,其中包含一些基于原始数据的动态生成的值(数据来自US Census API)。我正在使用这些数据填写一个等值线并提供工具提示的信息。如果我使用的工具提示我将称之为“静态”字段名称,这很好:
popup <- paste0(EduAtt_merged$areaName, "<br>","GEOID: ", EduAtt_merged$GEOID, "<br>", popupLabelText, round(EduAtt_merged[[input$eduattain]],2), "%<br>Of total population of: ", prettyNum(EduAtt_merged$B23001_001, scientific=FALSE, big.mark=","));
其中EduAtt_merged$B23001_001
是静态值(与“行”无关的元素名称相同)。
我还可以使用所选输入值来获取要显示为工具提示项EduAtt_merged[[input$eduattain]]
的值。
我的问题是,根据input$eduattain
选择,我可以选择另一个要显示的列吗?在此示例中,EduAtt_merged[[input$eduattain]]
是满足所选输入条件的区域总数的百分比。我也想得到实际的数值。
以下是我的专栏:
rownames(EduAtt_df) <- 1:nrow(EduAtt_df);
names(EduAtt_df)<-c("areaName", "GEOID", "B23001_001", "B23001_002", "B23001_003", "B23001_008", "B23001_010", "B23001_015", "B23001_017", "B23001_022", "B23001_024", "B23001_029", "B23001_031", "B23001_036", "B23001_038", "B23001_043", "B23001_045", "B23001_050", "B23001_052", "B23001_057", "B23001_059", "B23001_064", "B23001_066", "B23001_071", "B23001_073", "B23001_076", "B23001_078", "B23001_081", "B23001_083", "B23001_086", "B23001_088", "B23001_089", "B23001_094", "B23001_096", "B23001_101", "B23001_103", "B23001_108", "B23001_110", "B23001_115", "B23001_117", "B23001_122", "B23001_124", "B23001_129", "B23001_131", "B23001_136", "B23001_138", "B23001_143", "B23001_145", "B23001_150", "B23001_152", "B23001_157", "B23001_159", "B23001_162", "B23001_164", "B23001_167", "B23001_169", "B23001_172");
EduAtt_df$total_age16_19 <- (EduAtt_df$B23001_003 + EduAtt_df$B23001_089);
EduAtt_df$total_age20_21 <- (EduAtt_df$B23001_010 + EduAtt_df$B23001_096);
EduAtt_df$total_age22_24 <- (EduAtt_df$B23001_017 + EduAtt_df$B23001_103);
EduAtt_df$total_age25_29 <- (EduAtt_df$B23001_024 + EduAtt_df$B23001_110);
EduAtt_df$total_age30_34 <- (EduAtt_df$B23001_031 + EduAtt_df$B23001_117);
EduAtt_df$total_age35_44 <- (EduAtt_df$B23001_038 + EduAtt_df$B23001_124);
EduAtt_df$total_age45_54 <- (EduAtt_df$B23001_045 + EduAtt_df$B23001_131);
EduAtt_df$total_age55_59 <- (EduAtt_df$B23001_052 + EduAtt_df$B23001_138);
EduAtt_df$total_age60_61 <- (EduAtt_df$B23001_059 + EduAtt_df$B23001_145);
EduAtt_df$total_age62_64 <- (EduAtt_df$B23001_066 + EduAtt_df$B23001_152);
EduAtt_df$total_age65_69 <- (EduAtt_df$B23001_073 + EduAtt_df$B23001_159);
EduAtt_df$total_age70_74 <- (EduAtt_df$B23001_078 + EduAtt_df$B23001_164);
EduAtt_df$total_age75over <- (EduAtt_df$B23001_083 + EduAtt_df$B23001_169);
EduAtt_df$percent_age16_19 <- 100*((EduAtt_df$B23001_003 + EduAtt_df$B23001_089)/EduAtt_df$B23001_001);
EduAtt_df$percent_age20_21 <- 100*((EduAtt_df$B23001_010 + EduAtt_df$B23001_096)/EduAtt_df$B23001_001);
EduAtt_df$percent_age22_24 <- 100*((EduAtt_df$B23001_017 + EduAtt_df$B23001_103)/EduAtt_df$B23001_001);
EduAtt_df$percent_age25_29 <- 100*((EduAtt_df$B23001_024 + EduAtt_df$B23001_110)/EduAtt_df$B23001_001);
EduAtt_df$percent_age30_34 <- 100*((EduAtt_df$B23001_031 + EduAtt_df$B23001_117)/EduAtt_df$B23001_001);
EduAtt_df$percent_age35_44 <- 100*((EduAtt_df$B23001_038 + EduAtt_df$B23001_124)/EduAtt_df$B23001_001);
EduAtt_df$percent_age45_54 <- 100*((EduAtt_df$B23001_045 + EduAtt_df$B23001_131)/EduAtt_df$B23001_001);
EduAtt_df$percent_age55_59 <- 100*((EduAtt_df$B23001_052 + EduAtt_df$B23001_138)/EduAtt_df$B23001_001);
EduAtt_df$percent_age60_61 <- 100*((EduAtt_df$B23001_059 + EduAtt_df$B23001_145)/EduAtt_df$B23001_001);
EduAtt_df$percent_age62_64 <- 100*((EduAtt_df$B23001_066 + EduAtt_df$B23001_152)/EduAtt_df$B23001_001);
EduAtt_df$percent_age65_69 <- 100*((EduAtt_df$B23001_073 + EduAtt_df$B23001_159)/EduAtt_df$B23001_001);
EduAtt_df$percent_age70_74 <- 100*((EduAtt_df$B23001_078 + EduAtt_df$B23001_164)/EduAtt_df$B23001_001);
EduAtt_df$percent_age75over <- 100*((EduAtt_df$B23001_083 + EduAtt_df$B23001_169)/EduAtt_df$B23001_001);
EduAtt_df$percentUnemp_age16_19 <- 100*((EduAtt_df$B23001_008 + EduAtt_df$B23001_094)/(EduAtt_df$B23001_003 + EduAtt_df$B23001_089));
EduAtt_df$percentUnemp_age20_21 <- 100*((EduAtt_df$B23001_015 + EduAtt_df$B23001_101)/(EduAtt_df$B23001_010 + EduAtt_df$B23001_096));
EduAtt_df$percentUnemp_age22_24 <- 100*((EduAtt_df$B23001_022 + EduAtt_df$B23001_108)/(EduAtt_df$B23001_017 + EduAtt_df$B23001_103));
EduAtt_df$percentUnemp_age25_29 <- 100*((EduAtt_df$B23001_029 + EduAtt_df$B23001_115)/(EduAtt_df$B23001_024 + EduAtt_df$B23001_110));
EduAtt_df$percentUnemp_age30_34 <- 100*((EduAtt_df$B23001_036 + EduAtt_df$B23001_122)/(EduAtt_df$B23001_031 + EduAtt_df$B23001_117));
EduAtt_df$percentUnemp_age35_44 <- 100*((EduAtt_df$B23001_043 + EduAtt_df$B23001_129)/(EduAtt_df$B23001_038 + EduAtt_df$B23001_124));
EduAtt_df$percentUnemp_age45_54 <- 100*((EduAtt_df$B23001_050 + EduAtt_df$B23001_136)/(EduAtt_df$B23001_045 + EduAtt_df$B23001_131));
EduAtt_df$percentUnemp_age55_59 <- 100*((EduAtt_df$B23001_057 + EduAtt_df$B23001_143)/(EduAtt_df$B23001_052 + EduAtt_df$B23001_138));
EduAtt_df$percentUnemp_age60_61 <- 100*((EduAtt_df$B23001_064 + EduAtt_df$B23001_150)/(EduAtt_df$B23001_059 + EduAtt_df$B23001_145));
EduAtt_df$percentUnemp_age62_64 <- 100*((EduAtt_df$B23001_071 + EduAtt_df$B23001_157)/(EduAtt_df$B23001_066 + EduAtt_df$B23001_152));
EduAtt_df$percentUnemp_age65_69 <- 100*((EduAtt_df$B23001_076 + EduAtt_df$B23001_162)/(EduAtt_df$B23001_073 + EduAtt_df$B23001_159));
EduAtt_df$percentUnemp_age70_74 <- 100*((EduAtt_df$B23001_081 + EduAtt_df$B23001_167)/(EduAtt_df$B23001_078 + EduAtt_df$B23001_164));
EduAtt_df$percentUnemp_age75over <- 100*((EduAtt_df$B23001_086 + EduAtt_df$B23001_172)/(EduAtt_df$B23001_083 + EduAtt_df$B23001_169));
一个例子是我使用selectInput
获取percent_age22_24
作为我的选择。在observe
代码中我有:
eduAttainValue <- {
switch(names(eduChoices[eduChoices==input$eduattain]),
"percent_no_school"=EduAtt_merged$no_school,
"percent_Nursery"=EduAtt_merged$Nursery,
"percent_g1st"=EduAtt_merged$g1st,
"percent_g2nd"=EduAtt_merged$g2nd,
"percent_g3rd"=EduAtt_merged$g3rd,
"percent_g4th"=EduAtt_merged$g4th,
"percent_g5th"=EduAtt_merged$g5th,
"percent_g6th"=EduAtt_merged$g6th,
"percent_g7th"=EduAtt_merged$g7th,
"percent_g8th"=EduAtt_merged$g8th,
"percent_g9th"=EduAtt_merged$g9th,
"percent_g10th"=EduAtt_merged$g10th,
"percent_g11th"=EduAtt_merged$g11th,
"percent_g12th"=EduAtt_merged$g12th,
"percent_HS"=EduAtt_merged$HS,
"percent_GED"=EduAtt_merged$GED,
"percent_col_less1"=EduAtt_merged$col_less1,
"percent_col_1nodegree"=EduAtt_merged$col_1nodegree,
"percent_AS"=EduAtt_merged$AS,
"percent_BA"=EduAtt_merged$BA,
"percent_MA"=EduAtt_merged$MA,
"percent_Prof"=EduAtt_merged$Prof,
"percent_PHd"=EduAtt_merged$PHd
)
}
popup <- paste0(EduAtt_merged$areaName, "<br>","GEOID: ", EduAtt_merged$GEOID, "<br>", popupLabelText, round(EduAtt_merged[[input$eduattain]],2), "% (", eduAttainValue, ")<br>Of total population of: ", prettyNum(EduAtt_df$total, scientific=FALSE, big.mark=","));
弹出窗口没有eduAttainValue
显示的值。