2014-04-28 4 views
11

Sto provando a creare un'app utilizzando Shiny. Conterrà 2 elenchi a discesa, uno mostrerà il nome di 7 diversi settori- esso, banca, acciaio, fmcg ecc. L'altro elenco a discesa dovrebbe contenere l'elenco del nome delle aziende del settore selezionato, l'elenco dei nomi sarà dinamico.Modifica delle opzioni di selezione della selezione in r Shiny

1. Non ho idea di come modificare dinamicamente l'elenco dei nomi del secondo menu a discesa, ad es. se selezioniamo "IT" 2 ° discesa dovrebbe contenere "Infosys", "TCS", ecc, se selezioniamo "Banca", 2 ° discesa musr mostrare "SBI", "ICICI", ecc

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")) 
) 
)) 

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

C'è un esempio a https://gist.github.com/wch/4211337, e si possono trovare altri digitando 'r lucido selezione dinamica input 'nel tuo motore di ricerca preferito. – Andrie

+1

@Andrie l'ho digitato su Google e ho trovato questa pagina – Jeff

risposta

9

Se i dati sono in un data.frame con le variabili per rappresentare l'industria e azioni, è possibile utilizzare renderUI per creare dinamicamente il secondo selectInput.

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()) 
    }) 

})