Hi together,
I am trying to generate a user interface that allows the user to select first a plot type (e.g. XY, hist...) and then get a plot type specific set of input fields to further customise the plot (e.g. select the column for the histogram...).
I first generate the plot type specific settings with uiOutput and then populate the input elements based on the dataset (in the example iris). This works for the first time a plot type is selected or the app is started, but once the uiOutput is called again the input elements are no longer updated.
Below is my example code:
library(ggplot2)
library(stringr)
dataSel <- c("Full Data")
plotTypes <- c("XY", "Hist", "Box", "Matrix")
AxisScale <- c("lin", "log")
ui <- fluidPage(
titlePanel("Data Viewer"),
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(h3("Input Files"),
"Selected Plot Type:",
textOutput("control"),
width = 2
), # end sidebar
# Main panel for displaying outputs ----
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Plots",
fluidPage(
fluidRow(
column(
5,
hr(style = "border-top: 0px solid #000000;"),
wellPanel(
tags$head(tags$style(HTML(".selectize-input {height: 11px; font-size: 10px}"))),
style = "width: 760px",
fluidRow(h4("Plot 1")),
fluidRow(
column(3, selectInput("DataSource.plot1", h6("Data Source:"), choices = dataSel, selected = NULL)),
column(2, selectInput("PlotType.plot1", h6("Plot Type:"), choices = plotTypes, selected = NULL)),
column(2, sliderInput("Limit.No.Points.plot1", h6("Max No Points"), min=1,max=100,value=100)),
column(2, selectInput("Filter1.plot1", h6("Filter 1"), choices = dataSel, selected= NULL)),
column(3, sliderInput("Filter1.range.plot1", h6("Range"), min=1, max=100, value=c(0,100)))),
uiOutput("settings.plot1"),
plotOutput("plot1", click = "plot1.click", dblclick = dblclickOpts(id = "ToOpenImage", delay = 300))
))))),
tabPanel("Tables", tableOutput("table"))
)
)
)
)
################################### SERVER
server <- function(input, output, session) {
# check the plot type
output$plottype.plot1 <-reactive({input$PlotType.plot1})
# check the input columns
output$control <- renderText({
input$PlotType.plot1
})
output$settings.plot1 <- renderUI({
if(str_detect(input$PlotType.plot1, "XY"))
{
fluidRow(
column(2, selectInput("Xaxis.plot1", h6 ("X axis:"), "", selected = NULL)),
column(2, sliderInput("Xaxis.Range.plot1", h6 ("X axis range:"), min=1, max=100, value=c(0,100))),
column(1, selectInput("Xaxis.Scale.plot1", h6("Scale"), choices = AxisScale, selected = NULL)),
column(2, selectInput("Yaxis.plot1", h6 ("Y axise:"), "", selected = NULL)),
column(2, sliderInput("Yaxis.Range.plot1", h6 ("Y axis range:"), min=1, max=100, value=c(0,100))),
column(1, selectInput("Yaxis.Scale.plot1", h6("Scale"), choices = AxisScale, selected = NULL)),
column(2, selectInput("Color.plot1", h6 ("Color:"), "", selected = NULL))
)}
else if(str_detect(input$PlotType.plot1, "Hist"))
{
fluidRow(
column(2, selectInput("Xaxis.plot1", h6 ("X axis:"), "", selected = NULL)),
column(3, sliderInput("Xaxis.Range.plot1", h6 ("X axis range:"), min=1, max=100, value=c(0,100))),
column(2, selectInput("Yaxis.Scale.plot1", h6("Y scale"), choices = AxisScale, selected = NULL)),
column(2, selectInput("Group.plot1", h6 ("Group by:"), "", selected = NULL)),
column(3, sliderInput("No.bin.plot1", h6 ("No bins:"), min=1, max=100, value=10))
)}
else if(str_detect(input$PlotType.plot1, "Box"))
{
fluidRow(
column(2, selectInput("Yaxis.plot1", h6 ("Values:"), choices ="", selected = NULL)),
column(3, sliderInput("Yaxis.Range.plot1", h6 ("Y axis range:"), min=1, max=100, value=c(0,100))),
column(2, selectInput("Yaxis.Scale.plot1", h6("Y scale"), choices = "", selected = NULL)),
column(2, selectInput("Box.Category.plot1", h6 ("Category:"), choices ="", selected = NULL))
)}
else if(str_detect(input$PlotType.plot1, "Matrix"))
{
fluidRow(
column(2, selectInput("FacetX.plot1", h6 ("Facet X:"), "", selected = NULL)),
column(2, selectInput("FacetY.plot1", h6 ("Facet Y:"), "", selected = NULL)),
column(2, selectInput("MatrixType.plot1", h6 ("Type:"), "", selected = NULL)),
column(2, selectInput("MatrixValue1.plot1", h6 ("Value 1:"), "", selected = NULL)),
column(2, selectInput("MatrixValue1.plot2", h6 ("Value 2:"), "", selected = NULL))
)}
})
data <- reactive({iris})
updateSelectInput(session, inputId = "Xaxis.plot1", choices = names(iris), selected = names(iris)[1])
output$table <- renderTable({iris})
output$control <- renderText({names(iris)})
reactive.plot1 <- reactive({gplot <- ggplot(data())})
output$plot1 <- renderPlot({reactive.plot1()})
}
shinyApp(ui, server)
Thanks for the help.