我是Shiny / R的新手,我无法弄清楚以下内容:
我想在selectInput
上绘制2 panel
。第一个我想选择一个州(例如" Alabama"),第二个我想选择与第一个小组中所选州相对应的城市。
这是代码:
TabCit <- read.csv(file= "~/Tab_Aux/Cities.csv", encoding = "UTF-8", header=TRUE, stringsAsFactors = TRUE, sep = ",")
ufname <- sort(as.vector(unique(TabCit[,8])))
ui <- fluidPage(
fluidRow(
column(1),
column(2,selectInput(inputId = "inufname", "variable", ufname , width = "100px")),
column(3,selectInput(inputId = "incidname", "variable", uiOutput(“cituf”) ))
)
)
server <- function(input,output) {
output$cituf <- renderUI({ as.vector (sort(TabCit[TabCit[,8]==input$inufname,7]))})
}
shinyApp(ui=ui, server=server)
数据TabCit的样本
&#34;编码1,&#34;代码2&#34;&#34; CODE3&#34;&#34;码4&#34;&#34;代码4&#34;&#34; Code6& #34;&#34;市&#34;&#34;国&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿巴埃特&#34;&#34; MG&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿巴埃特图巴&#34;&#34; PA&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; ABATIA&#34;&#34; PR&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; ABELARDO LUZ&#34;,&#34; SC&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; ABREU E LIMA&#34;,&#34; PE&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿赛兰迪亚&#34;&#34; MA&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿卡拉乌&#34;&#34; CE&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; ACAUA&#34;&#34; PI&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿克里兰迪亚&#34;&#34; AC&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿克莱乌纳&#34;&#34; GO&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; ACU&#34;&#34; RN&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿达曼蒂纳&#34;&#34; SP&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿杜斯蒂纳&#34;&#34; BA&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; AFOGADOS DA INGAZEIRA&#34;,&#34; PE&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; AFONSO BEZERRA&#34;,&#34; RN&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; AFONSO CLAUDIO&#34;,&#34; ES&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿夫拉尼乌&#34;&#34; PE&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; AGUA BOA&#34;,&#34; MT&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; AGUA BRANCA&#34;,&#34; AL&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; AGUA CLARA&#34;,&#34; MS&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿瓜伊&#34;&#34; SP&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; AGUAS BELAS&#34;,&#34; PE&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; AGUAS DE LINDOIA&#34;,&#34; SP&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; AGUAS DE SAO PEDRO&#34;,&#34; SP&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; AGUAS FORMOSAS&#34;,&#34; MG&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; AGUAS LINDAS DE GOIAS&#34;,&#34; GO&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿古多&#34;&#34; RS&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿古杜斯&#34;&#34; SP&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;艾莫雷斯&#34;&#34; MG&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; ALAGOA GRANDE&#34;,&#34; PB&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿拉戈伊尼亚斯&#34;&#34; BA&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; ALCANTARA&#34;&#34; MA&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿尔科巴萨&#34;&#34; BA&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; ALEGRE&#34;&#34; ES&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿莱格雷特&#34;&#34; RS&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; ALEGRETE DO PIAUI&#34;,&#34; PI&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; ALEM PARAIBA&#34;,&#34; MG&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;阿伦克尔&#34;&#34; PA&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ; ALEXANDRIA&#34;&#34; RN&#34; 1,&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34; 1&#34;&#34 ;亚历山尼亚&#34;&#34; GO&#34;
答案 0 :(得分:0)
此处您不需要uiOutput
。它仅在您想要动态生成新UI或交换旧UI元素时使用。
在您的情况下,使用observeEvent()
来捕获第一个selectInput
的更新并更新第二个更新就足够了:
TabCit <- read.csv(file= "~/Tab_Aux/Cities.csv", encoding = "UTF-8", header=TRUE, stringsAsFactors = TRUE, sep = ",")
ufname <- sort(as.vector(unique(TabCit[,8])))
library(shiny)
ui <- fluidPage(
fluidRow(
column(1),
column(2,selectInput(inputId = "inufname", "variable", ufname , width = "100px")),
column(3,selectInput(inputId = "incidname", "variable", "" )) # Create an empty selectInput
)
)
server <- function(input,output, session) { # include the session in your server
#Here you use observeEvent to catch changes in the variable input$inufname
#and update the content of your second selectInput
observeEvent(input$inufname,
updateSelectInput(session,inputId = "incidname", choices = as.vector(sort(TabCit[TabCit[,8]==input$inufname,7])))
)
}
shinyApp(ui=ui, server=server)