简体   繁体   English

产生动态/多个输入框以根据Shiny R中的用户选择收集数据

[英]Producing dynamic/multiple input boxes to collect data depending on user selection in Shiny R

I am trying to build an app in Shiny that (1) asks user for number of Assets in his/her portfolio. 我正在尝试在Shiny中构建一个应用程序,该应用程序(1)要求用户提供其投资组合中的资产数量。 Depending on the numeric entry, (2) the user is presented with one numeric box to enter the % of portfolio owned AND the Ticker/name of the Asset. 根据数字输入,(2)将为用户显示一个数字框,以输入所拥有投资组合的百分比和资产的股票行情代码/名称。 For instance, If the user enters 3 for number of assets in portfolio, he will be presented with something like this: 例如,如果用户输入3作为投资组合中资产的数量,将向他显示以下内容:

Asset1 ----> Enter ticker______ Enter Wight______ 资产1 ---->输入代码______输入权重______

Asset2 ----> Enter ticker______ Enter Wight______ 资产2 ---->输入代码______输入权重______

Asset3 ----> Enter ticker______ Enter Wight______ Asset3 ---->输入代码______输入权重______

This is suppose to be dynamic so the greater the number of assets, the greater the input fields. 假定这是动态的,因此资产数量越多,输入字段就越大。 Finally, in step (3), I want to save the entered information for each Asset in a table and display the table. 最后,在步骤(3)中,我想将每个资产的输入信息保存在表中并显示该表。

Here is what I have got and it is no where near what I need it to be. 这就是我所拥有的,而且离我需要的东西也不远。 I am totally new to Shiny and that is half the reason for my troubles: 我对Shiny完全陌生,这是造成麻烦的一半:

UI.R 用户界面

 shinyUI(pageWithSidebar (

  headerPanel( "Portfolio Returns"),

  sidebarPanel(

    numericInput("assets", label = "Enter Total Assets", value="")

  ),
 mainPanel(

tableOutput("table"))     
 )
) 

server.R 服务器

shinyServer(
  function(input,output) {
  output$DynamicAssets <- renderUI ({

  Assets <- as.integer(input$assets)

  for(i in 1:Assets,function(i) {
  "ticker" = textInput("Ticker", label="Enter Ticker", value="Enter Ticker"),

  "weight" = numericInput ("AssetWeight", label="weights of Assets", value="")

     })
    })

  })
 })

I know the code is in complete because i have no idea what to do next. 我知道代码已经完成,因为我不知道下一步该怎么做。 this is all that i figured out from seraching the net. 这就是我从搜索网络中发现的所有内容。 Your help would be greatly appreciated. 您的帮助将不胜感激。

ui.R 用户界面

library(shiny)

shinyUI(pageWithSidebar (

  headerPanel( "Portfolio Returns"),

  sidebarPanel(
    numericInput("assets", label = "Enter Number of Assets in Portfolio", value="1"),
    uiOutput("tickers")
  ),
  mainPanel()
)) 

server.R 服务器
-Note that to pass multiple items in the renderUI() function you have to group them into a list, and here lapply() is creating a list of lists. -请注意,要在renderUI()函数中传递多个项目,您必须将它们分组为一个列表,在这里lapply()正在创建一个列表列表。

library(shiny)

shinyServer( function(input, output, session) {

  output$tickers <- renderUI({
    numAssets <- as.integer(input$assets)

    lapply(1:numAssets, function(i) {
      list(tags$p(tags$u(h4(paste0("Asset ", i)))),
           textInput(paste0("ticker", i), label = "Ticker Name", value = "Enter ticker..."),
           numericInput(paste0("weight", i), label = "Weight of Asset", value=0))  
    })
  })
})  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM