Shiny: create a selectInput after choosing a value of another selectInput - r

I have a shiny application where I want to build a conditional query system on a data frame.
First, I want to have a selectInput showing all available tables. After the user has chosen a table, I want another box to appear where he can select the column name he wants to filter for. This is what I have until now:
ui.r:
library(shiny)
library(shinydashboard)
source("global_variables.r")
ui=dashboardPage(
dashboardHeader(title="Test"),
dashboardSidebar(
sidebarMenu(
menuItem("Conditionals",tabName = "conditionals")
)
),
dashboardBody(
tabItems(
tabItem(tabName="conditionals",
fluidRow(
box(
title = "Conditionals",
width = 4,
selectInput("Con_tableName",choices=c("NONE",tableNames),label = "Table Name"),
tags$div(id = 'placeholder')
)
)
)
)
)
)
server.r:
library(shiny)
source("global_variables.r", local = FALSE)
Table1=data.frame();
shinyServer(
function(input, output,session) {
observe({
reactive(
if(input$Con_tableName!="NONE"){
insertUI( selector="#placeholder",
ui={
selectInput("Con_colName",choices=c("NONE",colnames(dynGet(input$Con_tableName))),label = "Column Name")
}
)
}
)
})
}
)
global_variables.r:
tableNames=c("Table1","Table2","Table3")
The problem is, that if I choose a value in the selectInput, observe doesnt get fired.
EDIT:
According to BigDataScientists comment,changed insertUI to renderUI. Updated files:
ui.r:
library(shiny)
library(shinydashboard)
source("global_variables.r")
ui=dashboardPage(
dashboardHeader(title="Test"),
dashboardSidebar(
sidebarMenu(
menuItem("Conditionals",tabName = "conditionals")
)
),
dashboardBody(
tabItems(
tabItem(tabName="conditionals",
fluidRow(
box(
title = "Conditionals",
width = 4,
selectInput("Con_tableName",choices=c("NONE",tableNames),label = "Table Name"),
uiOutput("conditionalUI")
)
)
)
)
)
)
server.r:
library(shiny)
source("global_variables.r", local = FALSE)
Table1=data.frame();
shinyServer(
function(input, output,session) {
observeEvent(input$Con_tableName,{
reactive(
if(input$Con_tableName!="NONE"){
output$conditionalUI=renderUI({
selectInput("Con_colName",choices=c("NONE",colnames(input$Con_tableName)),label = "Column Name")
})
}
)
})
}
)

You can use conditionalPanel(). Below there is a small example which might work in your case.
library(shiny)
shinyApp(
ui <- fluidPage(
mainPanel(
selectInput("input1", "Select something", choices = c('','1','2','3')),
conditionalPanel("input.input1!=''",
selectInput('input2', "Select something else", choices = c('4','5')))
)
),
server <- function(input, output){}
)

Related

Dynamic Tab creation with content

I am trying to build a shiny app where the user can decide how many tabs he wants to be shown. Here's what I have so far:
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(glue)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sliderInput(inputId = "slider", label = NULL, min = 1, max = 5, value = 3, step = 1)
),
dashboardBody(
fluidRow(
box(width = 12,
p(
mainPanel(width = 12,
column(6,
uiOutput("reference")
),
column(6,
uiOutput("comparison")
)
)
)
)
)
)
)
server <- function(input, output) {
output$reference <- renderUI({
tabsetPanel(
tabPanel(
"Reference",
h3("Reference Content"))
)
})
output$comparison <- renderUI({
req(input$slider)
tabsetPanel(
lapply(1:input$slider, function(i) {
tabPanel(title = glue("Tab {i}"),
value = h3(glue("Content {i}"))
)
})
)
})
}
shinyApp(ui = ui, server = server)
This does not produce the desired results, as the comparison tabs are not shown properly.
I have already checked out these 2 threads:
R Shiny - add tabPanel to tabsetPanel dynamically (with the use of renderUI)
R Shiny dynamic tab number and input generation
but they don't seem to solve my problem. Yes, they create tabs dynamically with a slider, but they don't allow to fill these with content as far as I can tell.
What works for me is a combination for lapply and do.call
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(glue)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sliderInput(inputId = "slider", label = NULL, min = 1, max = 5, value = 3, step = 1)
),
dashboardBody(
fluidRow(
box(width = 12,
p(
mainPanel(width = 12,
column(6,
uiOutput("reference")
),
column(6,
uiOutput("comparison")
)
)
)
)
)
)
)
server <- function(input, output) {
output$reference <- renderUI({
tabsetPanel(
tabPanel(
"Reference",
h3("Reference Content"))
)
})
output$comparison <- renderUI({
req(input$slider)
myTabs = lapply(1:input$slider, function(i) {
tabPanel(title = glue("Tab {i}"),
h3(glue("Content {i}"))
)
})
do.call(tabsetPanel, myTabs)
})
}
shinyApp(ui = ui, server = server)

