I would like for the app user to select something from the drop-down list then enter a numeric value for it which would translate into a dataframe. If the same item is selected upon clicking the button n times, it would replace the original value, otherwise, it should cbind the new values into the dataframe. I don't know why there is a strange header on my dataframe as well. Please advise, thanks.
ui <- fluidPage(
selectInput("solvent", "Choose a solvent:",
list(`Solvent` = c("ETOH", "SALINE", "DW5",'CREMOPHOR','WATER','DMSO','2% KLUCEL (pH=4 /w HCl)')
)),
numericInput('vol','Vol solvent (ml)',0),
actionButton('add','Add'),
textOutput("result"),
tableOutput('table')
)
server = function(input, output) {
rv <- reactiveValues()
observeEvent(input$add,{rv$data<-data.frame(c(input$solvent,input$vol)); setNames(rv$data,input$solvent);
if (as.numeric(input$add)>1 & colnames(rv$data) == input$solvent)
{rv$data[1,input$solvent] = input$vol}
else {cbind(rv$data,data.frame(input$solvent,input$vol))}
})
output$table<-renderTable({ rv$data})
output$result <- renderText({
paste("You chose", input$solvent)
})
}
shinyApp(ui, server)
Filling an empty dataframe and overwriting the values requires unfortunately a bit more than a two-liner. I suggest the following:
rv <- reactiveValues()
observeEvent(input$add, {
if(is.null(rv$data)){
rv$data <- data.frame(input$vol)
names(rv$data) <- input$solvent
}
rv$data[input$solvent] <- input$vol
})
The full version you can find below:
ui <- fluidPage(
selectInput("solvent", "Choose a solvent:",
list(`Solvent` = c("ETOH", "SALINE", "DW5",'CREMOPHOR','WATER','DMSO','2% KLUCEL (pH=4 /w HCl)')
)),
numericInput('vol','Vol solvent (ml)',0),
actionButton('add','Add'),
textOutput("result"),
tableOutput('table')
)
server = function(input, output) {
rv <- reactiveValues()
observeEvent(input$add, {
if(is.null(rv$data)){
rv$data <- data.frame(input$vol)
names(rv$data) <- input$solvent
}
rv$data[input$solvent] <- input$vol
})
output$table<-renderTable({ rv$data})
output$result <- renderText({
paste("You chose", input$solvent)
})
}
shinyApp(ui, server)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.