I am building a Shiny app that would take in numeric data (in a "handsontable" format) and make a boxplot out of the entered data. I would like to have an option to change column names (as "textInput") of the resulting "handsontable" and pass those names to the plot's "x" axis labels. While the code below works fine for changing names in the table I could not find a solution to pass those updated column names ("colHeaders") to the plot. I would very much appreciate a piece of advice. Many thanks! edit: the app also calculates column means and saves PNG but that is unlikely to be related to my problem
library(shiny)
library(rhandsontable)
library(dplyr)
#UI
ui <- shinyUI(fluidPage(
titlePanel("Boxplot builder"),
textOutput('result'),
sidebarLayout(
sidebarPanel(
wellPanel(
plotOutput("plot")),
wellPanel(
h3("Build BoxPlot"),
actionButton("build", "Build")),
wellPanel(
h3("Export Plot"),
downloadButton('ExportPlot', 'Export as png')),
),
mainPanel(textInput("colnames", "Enter Column Names (separated by comma)",
value="", placeholder = "e.g. Var1, Var2, etc"),
h5(tags$b("Enter data")),
rHandsontableOutput('table'))
)))
rowNames <- c(sprintf("[%d]",seq(1:20)), "Means")
defaultDF <- data.frame(
row.names = rowNames,
A = rep(NA_integer_, nrow=21),
B = rep(NA_integer_, 21),
C = rep(NA_integer_, 21),
stringsAsFactors = FALSE)
#Server
server <- function(input, output, session)
({
values <- reactiveValues(data = defaultDF)
observe({
req(input$table)
DF <- hot_to_r(input$table)
DF[setdiff(rowNames, "Means"),]
DF["Means",] <- colMeans(DF[setdiff(rowNames, "Means"),], na.rm = TRUE)
values$data <- DF
})
output$table <- renderRHandsontable({
req(values$data)
rhandsontable(values$data, rowHeaderWidth = 100,
colHeaders=str_trim(unlist(strsplit(input$colnames,","))),) %>%
hot_row(nrow(values$data), readOnly = TRUE)
})
observeEvent(input$build, {
output$plot <- renderPlot({
boxplot(values$data)
})
})
#Export PNG
output$ExportPlot = downloadHandler(
filename = 'BoxPlot.png',
content = function(file) {
png(file)
boxplot(values$data)
dev.off()
})
})
shinyApp(ui = ui, server = server)