How to merge the Column names with the FluidRow in RShiny

I am currently developing a Shiny App. In that I am not getting the expected output. The expected output is
But the output I get is
This is the code used
ui.R
shinyUI(
dashboardPage(
dashboardSidebar(
sidebarMenu(
id = 'MENU', badgeColor = "aqua",
menuItem('VIEW', tabName = 'view'),
menuItem('EDIT',tabName = 'edit')
)
),
dashboardBody(
tabItems(tabItem(tabName = "edit",
uiOutput("moreControls"))))
server.R
shinyServer(function(input, output, session) {
output$moreControls <- renderUI({
wellPanel(
fluidRow(column(4,wellPanel(
wellPanel("PEOPLE", style = "background-color:#0ec3c6;border-color:#0ec3c6;text-align:center;color: white;font-size: 24px;font-style: bold ;padding: 12px;"),
style ="background-color:RGB(255,255,255); border-color:RGB(255,255,255);align:right;",
textInput('email', 'Enter Email_Id'),
textInput('fn', ' Enter First Name')))))})
})
Can anyone help me with this issue? Thanks in advance..
Below code will give you the required wellPanel layout.
Note: I didn't use your full code, just tried to achieve specified layout. So replace the code block if it solves your problem.
library(shiny)
ui <- fluidPage(
wellPanel(
fluidRow(column(4,
fluidRow(wellPanel("PEOPLE", style = "background-color:#0ec3c6;border-color:#0ec3c6;text-align:center;color: white;font-size: 24px;font-style: bold ;padding: 12px;")),
style = "background-color:RGB(255,255,255); border-color:RGB(255,255,255);align:right;",
fluidRow(column(4, "Enter Email-ID"), column(8, textInput(label = NULL, inputId = 'EmailID' ))),
fluidRow(column(4, "Enter First Name"), column(8, textInput(label = NULL, inputId = 'FirstName')))))))
server <- function(input, output, session) {
onSessionEnded(stopApp)
}
shinyApp(ui, server)

How to call choice name instead of value in Shiny?

I'm working on a dashboard where sometimes I need to call the input's choice name and other times it's value, but I only know how to get the latter. Is there a way to call the first one?
Here is a minimum reproducible example:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
header = dashboardHeader(),
sidebar = dashboardSidebar(),
body = dashboardBody(
selectInput(
"input",
h5("The output should give the choice name instead of it's value"),
choices=c(
"Name 1" = 1,
"Name 2" = 2,
"Name 3" = 3
)
),
textOutput("output")
)
)
server <- function(input, output, session) {
output$output <- renderPrint({paste(input$input)})
}
shinyApp(ui = ui, server = server)
I think it is easiest to create a data.frame with the choices and the corresponding names in advance, and use that to look up the name of the selected input. A working example is given below, hope this helps!
library(shiny)
library(shinydashboard)
choices_df = data.frame(
names = c('Name 1', 'Name 2', 'Name 3'),
id = seq(3)
)
ui <- dashboardPage(
header = dashboardHeader(),
sidebar = dashboardSidebar(),
body = dashboardBody(
selectInput(
"input",
h5("The output should give the choice name instead of it's value"),
choices= setNames(choices_df$id,choices_df$names)
),
textOutput("output")
)
)
server <- function(input, output, session) {
output$output <- renderPrint({paste(choices_df$names[choices_df$id==input$input])})
}
shinyApp(ui = ui, server = server)

ValueBox in R - Only number is shown

I am building a shiny dashboard and I want to implement a valueBox within the Dashboard.
body <- dashboardBody(
fluidRow(
valueBox(totalSales,"Total Sales",color="blue")
),
fluidRow(
DT::dataTableOutput("salesTable")
),
fluidRow(
DT::dataTableOutput("top10Sales")
)
)
And this is the result:
The number on the upper left is the variable totalSales but it isn't formatted in a valueBox.
Does anyone know what the problem is?
I appreciate your answers!!
My try with valueBoxOutput, but with the same result:
ui.R
body <- dashboardBody(
fluidRow(
valueBoxOutput("totalSales")
),
fluidRow(
DT::dataTableOutput("salesTable")
),
fluidRow(
DT::dataTableOutput("top10Sales")
)
)
server.R
function(input, output, session) {
output$salesTable = DT::renderDataTable(top10Sales)
output$top10Sales = DT::renderDataTable(top10Sales)
#output$totalSales = DT::renderDataTable(totalSales)
output$totalSales <- renderValueBox({
valueBox(totalSales, "Approval",color = "yellow")
})
}
And still the same result:
By the way: Infobox is working:
infoBox("test", value=1, width=3)
valueBox has to be used on the server side. To display a shiny dynamic UI element, there's generally a function (in this case valueBoxOutput) available to display it:
library(shinydashboard)
library(dplyr)
library(DT)
body <- dashboardBody(
fluidRow(
valueBoxOutput("totalCars")
),
fluidRow(
DT::dataTableOutput("table")
)
)
ui <- dashboardPage(header = dashboardHeader(),
sidebar = dashboardSidebar(),
body = body
)
server <- function(input, output) {
output$table = DT::renderDataTable(mtcars)
output$totalCars <- renderValueBox({
valueBox("Total", nrow(mtcars), color = "blue")
})
}
shinyApp(ui, server)

Shiny Dashboard - display a dedicated "loading.." page until initial loading of the data is done

I have initial loading of data from the DB in the server.R which takes a few seconds. Until this is done, the page displayed is distorted (wrong data in selection box, and weird placing of the boxes, see below).
I want to display a different page (or at least different content in my first-displayed tab) until the data is completely loaded.
I thought about doing some kind of conditionalPanel using a condition based on a dedicated global variable (initial_loading_done), but wherever I tried placing the conditionalPanel it didn't work.
This is the structure of my UI.R:
shinyUI(
dashboardPage(
dashboardHeader(title = "Title"),
dashboardSidebar(
sidebarMenu(
menuItem("Tab1", tabName = "Tab1",icon = icon("dashboard")),
menuItem("Tab2", tabName = "Tab2", icon = icon("bar-chart-o"))
)
),
dashboardBody(
includeCSS("custom_css.css"),
tabItems(
tabItem(tabName = "Tab1",
fluidRow(<content>),
mainPanel(
fluidRow(<content>)
)
),
tabItem(tabName = "Tab2",
fluidRow(<content>),
mainPanel(
dataTableOutput('my_data_table')
)
)
)
)
)
)
Here's a very simple example using shinyjs package
The idea is to create the loading "page" and the content "page" under different IDs, have the content page initially hidden, and use show() and hide() after the app is ready
library(shiny)
library(shinyjs)
load_data <- function() {
Sys.sleep(2)
hide("loading_page")
show("main_content")
}
ui <- fluidPage(
useShinyjs(),
div(
id = "loading_page",
h1("Loading...")
),
hidden(
div(
id = "main_content",
"Data loaded, content goes here"
)
)
)
server <- function(input, output, session) {
load_data()
}
shinyApp(ui = ui, server = server)
In server I like to use reactiveValues() to store a setupComplete condition. Then, when the data is loaded my setupComplete is set to TRUE.
In the ui we can then assess this setupComplete condition in a conditionalPanel, and only display the content (in my example the three box() widgets).
Here's a working example
## app.R ##
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
actionButton(inputId = "btn_data", label = "Download"),
conditionalPanel(condition = "output.setupComplete",
box( title = "box1" ),
box( title = "box2" ),
box( title = "boc3" )
),
conditionalPanel(condition = "!output.setupComplete",
box( title = "loading"))
)
)
server <- function(input, output) {
rv <- reactiveValues()
rv$setupComplete <- FALSE
## simulate data load
observe({
if(input$btn_data){
df <- data.frame(id = seq(1,200),
val = rnorm(200, 0, 1))
## Simulate the data load
Sys.sleep(5)
## set my condition to TRUE
rv$setupComplete <- TRUE
}
## the conditional panel reads this output
output$setupComplete <- reactive({
return(rv$setupComplete)
})
outputOptions(output, 'setupComplete', suspendWhenHidden=FALSE)
})
}
shinyApp(ui, server)
The code
hidden(
div(
id = "main_content",
"Data loaded, content goes here"
)
doesn't work with tabsetPanel. But if you move the id to the div level it works beautifully. Thanks to shinyjs author Dean Attali for this tip. https://stackoverflow.com/users/4432127/keshete
hidden(
div(id = "mainTabsetPanel",
tabsetPanel(
....

Resources