Hi fellow Shiny users,
I would like my users to be able to add new variables into a master data frame. The users will type the definition using textInput. Then we will add it to the data frame using server.R. Here's my code. I am not able to make it work. Any help will be greatly appreciated. Thank you!
Rawdata:
colA <- c('1','2','3','3','2')
colB <- c('1','1','3','3','2')
colC <- c('14','12','33','33','26')
colD <- c('Value','Mainstream','Value','Premium','Premium')
colE <- c(1,2,3,4,5)
rawdata <- as.data.frame(cbind(colA,colB, colC, colD, colE))
View(rawdata)
ui.R:
fluidPage(
sidebarLayout(
sidebarPanel(
textInput("addVar", "New attribute definition"),
helpText("Note: Type the attribute definition using R code."),
helpText("For example:"),
helpText("data$Value <- ifelse (data$price_tiers_new == 'Value', 1, 0)"),
br(),
actionButton("addButton", strong("Add!")),
width = 3
),
mainPanel(
verticalLayout(
br()
#Will display histogram of the newly added variables
)
)
)
)
server.R:
function(input, output, session) {
curr <- reactiveValues()
curr$df <- rawdata
observeEvent(input$addButton, {
eval(parse(text=input$filter))
})
}
For example, here are two new variable definitions to try. If we add the first definition, rawdata will have one additional column (Value). If we add the second definition, rawdata will have two additional columns (Value and Premium).
curr$df$Value <- ifelse(curr$df$colD == 'Value', 1, 0)
curr$df$Premium <- ifelse(curr$df$colD == 'Premium', 1, 0)