Dear All.
I am new to Shiny and stuck on this task for a while and I was hoping for some help out here. Codes work quiet fine in R but I get lost in when attempting this in shiny.
So my objective is to merge 2 df in shiny created via 1) a user input and 2) another df into one data frame. From the resultant dataframe, i want to perform a few operations and create new columns and render a table with the new variables. I want to achieve this by the click of an action button.
Would appreciate any help.
Thanks in advance.
Ps. code
###Data to be merged with user input.
agemo<-c(25,26,25,28,30)
racethn<-c('NEU','HA','NHW','NHB','HA')
gender<-c(1,2,2,2,1)
coef<- c(0.23,0.43,0.50,0.77)
pub_data<-data.frame(cbind(coef,gender,racethn,agemo))
ui <-shinyUI(fluidPage(
# Application title
titlePanel("User input + merge and calculate with r shiny"),
sidebarLayout(
sidebarPanel(
numericInput("id_visit_num", "id_visit_num:", 1,min = 1, max = 5000),
numericInput("agemo","age,mo", 24,min = 24, max = 240),
numericInput("nutrient","nutrient", 0,min = 0.5, max = 25),
numericInput("gender", "Gender:", 1,min = 1, max = 2),
radioButtons(inputId = "racethn","Race-Ethnicity",
label = "Race-Ethnicity",
choices = c("NEU","NHW","NHB","HA")),
actionButton("do", "Click Me")
# actionButton("calculate", "Generate Results")
),
# Show a Table
mainPanel(
basicPage(
DT::dataTableOutput("user_data"),
DT::dataTableOutput("FinalData"))
)
)
)
)
###data for all sessitions
server<-function(input, output) {
observeEvent(input$do, {
user_data <- data.frame(
"id_visit_num" =input$id_visit_num,
"nutrient" =input$nutrient,
"agemo" =input$agemo,
"gender" =input$gender,
"racethn" = input$racethn
)
#Struggling here. I want to merge input data frame with another df(static) and perform some calculation
FinalData =reactive(input$submit,{
ptdata = merge(pub_data,user_data,by = c("gender","agemo","racethn"), all = TRUE)
names(ptdata)<-tolower(names(ptdata))
ptdata<-ptdata%>% mutate(corrected_nutrient =(((nutrient/coef)
})
# print(input$id_visit_num)
# print(input$height)
# print(input$inflammation)
print(user_data)
# print(user_pubdata)
output$user_data = DT::renderDataTable({user_data})
output$user_pubdata = DT::renderDataTable({FinalData()})
})
}
shinyApp(ui, server)