I am trying to compute a new variable in a data frame in Shiny, which is computed conditionally on another variable.
Here is 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 is 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 is 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 brilliant code, I get the following error:
Error in mydata $ z [mydata $ y> = 5] <- reactive ({: invalid type / length (closure / 0) in vector distribution
I tried different ways of subset and assignment, but I'm stuck. Your help is much appreciated!
r shiny dataframe
dadrivr
source share