我希望将两个变量(“ticker”和"SharesVec")定义为空向量,并在触发按钮“操作”时向这些向量添加文本输入和数字输入(分别)。稍后我将使用这些向量来运行一个函数。我不确定我是否理解如何在R shiny (初学者)中正确使用变量。这是我到目前为止尝试过的:
用户界面
shinyUI(fluidPage(
titlePanel("Portfolio Analysis Tool"),
br(),
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "DateFrom",
label = "Starting Year (YYYY-01-01)",
choices = format(Sys.Date(), "%Y"):1975
),
selectInput(
inputId = "DateTo",
label = "Ending Year (YYYY-12-31)",
choices = format(Sys.Date(), "%Y"):1975
),
h2("Initial Portfolio"),
p("Include every ticker owned at some point during the period (Number of shares = 0 if none are held at the starting date)", style = "font-size: 12px"),
textInput("Stock","Ticker"),
numericInput("Shares","Number of Shares",0, min = 0, step = 0.5),
column(12,
splitLayout(cellWidths = c("58%", "58%"),
actionButton("action", "Add",icon("dollar-sign"),
style="color: #fff; background-color: #337ab7; border-color: #2e6da4"),
actionButton("action1", "Reset",icon("trash"),
style="color: #fff; background-color: #337ab7; border-color: #2e6da4"))),
br(),
br(),
h2("Rebalancing"),
dateInput("DateReb", "Date of Purchase/Sale"),
textInput("Stock1", "Ticker"),
numericInput("Shares1","Number of Shares (+/-)", 0, step = 0.5),
column(12,
splitLayout(cellWidths = c("58%", "58%"),
actionButton("action2", "Add",icon("dollar-sign"),
style="color: #fff; background-color: #337ab7; border-color: #2e6da4"),
actionButton("action3", "Reset",icon("trash"),
style="color: #fff; background-color: #337ab7; border-color: #2e6da4"))),
br(),
br(),
),
mainPanel(
column(10,
splitLayout(cellWidths = c("50%", "50%"),
htmlOutput("InitialHoldings", style = "font-weight: bold; text-decoration: underline"),
htmlOutput("Rebalancing", style = "font-weight: bold; text-decoration: underline"))),
br(),
br(),
column(12,
tableOutput("table"),
style = "height:340px; overflow-y: scroll; border: 1px solid #e3e3e3; border-radius: 8px; background-color: #f7f7f7;text-align: left"),
textOutput("TEST")
)
)
))服务器
library(quantmod)
library(PerformanceAnalytics)
tickers <- c()
SharesVec <- c()
shinyServer(function(input, output) {
output$InitialHoldings <- renderText({paste("Initial Holdings")})
output$Rebalancing <- renderText({paste("Rebalancing")})
#Store Initial Stocks/Nb of Shares from User Inputs
values <- reactiveValues()
values$df <- data.frame("Stock" = numeric(0), "Shares" = numeric(0))
newEntry <- observe({
if(input$action > 0) {
isolate(values$df[nrow(values$df) + 1,] <- c(input$Stock, input$Shares))
tickers <- c(tickers,input$Stock)
SharesVec <- c(SharesVec,input$Shares)
}
})
output$table <- renderTable({values$df})
output$TEST <- renderText({paste(tickers)})
})注意:我已经在代码中包含了output$TEST,以查看向量“ticker”是否正确地自我更新。另外,我想指出的是,由于我添加了行
tickers <- c(tickers,input$Stock)
SharesVec <- c(SharesVec,input$Shares)在服务器中,操作按钮不能正常工作(不再需要它来向我的表中添加数据)。我也尝试过使用全局变量->>,但似乎不起作用。有人能帮帮我吗?
发布于 2021-03-18 11:34:26
将向量存储在reactiveValues中。您可以将服务器函数更改为:
library(shiny)
shinyServer(function(input, output) {
output$InitialHoldings <- renderText({paste("Initial Holdings")})
output$Rebalancing <- renderText({paste("Rebalancing")})
#Store Initial Stocks/Nb of Shares from User Inputs
values <- reactiveValues(tickers = NULL, SharesVec = NULL)
observeEvent(input$action, {
values$tickers <- c(values$tickers,input$Stock)
values$SharesVec <- c(values$SharesVec,input$Shares)
})
output$TEST <- renderText({
paste(values$tickers, values$SharesVec, sep = ':', collapse = '\n')
})
}使用output$TEST向您显示存储的值。
https://stackoverflow.com/questions/66684107
复制相似问题