r - 更改 r Shiny 中的选择输入选项

标签 r shiny

我正在尝试使用 Shiny 构建一个应用程序。它将包含 2 个下拉列表,一个将显示 7 个不同行业的名称 - it,bank,steel,fmcg 等。另一个下拉列表应包含所选行业的公司名称列表,即名称列表将是动态的。

1.我不知道如何动态更改第二个下拉列表的名称列表,例如。如果我们选择“IT”,第二个下拉菜单应包含“infosys”、“tcs”等,如果我们选择“银行”,第二个下拉菜单应显示“SBI”、“ICICI”等。

用户界面

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

服务器
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)
})

})

最佳答案

如果您的数据位于 data.frame用变量来表示行业和股票,你可以使用 renderUI动态创建第二个 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())
  })

})

关于r - 更改 r Shiny 中的选择输入选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23334696/

相关文章:

R 减去基于类别的值和日期 tidyverse-way

r - 在 R 中使用历史方法对组件 VaR 没有贡献

javascript - 如何在 Shiny 中保存带有绘制形状/点的传单 map ?

css - 如何使 Shiny 的 'showNotification'可滚动?

r - 更改从在浏览器中打开的 Shiny 应用程序下载的绘图文件的文件名

r - 在 Rshiny/rcharts 中渲染后添加 highcharts 绘图带

r - 强制 Shiny 的应用程序在移动设备上显示桌面 View

r - 如果在dplyr下的前一行中满足条件,如何更改行中的值

r - 使用 sf 在 R 中交叉多边形

javascript - 如何从 Shiny 的 DT 中的单选按钮(使用 JS 回调制作)访问用户输入,并在一个 DT 中有不同的 JS 元素?