I must admit I am relatively new to R-Shiny so apologies if this is a very basic question.
I am trying to create an R-Shiny app which takes in user inputs in relation to an asset class' expected annual return, maximum weight and minimum weight. Ideally I would like to end up with a server dataframe similar to the below, with the expected returns and weights configurable:
Asset Class | Expected_Return | Max_Weight | Min_Weight |
---|---|---|---|
Equity | 0.08 | 1.00 | 0.25 |
Bonds | 0.02 | 0.75 | 0.10 |
Gold | 0.03 | 0.20 | 0.00 |
Property | 0.05 | 0.30 | 0.00 |
Cash | 0.01 | 0.10 | 0.00 |
My UI code:
library(shiny)
ui <- fluidPage(
fluidRow(
column(width = 1, align="center",
checkboxInput("equity_include", "Equity", value = TRUE, width = '100%')
),
column(width = 5, align="center",
numericInput("equity_er", "Global Expected Annual Return (%)", value = 0)
),
column(width = 3, align="center",
numericInput("equity_maxw", "Max Weight %", value = 100, min = 0, max = 100)
),
column(width = 3, align="center",
numericInput("equity_minw", "Min Weight %", value = 0, min = 0, max = 100)
)
),
fluidRow(
column(width = 1, align="center",
checkboxInput("bonds_include", "Bonds", value = TRUE, width = '100%')
),
column(width = 5, align="center",
numericInput("bonds_er", "Bonds Expected Annual Return (%)", value = 0)
),
column(width = 3, align="center",
numericInput("bonds_maxw", "Max Weight %", value = 100, min = 0, max = 100)
),
column(width = 3, align="center",
numericInput("bonds_minw", "Min Weight %", value = 0, min = 0, max = 100)
)
),
fluidRow(
column(width = 1, align="center",
checkboxInput("gold_include", "Gold", value = TRUE, width = '100%')
),
column(width = 5, align="center",
numericInput("gold_er", "Gold Expected Annual Return (%)", value = 0)
),
column(width = 3, align="center",
numericInput("gold_maxw", "Max Weight %", value = 100, min = 0, max = 100)
),
column(width = 3, align="center",
numericInput("gold_minw", "Min Weight %", value = 0, min = 0, max = 100)
)
),
fluidRow(
column(width = 1, align="center",
checkboxInput("property_include", "Property", value = TRUE, width = '100%')
),
column(width = 5, align="center",
numericInput("property_er", "Property Expected Annual Return(%)", value = 0)
),
column(width = 3, align="center",
numericInput("property_maxw", "Max Weight %", value = 100, min = 0, max = 100)
),
column(width = 3, align="center",
numericInput("property_minw", "Min Weight %", value = 0, min = 0, max = 100)
)
),
fluidRow(
column(width = 1, align="center",
checkboxInput("cash_include", "Cash", value = TRUE, width = '100%')
),
column(width = 5, align="center",
numericInput("cash_er", "Cash Expected Annual Return(%)", value = 0)
),
column(width = 3, align="center",
numericInput("cash_maxw", "Max Weight %", value = 100, min = 0, max = 100)
),
column(width = 3, align="center",
numericInput("cash_minw", "Min Weight %", value = 0, min = 0, max = 100)
)
),
)
My Server Code:
server <- function(input, output) {
library("tidyverse")
library(plotly) # To create interactive charts
library(timetk) # To manipulate the data series
#Create Dataframe of User Inputs
assets <- c("Equity", "Bonds", "Gold", "Property", "Cash")
include <- c(input$equity_include, input$bonds_include, input$gold_include, input$property_include, input$cash_include)
expected_return <- c(input$equity_er, input$bonds_er, input$gold_er, input$property_er, input$cash_er)
max_weight <- c(input$equity_maxw, input$bonds_maxw, input$gold_maxw, input$property_maxw, input$cash_maxw)
min_weight <- c(input$equity_minw, input$bonds_minw, input$gold_minw, input$property_minw, input$cash_minw)
user_inputs <- data.frame(assets, include, expected_return, max_weight, min_weight)
Unfortunately I am getting multiple reactivity errors which I have been unable to resolve using the typical reactive() function.
I'd really appreciate any help you may be able to offer on this issue.
Thank you in advance!
I know answers are usually supposed to solve your specific problem but I am quite lazy to type all those defaults, you can do this with editable data frames, I like to use the DT package for this
example based on:https://yihui.shinyapps.io/DT-edit/
library(shiny)
library(DT)
dt_output = function(title, id) {
fluidRow(column(
12, h1(paste0('Table ', sub('.*?([0-9]+)$', '\\1', id), ': ', title)),
hr(), DTOutput(id)
))
}
render_dt = function(data, editable = 'cell', server = TRUE, ...) {
renderDT(data, selection = 'none', server = server, editable = editable, ...)
}
shinyApp(
ui = fluidPage(
title = 'Double-click to edit table rows',
dt_output('server-side processing (editable = "row")', 'x6'),
),
server = function(input, output, session) {
d6 = iris
d6$Date = Sys.time() + seq_len(nrow(d6))
options(DT.options = list(pageLength = 5))
# server-side processing
output$x6 = render_dt(d6, 'row')
# edit a row
observeEvent(input$x6_cell_edit, {
d6 <<- editData(d6, input$x6_cell_edit, 'x6')
})
})