Matt
Matt

Reputation: 53

R Shiny - calculation using data in csv and user numericInput

I am trying to create a Shiny app where a user can upload a csv file, input a numeric value, add the numeric value to a specific column of data in the csv, then save the raw + calculated data in a table. I generated a simplified Shiny script below using a simple csv file.

enter image description here

When I try and run the app, I get the error:

Problem with mutate() input sum. [31mx[39m non-numeric argument to binary operator [34mi[39m Input sum is A + C.

I tried looking up some examples of how to fix this, but was unable to find something that utilized eventReactive() to keep the data table updated with the new calculated data. Any help is greatly appreciated.

library(shiny) library(dplyr)

ui <- fluidPage(

# Application title
titlePanel("Test"),

# Show a plot of the generated distribution
    mainPanel(
      
        fileInput(
            inputId = "csvFile",
            label = "Upload csv file",
            accept = c(".csv")),
        
        uiOutput("C"),
        uiOutput("D"),
        
        tableOutput("modifiedData")
    
    )
)

server <- function(input, output) {

output$C <- renderUI(
    {numericInput("C", "Variable C", 0)}
    )
output$D <- renderUI(
    {numericInput("D", "Variable D", 0)}
)

userData <- eventReactive(input$csvFile,
                          {
                              
                              req(input$csvFile)
                              
                              raw_df <- read.csv(input$csvFile$datapath)
                              calc_df <- raw_df %>%
                              
                              mutate(sum = A + C)
                              
                          }) 

output$modifiedData <- renderTable({userData()})

}

shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 574

Answers (1)

YBS
YBS

Reputation: 21287

You should be using the reactive variable input$C. Also, you should make the eventReactive dependent on input$C. Try this

ui <- fluidPage(
  
  # Application title
  titlePanel("Test"),
  
  # Show a plot of the generated distribution
  mainPanel(
    
    fileInput(
      inputId = "csvFile",
      label = "Upload csv file",
      accept = c(".csv")),
    
    uiOutput("C"),
    uiOutput("D"),
    
    tableOutput("modifiedData")
    
  )
)

server <- function(input, output) {
  
  output$C <- renderUI(
    {numericInput("C", "Variable C", 0)}
  )
  output$D <- renderUI(
    {numericInput("D", "Variable D", 0)}
  )
  
  userData <- eventReactive(list(input$csvFile, input$C),
                            {
                              
                              req(input$csvFile)
                              
                              raw_df <- read.csv(input$csvFile$datapath)
                              calc_df <- raw_df %>%
                                
                                mutate(sum = A + input$C)
                              
                            }) 
  
  output$modifiedData <- renderTable({userData()})
}

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions