R 闪亮更新选择输入不显示值下拉菜单空白
我创建了第二个输入字段“选择一个国家”,用于基于侧面板上先前输入字段“选择一个地区”的国家图 我使用指定数据表的“国家”列作为名称,这些名称将在选择每个“地区”(也是一列)后显示,并带有updateSelectInput 出于某种原因,它在我创建的一个名为“性别”的选项卡中工作,该选项卡的格式完全相同,只是使用的数据表不同。我几乎复制并粘贴了“性别”选项卡中“最高教育”选项卡中的代码块。但是对于“最高教育”选项卡,当我选择某个特定地区时,“选择一个国家”的下拉菜单为空,尽管我可以看到它加载了该地区列表中第一个国家的绘图 我一直在玩弄变量“国家”的类型,在因素和性格之间切换。目前,代码仅适用于“性别”选项卡。我束手无策 有人能发现我的代码有什么问题吗 数据集如下所示:R 闪亮更新选择输入不显示值下拉菜单空白,r,drop-down-menu,ggplot2,shiny,rworldmap,R,Drop Down Menu,Ggplot2,Shiny,Rworldmap,我创建了第二个输入字段“选择一个国家”,用于基于侧面板上先前输入字段“选择一个地区”的国家图 我使用指定数据表的“国家”列作为名称,这些名称将在选择每个“地区”(也是一列)后显示,并带有updateSelectInput 出于某种原因,它在我创建的一个名为“性别”的选项卡中工作,该选项卡的格式完全相同,只是使用的数据表不同。我几乎复制并粘贴了“性别”选项卡中“最高教育”选项卡中的代码块。但是对于“最高教育”选项卡,当我选择某个特定地区时,“选择一个国家”的下拉菜单为空,尽管我可以看到它加载了该地
iso3 region participation country male female lower class
ALB region2 0.5262631 Albania 0.5834176 0.4702970 0.4285714
AND region1 0.6699900 Andorra 0.7236581 0.6160000 0.4117647
ARG region4 0.2857675 Argentina 0.3109277 0.2631020 0.2270694
“性别选项卡”的数据集如下:
“最高学历”选项卡的数据集如下所示:
data.frame': 85 obs. of 4 variables:
$ region : Factor w/ 7 levels "region1","region2",..: 2 1 4 2 7 3 5 3 2 2 ...
$ country: Factor w/ 85 levels "Albania","Algeria",..: 1 3 4 5 6 7 13 8 12 10 ...
$ male : num 0.58 0.72 0.31 1 0.67 0.45 0.41 0.62 0.21 0.53 ...
$ female : num 0.47 0.62 0.26 1 0.67 0.4 0.24 0.38 0.16 0.35 ...
'data.frame': 85 obs. of 8 variables:
$ region : Factor w/ 7 levels "region1","region2",..: 2 1 4 2 7 3 5 3 2 2
$ country : Factor w/ 85 levels "Albania","Algeria",..: 1 3 4 5 6 7 13 8
$ Primary : num 0.456 0.525 0.215 1 0.519 ...
$ Secondary.incomplete : num 0.489 0.614 0.337 0.995 0.727 ...
$ Secondary.vocational : num 0.561 0.681 0.324 1 0.768 ...
$ Secondary.preparatory: num 0.583 0.632 0.492 0.998 0.793 ...
$ Tertiary.incomplete : num 0.696 0.732 0.545 0.981 0.802 ...
$ Tertiary : num 0.728 0.833 0.625 0.997 0.854 ...
用户界面
库(闪亮)
dataset其他人指出了我的问题:
我只有一个国家/地区的SelectInput字段,但我有两个选项卡。这会混淆浏览器。
因此,我创建了一个单独的chuck代码,指定server.R中的输入,并为ui.R使用不同的inputID
用户界面
服务器.R
observe({
region = input$region
updateSelectInput(session, "country2",
choices =
levels(as.factor(as.character(wvs_c$country[wvs_c$region==region]))), selected =
levels(as.factor(as.character(wvs_c$country[wvs_c$region==region])))[1]
)
})
# then in the codes for highest education country plot
....
} else {
region = input$region
country = input$country2
library(rworldmap)
library(plyr)
library(reshape)
library(ggplot2)
wvs_c <- read.csv("./wvs_c")
wvs_c <- wvs_c[, -1]
shinyServer(function(input,output,session) {
gender <- wvs_c[,c(2, 4:6)]
highested <- wvs_c[,c(2, 4, 12:17)]
colnames(highested) <- c("region", "country", "Primary", "Secondary.incomplete",
"Secondary.vocational","Secondary.preparatory",
"Tertiary.incomplete", "Tertiary")
# Create a second field of input "Select a country" based on the first input field
"Select a region"
observe({
region = input$region
updateSelectInput(session, "country",
choices = levels(as.factor(as.character(wvs_c$country[wvs_c$region==region]))),
selected = levels(as.factor(as.character(wvs_c$country[wvs_c$region==region])))[1]
)
})
# Create charts for each country's gender breakdown
selectedPlot <- reactive({
if (input$region == "the world") {
#for regional average of gender
test<- aggregate(gender[, c("male", "female")], by =
list(as.character(gender$region)), function(x) c(mean=mean(x)))
colnames(test)[1] <- "region"
test2 <- melt(test[,c('region','male','female')],id.vars = 1)
## codes for ggplot using "test2", works
} else {
region = input$region
country = input$country
cbbPalette <- c("#01DFD7", "#F78181")
x <- gender[(gender$country== country),]
x <- melt(x[,c('country','male','female')], id.vars = 1)
x1 <- ggplot(data=x, aes(x=variable, y=value))
x1 <- x1 + geom_bar(aes(fill = variable), position="dodge", stat="identity") +
scale_fill_manual(values=cbbPalette)
x1 <- x1 + labs(y = 'Percent of members', x = '') + theme(axis.text.x =
element_blank()) + ylim(0, 1) + theme(legend.title=element_blank())
x1
}
})
output$myplot = renderPlot({
selectedPlot()
}
)
# Create charts for each country's educational level breakdown
selectedPlot1 <- reactive({
if (input$region == "the world") {
test3 <- aggregate(highested_data[, c('Primary', 'Secondary.incomplete',
'Secondary.vocational','Secondary.preparatory', 'Tertiary.incomplete',
'Tertiary')], by = list(as.character(highested_data$region)), function(x)
c(mean=mean(x)))
colnames(test3)[1] <- "region"
test3 <- melt(test3[, c
('region','Primary','Secondary.incomplete','Secondary.vocational'
,'Secondary.preparatory','Tertiary.incomplete','Tertiary')],
id.vars = 1)
## codes for ggplot using "test3", works
} else {
region = input$region
country = input$country
cbbPalette1 <- c("#F7BE81", "#F79F81", "#82FA58", "#04B486", "#00BFFF",
"#01A9DB")
y <- highested[(highested$country == country),]
y <-melt(y[,
c('country','Primary','Secondary.incomplete','Secondary.vocational'
,'Secondary.preparatory','Tertiary.incomplete','Tertiary')], id.vars = 1)
y1 <- ggplot(data=y, aes(x=variable, y=value))
y1 <- y1 + geom_bar(aes(fill = variable), position="dodge", stat="identity")
+ scale_fill_manual(values=cbbPalette1)
y1 <- y1 + labs(y = 'Percent of members', x = '') + theme(axis.text.x =
element_blank()) + ylim(0, 1) +
theme(legend.title=element_blank())
y1
}
})
output$myplot1 = renderPlot({
selectedPlot1()
}
)
tabPanel('Highest education attained', dataTableOutput('mytable1'),
selectInput('country2', 'Select a Country:',
names(wvs_c$country), selected=names(wvs_c$country)[1]),
plotOutput("myplot1")
)
observe({
region = input$region
updateSelectInput(session, "country2",
choices =
levels(as.factor(as.character(wvs_c$country[wvs_c$region==region]))), selected =
levels(as.factor(as.character(wvs_c$country[wvs_c$region==region])))[1]
)
})
# then in the codes for highest education country plot
....
} else {
region = input$region
country = input$country2