New variable in dataset/data table based on user inputs in R shiny

I'm trying to make a new variable in my dataset called "total_cost" based on user inputs on the side bar.

What I would like to do is have a user go in, set a value for the numeric input for "Phase 1", "Phase 2" etc., then click "submit".

When they click submit, I would like for a reactive data table to show the variable they just made, in addition to the columns that were already there.

Like this:

Data Set Column 1 ; Data Set Column 2; Data Set Column 3; total_Cost.

Any help would be greatly appreciated as I'm a bit lost on how to make these data tables update based on user input. And the values in these data tables are critical for building the other graphics/charts/variables in my app.

library(shiny)


datatableforapp<-dataset %>% select(1-3)

ui <- fixedPage(
  
  
  titlePanel("Title"),
  sidebarLayout(
    sidebarPanel( position = "left",
            numericInput("Phase_1 ", "Phase 1 Cost", 
                         value = 0, min = 0, max = 100),
            numericInput("Phase_2 ", "Phase 2 Cost",, 
                         value = 0, min = 0, max = 100),
            numericInput("Phase_3", "Phase 3 Cost", 
                         value = 0, min = 0, max = 100),
            numericInput("Phase_4 ", "Phase 4 Cost",, 
                         value = 0, min = 0, max = 100),
            numericInput("Phase_5 ", "Phase 5 Cost",, 
                         value = 0, min = 0, max = 100),
            numericInput("Phase_6 ", "Phase 6 Cost",, 
                         value = 0, min = 0, max = 100),
        ),
    mainPanel(
  tableOutput("table"),
     tableOutput("table1"))))


server <- function(input, output, session) {

dynamic1 <- reactive({
    dynamic1_num <- input$Phase_1 + input$Phase_2 + input$Phase_3 + input$Phase_4 + input$Phase_5 + +input$Phase_6
    
    data.frame(dynamic1_num,datatableforapp)})

output$dynamic <- renderTable(dynamic)
output$dynamic1<- renderTable(dynamic1())}


# Run the application 
shinyApp(ui = ui, server = server)

You have issues with adding superfluous empty spaces when you define your numericInputs Id, I omitted these.
Also the ID's you give your UI Outputs, need to be output$IDNAMES that you set renderfunctions for. In this case
output$table1 for example.

Additionally your app was not reproducible as it was based on your private dataset so I replaced this with head(iris) which is available for all R users.

library(shiny)


datatableforapp<-head(iris) %>% select(1-3)

ui <- fixedPage(
  
  
  titlePanel("Title"),
  sidebarLayout(
    sidebarPanel( position = "left",
                  numericInput("Phase_1", "Phase 1 Cost", 
                               value = 0, min = 0, max = 100),
                  numericInput("Phase_2", "Phase 2 Cost",, 
                               value = 0, min = 0, max = 100),
                  numericInput("Phase_3", "Phase 3 Cost", 
                               value = 0, min = 0, max = 100),
                  numericInput("Phase_4", "Phase 4 Cost",, 
                               value = 0, min = 0, max = 100),
                  numericInput("Phase_5", "Phase 5 Cost",, 
                               value = 0, min = 0, max = 100),
                  numericInput("Phase_6", "Phase 6 Cost",, 
                               value = 0, min = 0, max = 100),
    ),
    mainPanel(
      tableOutput("table1"))))


server <- function(input, output, session) {
  
  dynamic1 <- reactive({

    dynamic1_num <- sum(req(c(input$Phase_1 , 
      input$Phase_2 , input$Phase_3 , input$Phase_4, input$Phase_5 , input$Phase_6)))
    data.frame(dynamic1_num,datatableforapp)})
  
  output$table1 <- renderTable(req(dynamic1()))
  }


# Run the application 
shinyApp(ui = ui, server = server)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.