I am trying to calculate a new variable in a dataframe in Shiny that is calculated conditionally on another variable.
Here's a small example of what I'm trying to do:
mydata <- data.frame(cbind(x = 1, y = 1:10))
value <- 10 #from user input
mydata$z[mydata$y >= 5] <- mydata$y[mydata$y >= 5] + value
mydata$z[mydata$y < 5] <- mydata$y[mydata$y < 5] - value
Here's my ui.R file:
#Library
library("shiny")
# Define UI for miles per gallon application
shinyUI(pageWithSidebar(
# Application title
headerPanel("Test"),
sidebarPanel(
numericInput("value", "Enter Value:", 10)
),
mainPanel(
tableOutput("table")
)
)
)
Here's my server.R file:
#Libraries
library(shiny)
#Load Data
mydata <- data.frame(cbind(x = 1, y = 1:10))
# Define server logic
shinyServer(function(input, output) {
mydata$z[mydata$y >= 5] <- reactive({
mydata$y + input$value
})
mydata$z[mydata$y < 5] <- reactive({
mydata$y - input$value
})
output$table <- renderTable({
mydata
})
})
With this Shiny code, I receive the following error:
Error in mydata$z[mydata$y >= 5] <- reactive({ : invalid type/length (closure/0) in vector allocation
I've tried different ways of subsetting and assigning, but I'm stuck. Your help is greatly appreciated!