根据Shiny R中的用户选择,生成动态/多个输入框以收集数据 [英] Producing dynamic/multiple input boxes to collect data depending on user selection in Shiny R

查看:161
本文介绍了根据Shiny R中的用户选择,生成动态/多个输入框以收集数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Shiny中构建一个应用程序,(1)向用户询问他/她的投资组合中的资产数量。根据数字条目,(2)用户被呈现一个数字框,以输入所有的投资组合的百分比和股票代码/资产的名称。
例如,如果用户为投资组合中的资产数量输入3,他将被呈现如下:



Asset1 ----> Enter tick er ____ ight ight ight ight ight ight Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter ticker______输入Wight ______



这是假设是动态的,所以资产数量越多,输入字段越大。
最后,在步骤(3)中,我想将每个资产的输入信息保存在表格中并显示表格。



这是我所拥有的得到了,我不需要它就在附近。我完全是新来的,这是我的麻烦的一半原因:



UI.R

  ShinyUI(pageWithSidebar(

headerPanel(Portfolio Returns),

sidebarPanel(

numericInput ,label =输入总资产,value =)

),
mainPanel(

tableOutput(table))


server.R

  ShinyServer(
函数(输入,输出){
输出$ DynamicAssets< - renderUI({

资产& as.integer(输入$ assets)

for(i in 1:Assets,function(i){
ticker= textInput(Ticker,label =Enter Ticker value =输入代码)

weight= numericInput(AssetWeight,label =资产的权重,value =)

})
})

})
})

我知道代码是完整的,因为我不知道下一步该怎么做。这是我从网路上清除的一切。您的帮助将不胜感激。

解决方案

ui.R

库(闪亮)

ShinyUI(pageWithSidebar(

headerPanel(Portfolio Returns),

sidebarPanel (
numericInput(assets,label =输入投资组合中的资产数量,value =1),
uiOutput(tickers)
),
mainPanel ()
))

server.R

- 注意要在 renderUI()函数中传递多个项目,您必须将它们分组到列表中,这里 lapply()正在创建列表列表。

 库(闪亮)

shinyServer(function(input,output ,会话){

输出$ tickers< - renderUI({
numAssets< - as.integer(输入$ assets)

lapply(1:numAssets ,function(i){
list(tags $ p(tags $ u(h4(paste0(Asset,i)))),
textInput(paste0(ticker,i) =代号Na我的价值=输入代码...)
numericInput(paste0(weight,i),label =资产重量,value = 0))
})
})
})


I am trying to build an app in Shiny that (1) asks user for number of Assets in his/her portfolio. 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. For instance, If the user enters 3 for number of assets in portfolio, he will be presented with something like this:

Asset1 ----> Enter ticker______ Enter Wight______

Asset2 ----> Enter ticker______ Enter Wight______

Asset3 ----> Enter ticker______ Enter Wight______

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.

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:

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.

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

这篇关于根据Shiny R中的用户选择,生成动态/多个输入框以收集数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