2014-04-28 19 views
11

Shiny를 사용하여 앱을 작성하려고합니다. 그것은 2 개의 드롭 다운을 포함 할 것이고, 하나는 7 개의 다른 섹터 (은행, 철강, fmcg 등)의 이름을 보여줄 것입니다. 다른 드롭 다운에는 선택된 섹터의 회사 명 목록이 포함되어야합니다. 즉, 이름 목록은 동적입니다.선택 입력 선택 변경하기 Shiny

1. 두 번째 드롭 다운의 이름 목록을 동적으로 변경하는 방법에 대해 알지 못합니다. 예를 들어. 우리는 우리가 "은행"을 선택하면 "IT가"2 드롭 다운이 등 "인포시스를", "TCS"이 포함되어야 선택하면, 2 드롭 다운 등 "SBI", "ICICI"

ui.R

library(shiny) 
shinyUI(fluidPage(
titlePanel("Forecasting of stock prices and their accuracies"), 

sidebarLayout(
sidebarPanel(
radioButtons("rd", 
      label="Select time range for training dataset", 
      choices=list("23 month","18 month","12 month","6 month"), 
      selected="23 months"), 

selectInput("sector", 
      label="select a sector",choices=list("IT"=1,"Bank"=2,"Power"=3,"Steel"=4,   
"FMCG"=5,"Infrastructure"=6,"Automobile"=7),     
      selected=1), 


selectInput("stock", 
      label="select a option",choices=list("co.1"=1,"co.2"=2, 
"co.3"=3,"co.4"=4,"co.5"=5," 
co.6"=6,"co.7"=7,"co.8"=8), 
      selected=1) 

), 
mainPanel("Display results", 
     textOutput("summary"), 
     tableOutput("view")) 
) 
)) 
을 보여 musr 데이터가 산업과 주식을 대표하는 변수와 data.frame에있는 경우

server.R

shinyServer(function(input, output) { 
datasetInput <- reactive({ 
if(input$sector=="1"){ 
switch(input$stock, 
       "1" = Infy, 
       "2" = TCS, 
       "3" = Wipro, 
       "4" = TechM)} 

else if(input$sector=="2"){ 
    switch(input$stock, 
      "1" = SBIN, 
      "2" = ICICI, 
      "3" = HDFC, 
      "4" = Axis, 
      "5" = IDBI, 
      "6" = PSB, 
      "7" = BOI, 
      "8" = Bob 
    )} 
}) 

output$view<-renderTable({ 
head(datasetInput(),n=10) 
}) 

}) 
+2

이 예는 https://gist.github.com/wch/4211337에 있고, 당신이 찾을 수 있습니다 다른 사람들은 당신의 마음에 드는 검색 엔진에'r 반짝 이는 동적 선택 입력'을 입력하여. – Andrie

+1

@Andrie Google에 입력 한이 페이지가 있습니다. – Jeff

답변

9

, 동적 두 번째 selectInput을 만들 renderUI를 사용할 수 있습니다.

ui.R

library(shiny) 
shinyUI(fluidPage(
    titlePanel("Forecasting of stock prices and their accuracies"), 

    sidebarLayout(
    sidebarPanel(
     radioButtons("rd", 
        label="Select time range for training dataset", 
        choices=list("23 month","18 month","12 month","6 month"), 
        selected="23 months"), 
     uiOutput("Box1"), 
     uiOutput("Box2") 
    ), 
    mainPanel("Display results", 
       tableOutput("view")) 
) 
)) 

server.R

library(shiny) 
biz = data.frame(
    Sector = c("a", "a", "a", "a", "b", "b", "b", "b", "b", "b", "b", "b"), 
    Stock = c("Infy","TCS","Wipro","TechM","SBIN","ICICI","HDFC", "Axis", "IDBI", "PSB","BOI","Bob"), 
    stringsAsFactors = FALSE 
) 
shinyServer(function(input, output) { 


    output$Box1 = renderUI(selectInput("sector","select a sector",c(unique(biz$Sector),"pick one"),"pick one")) 


    output$Box2 = renderUI(
    if (is.null(input$sector) || input$sector == "pick one"){return() 
    }else selectInput("stock", 
         "Select a stock", 
         c(unique(biz$Stock[which(biz$Sector == input$sector)]),"pick one"), 
         "pick one") 
) 


    subdata1 = reactive(biz[which(biz$Sector == input$sector),]) 
    subdata2 = reactive(subdata1()[which(subdata1()$Stock == input$stock),]) 

    output$view = renderTable({ 
    if(is.null(input$sector) || is.null(input$stock)){return() 
    } else if (input$sector == "pick one" || input$stock == "pick one"){return() 

    } else return(subdata2()) 
    }) 

})