Rshiny : Side Panel Creation on Action Button Click

I am creating an application where my first page contain two buttons and require different panels to open of click of each button. I have created one shiny script for buttons and two different shiny scripts for their actions. All the three are ready, but i am not getting how to integrate these. Please help as its important for my thesis work.

Code for Button Creation :
library("shinythemes")

ui <- fluidPage(theme = shinytheme("cosmo"),
h3(actionButton("runif", "For1"),align = "center"),
h3(actionButton("rnorm", "For2"),align = "center"),
hr(),
plotOutput("plot")
)

server <- function(input, output){
v <- reactiveValues(data = NULL)

observeEvent(input$runif, {
v$data <- runif(100)
})

observeEvent(input$rnorm, {
v$data <- rnorm(100)
})

output$plot <- renderPlot({
if (is.null(v$data)) return()
hist(v$data)
})
}

shinyApp(ui, server)

Code for Click Button DS1 :

Add a sidebar layout to the application

sidebarLayout(
# Add a sidebar panel around the text and inputs
sidebarPanel(

  fileInput("file1", "Choose a csv dataset file",multiple= FALSE, accept = c(
    "csv",
    "comma-separated-values",
    ".csv"
  ),width = 500,buttonLabel = "select one file",placeholder = "Add a file ")
),
# Add a main panel around the plot and table
mainPanel(
    tabsetPanel(
      tabPanel("Dataset Overview",tableOutput("contents")),
      tabPanel("Dataset Summary",tableOutput("datatypes")),
      tabPanel("Data Analysis",plotOutput("missingValueStats"))

)
)
)
)
)

Define the server logic

server <- function(input, output) {
output$contents <- renderTable({
req(input$file1)
df <- read.csv(input$file1$datapath)
})
output$datatypes <- renderTable({
df <- read.csv(input$file1$datapath, stringsAsFactors = FALSE)
dataTypes<- sapply(df,class)
ColumnNames<- colnames(df)
table(ColumnNames,dataTypes)
})
output$missingValueStats <- renderPlot({
df <- read.csv(input$file1$datapath, header = T, na.strings = c("","NA"))
na_count <-sapply(df, function(y) sum(length(which(is.na(y)))))
na_count <- data.frame(colnames(df),na_count)
par(las=2)
par(mar=c(5,8,4,2)) # increase y-axis margin.
barplot(na_count[ ,2], names.arg = na_count[ ,1], horiz = TRUE)

})
output$duplicateValueStats <- renderText({
  df <- read.csv(input$file1$datapath, header = T, na.strings = c("","NA"))
  DuplicateRows<- aggregate(list(numdup=rep(1,nrow(df))), df, length)
  aggregateSumOfDuplicates <- sum(DuplicateRows[DuplicateRows$numdup > 1,]$numdup, na.rm = TRUE)
  DuplicateRows <- transform(DuplicateRows, duplicityCheck = ifelse(DuplicateRows$numdup>1,"dup","nondup"))
  duplicateRowsActual <- nrow(DuplicateRows[DuplicateRows$duplicityCheck == "dup",])
  NetDuplicates <- aggregateSumOfDuplicates- DuplicateRowsActual
  TotalNumberOfRows<- nrow(df)
  percentageOfDup <- (NetDuplicates/TotalNumberOfRows)*100
  paste("The percentage of duplicates present within dataset: ",percentageOfDup,"%")
})

}

Run the application

shinyApp(ui = ui, server = server)

I am novice, may be i am not using correct functions or tags for my work.

I recommend you read and work through the following guide which will teach you the necessary principles and techniques that you are missing at this time.
https://shiny.rstudio.com/articles/dynamic-ui.html

1 Like

Thank You so muchh... :slight_smile:It worked for me.. was not finding any appropriate article for my work. With this i am able to create my dashboard.

One quick query please : - why the items are rendering as many times as i am clicking on an rendered item. How to restrict creation of one ui item only one time.. rest evrything is working fine..
Great Help!

Regards,
Garima.

Any leads here please.. as said its even written in the document that if we are adding more elements in insertui then it will keep on calling insertui and the items are getting created thus n number of times.
IS there can way of restricting the calling of insertui only once?

Regards,
Garima.

can you provide a small reprex illustrating your issue ?

Sure..

Code for UI
if (interactive()) {
ui <- fluidPage(
  h1("Data Cleaning Tool Advisor System", align = "center"),
  br(),
  br(),
  br(),
  h3(actionButton("dsP", "For DS P"),align = "center"),
  h3(actionButton("dsN", "For DS N"),align = "center")
)
server <- function(input, output, session) {
  observeEvent(input$dsP, {
    insertUI(
      selector = "#dsP",
      where = "beforeEnd",
      ui = fileInput("file1", "Choose a csv dataset file",multiple= FALSE, accept = c(
        "csv",
        "comma-separated-values",
        ".csv"
      ),width = 500,buttonLabel = "select one file",placeholder = "Add a file for analysis")
    )
    insertUI(
      selector = "#dsP",
      where = "afterEnd",
      ui = actionButton("Go","Go")
    )
  })
)

Ui code is running as much times as i am clicking on the rendered screen.

you haven't exactly provided a full description of desired behaviour, but this should get you closer.

library(shiny)
ui <- fluidPage(
  h1("Data Cleaning Tool Advisor System", align = "center"),
  br(),
  br(),
  br(),
  h3(actionButton("dsP", "For DS P"),align = "center"),
  uiOutput("dspFileInput"),
  h3(actionButton("dsN", "For DS N"),align = "center")
)
server <- function(input, output, session) {
 
  output$dspFileInput <- renderUI({
    req(input$dsP)
    tagList(fileInput("file1", "Choose a csv dataset file",multiple= FALSE, accept = c(
        "csv",
        "comma-separated-values",
        ".csv"
      ),width = 500,buttonLabel = "select one file",placeholder = "Add a file for analysis")
    , actionButton("Go","Go"))}    )
 }
 
shinyApp(ui, server)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.