Hello folks,
I have data frame which is having name, Salary, hike_percent. My requirement is add 3 (c1,c2,c3) columns to same data frame from shiny app UI by entering below formula.
- c1= (salary * hike_percent)
- c2= "Salary Hike"
3.c3=100.35
All 3 columns trying to add one after one and same get displayed in data table. Below is the code developed. Unfortunately i am not able to achieve the scenario due to issue with "fun_newcolumns". Please help me to solve the problem.
library(shiny)
library(dplyr)
library(data.table)
library(DT)
library(shinyWidgets)
dt <- data.table('name' = c('AAA', 'BBB', 'CCC'),'Salary' = c(1000, 1500, 2500),'hike_percent' = c(0.10, 0.13, 0.25))
fun_newcolumns <- function(colname,formula) {
tran <- as.character(colname)
newv <- names(formula)
data <- dplyr::mutate_(dt, .dots = setNames(tran, newv))
data
}
#dt2 <- fun_newcolumns(colname=c1,formula=Salary )
# UI
ui <- fluidPage(sidebarLayout(
sidebarPanel( prettyCheckboxGroup(inputId = "columns",label = "exported list of columns",choices = unique(names(dt))
)),
mainPanel(
br(),
div(style = "display: inline-block;vertical-align:top left; width: 300px;", textInput("newcolumnname", "new column name")),
div(style = "display: inline-block;vertical-align:top centered; width: 500px;", textInput("formula", "enter formula here")),
div(style = "display: inline-block;vertical-align:top right; width: 100px;", actionButton("addnewcolumn", "Add new column")),
uiOutput("renderui"),
DT::DTOutput("data_tbl")
)
))
#SERVER
server <- function(input, output, session)
{
reactive_string <- eventReactive(input$addnewcolumn, { dt2 <- fun_newcolumns(input$colname,input$formula) })
output$renderui <- renderUI({ reactive_string() })
reactive_string2 <- eventReactive(input$addnewcolumn,{ dt2 })
output$data_tbl <- DT::renderDT({ reactive_string2() })
}
#Run the Shiny App to Display Webpage
shinyApp(ui = ui, server = server)