Select multiple choices in selectInput() when selectize=F - r

How can I select multiple items in selectInput() when selectize=F?
library(shiny)
library(shinydashboard)
shinyApp(
ui = dashboardPage(
header = dashboardHeader(),
sidebar = dashboardSidebar(),
body = dashboardBody(
uiOutput("box1")
),
title = "DashboardPage"
),
server = function(input, output) {
output$box1<-renderUI({
box(
selectInput(inputId = "in", label = "Choose", choices = c('Short','A very short sentence.'),
selectize = F,multiple=T, size = 5, width = "150px")
)
})
}
)

What you have is allowing multiple selections.
You may see it more clearly if you add this (even if it's temporary)
Add verbatimTextOutput(outputId = "res") after the uiOutput("box1") (don't forget to add a comma) and add output$res <- renderPrint({input$`in`}) after output$box1 in server
library(shiny)
library(shinydashboard)
shinyApp(
ui = dashboardPage(
header = dashboardHeader(),
sidebar = dashboardSidebar(),
body = dashboardBody(
uiOutput("box1"), # comma added here
verbatimTextOutput(outputId = "res") # this is added
),
title = "DashboardPage"
),
server = function(input, output) {
output$box1 <- renderUI({
box(
selectInput(inputId = "in", label = "Choose", choices = c('Short','A very short sentence.'),
selectize = F,multiple=T, size = 5, width = "150px")
)# ends the box
}) # ends output$box1
output$res <- renderPrint({input$`in`}) # this is added here - since 'in' is a keyword I would suggest a different id...
} # ends server call
) # ends shinyApp

Related

Adjust the height of bsCollapsePanel in shiny app

I'm using the shinyBS package in a shiny app and I'm struggle with adjusting the height of the panel ! I have tried to put the height=100% in bsCollapse and bsCollapsePanel but it did not work !
library(shiny)
library(shinythemes)
library(shinyjs)
library(collapsibleTree)
library(shinyBS)
library(shinycssloaders)
ui <- fluidPage(
theme = shinytheme("flatly"),
useShinyjs(),
navbarPage(HTML("MyApp"),
tabPanel("Home",
bsCollapse(id = "output_data_all", multiple = TRUE,open = "Data",
bsCollapsePanel("Data", style = "info",
selectInput(inputId = 'Ind1',
label = 'Select Container',
choices = c(),
selected = NULL),
br(),br(),
withSpinner(collapsibleTreeOutput("t1"),type=5),
br(),br()
))
)
)
)
Server <- function(input, output,session){
output$t1 <- renderCollapsibleTree({
})
}
You can enclose the contents of the panel in a div with style = "height: 70vh" (adjust 70 as you want - this means 70% of the viewport height):
ui <- fluidPage(
theme = shinytheme("flatly"),
useShinyjs(),
navbarPage(
HTML("MyApp"),
tabPanel("Home",
bsCollapse(id = "output_data_all", multiple = TRUE,open = "Data",
bsCollapsePanel("Data", style = "info",
div(
style = "height: 70vh",
selectInput(inputId = 'Ind1',
label = 'Select Container',
choices = c(),
selected = NULL),
br(),br(),
withSpinner(collapsibleTreeOutput("t1"),type=5),
br(),br()
)
)
)
)
)
)

Set the maximum number of choices made by pickerInput()

Im trying to limit the max number of choices made by pickerInput() to two in shiny app but I cannot make it work.
library(shiny)
library(shinydashboard)
library(plotly)
library(shinyWidgets)
header <- dashboardHeader()
sidebar <- dashboardSidebar(
fluidRow(column(12,
pickerInput(
inputId = "iss",
label = "Issue",
choices = colnames(mtcars),
multiple = T,
options = list("max-options-group" = 2)
)
))
)
body <- dashboardBody(fluidPage(
)
)
ui <- dashboardPage(title = 'Search', header, sidebar, body)
server <- function(input, output, session) {
}
shinyApp(ui = ui, server = server)
The problem is that you are using "max-options-group" but you are not using any groups in your choices. You must use "max-options" = 2 in the options argument of pickerInput().
For completeness, this is the modified version of your code. We cannot pick more than 2 options with it:
library(shiny)
library(shinydashboard)
library(plotly)
library(shinyWidgets)
header <- dashboardHeader()
sidebar <- dashboardSidebar(
fluidRow(column(12,
pickerInput(
inputId = "iss",
label = "Issue",
choices = colnames(mtcars),
multiple = T,
options = list("max-options" = 2)
)
))
)
body <- dashboardBody(fluidPage(
)
)
ui <- dashboardPage(title = 'Search', header, sidebar, body)
server <- function(input, output, session) {
}
shinyApp(ui = ui, server = server)
Try this
columns <- as.list(names(mtcars))
type <- as.list(1:ncol(mtcars))
header <- dashboardHeader()
sidebar <- dashboardSidebar(
fluidRow(column(12,
pickerInput(
inputId = "iss",
label = "Issue",
choices = list(Columns = columns,
Type = type),
selected = list(columns[[1]],type[[1]]),
multiple = T,
inline=TRUE,
options = list("max-options-group" = 1, `style` = "btn-info")
)
))
)
body <- dashboardBody(fluidPage())
ui <- dashboardPage(title = 'Search', header, sidebar, body)
server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)

R shiny: Align inputs/ outputs horizontally within columns

I would like to align inputs/ outputs horizontally within columns. I can do this by splitting IDs, but I would prefer a way that dynamically splits the single input/ output ID into spaced columns.
Here is my code:
library(shinythemes)
library(shiny)
rm(list = ls())
ui <- navbarPage('Example',id = "inTabset",
tabPanel(title = "Example_1", value = "Example_1",
fluidPage(
tags$b( h4("Example_1", align = "left")),
theme = shinytheme("paper"),
fluidRow(
column(6,checkboxGroupInput("checkGroup", label ="",
choices = c(1,2,3,4,5,6,7,8),
selected = c(1,4,7)) )
),
br()
),
hr(),
verbatimTextOutput("example1")
),
tabPanel(title = "Example_2", value = "Example_2",
fluidPage(
tags$b( h4("Example_2", align = "left")),
br(),
fluidRow(
column(4, uiOutput("VarsInput")),
fluidRow(verbatimTextOutput("dataInfo")),
br(),
hr())
)
))
server <- function(input, output, session) {
output$example1 = renderPrint(input$checkGroup)
### output$example2 = ????
### i.e what data (a,b,c,d,e or f) has been chosen from the selectInput below?
K <- reactive({
length(input$checkGroup)
})
output$VarsInput <- renderUI({
NoV = K()
C = sapply(1:(ceiling(NoV)), function(i){paste0(input$checkGroup[i])})
output = tagList()
for(i in seq_along(1:ceiling(NoV))){
output[[i]] = tagList()
output[[i]][[1]] = selectInput(C[i], C[i], c("",c("a","b","c","d","e","f")))
}
output
})
}
shinyApp(ui, server)
As an example, the first input looks like this:
single column
I could, of course, split this into 2 (with the following code) to look like the following picture. However, this entails creating a separate ID, which I want to avoid.
2 columns
library(shinythemes)
library(shiny)
rm(list = ls())
ui <- navbarPage('Example',id = "inTabset",
tabPanel(title = "Example_1", value = "Example_1",
fluidPage(
tags$b( h4("Example_1", align = "left")),
theme = shinytheme("paper"),
fluidRow(
column(6,checkboxGroupInput("checkGroup1", label ="",
choices = c(1,2,3,4),
selected = c(1,4)) ),
column(6,checkboxGroupInput("checkGroup2", label ="",
choices = c(5,6,7,8),
selected = c(7)) )
),
br()
),
hr(),
verbatimTextOutput("example1")
))
server <- function(input, output, session) {
output$example1 = renderPrint(input$checkGroup1)
output$example2 = renderPrint(input$checkGroup2)
}
shinyApp(ui, server)
Many thanks.
Using inline=TRUE with width="100px" will control how wide you want to display. Then use
tags$head(
tags$style(
".checkbox-inline{margin-left:10px;"
)
)
to indent the first row. Try this
library(shinythemes)
library(shiny)
rm(list = ls())
ui <- navbarPage('Example',id = "inTabset",
tabPanel(title = "Example_1", value = "Example_1",
fluidPage(
tags$b( h4("Example_1", align = "left")),
theme = shinytheme("paper"),
fluidRow(
column(6,checkboxGroupInput("checkGroup", label ="", width="100px", inline=TRUE,
choices = c(1,2,3,4,5,6,7,8),
selected = c(1,4,7)) )
),
br()
),
hr(),
verbatimTextOutput("example1")
),
tags$head(
tags$style(
".checkbox-inline{margin-left:10px;"
)
),
tabPanel(title = "Example_2", value = "Example_2",
fluidPage(
tags$b( h4("Example_2", align = "left")),
br(),
fluidRow(
column(4, uiOutput("VarsInput")),
fluidRow(verbatimTextOutput("dataInfo")),
br(),
hr())
)
))
server <- function(input, output, session) {
output$example1 = renderPrint(input$checkGroup)
### output$example2 = ????
### i.e what data (a,b,c,d,e or f) has been chosen from the selectInput below?
K <- reactive({
length(input$checkGroup)
})
output$VarsInput <- renderUI({
NoV = K()
C = sapply(1:(ceiling(NoV)), function(i){paste0(input$checkGroup[i])})
output = tagList()
for(i in seq_along(1:ceiling(NoV))){
output[[i]] = tagList()
output[[i]][[1]] = selectInput(C[i], C[i], c("",c("a","b","c","d","e","f")))
}
output
})
}
shinyApp(ui, server)

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)

Shiny dashboard and DT table not showing

I have a dashboard where I would like to show a table, but I cant figure out why my table is not showing. If I replace the table for example with some text, h2(....) it does show. I would like to click on "Species" and have the table show on the right when clicking it.
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(DT)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(sidebarMenu(
menuItem(
"Species",
tabName = "Species",
icon = NULL,
switchInput(
inputId = "long1",
onLabel = "Go",
offLabel = "NoGo",
value = T
),
actionButton("Gobtn", "Get data"),
menuItem("Specs", tabName = "Specs", icon = NULL)
)
)),
dashboardBody(tabItems(
tabItem(tabName = "Species",
DT::renderDataTable("Table1")),
tabItem(tabName = "Specs",
h2("Hi"))
))
)
server.r
server <- shinyServer(function(input, output, session) {
output$Table1 <- DT::renderDataTable({
iris
})
})
shinyApp(ui, server)
Few things to get your code up and running here. Couple have been noted by other contributors.
We need to use DT::dataTableOutput("Table1") on the UI side as renderDataTable will not work here, that is the server side function.
The other would be that using the switchInput within the menuItem may confused the app, as these are not standard parameters to pass into the function. From what I can see from your code, which is a common challenge, is that you want to be able to show this switchInput only when the 'Species' tab is selected. We can account for this using conditionalPanel. To do this, we can set id = "tabs" within the sidebarMenu and then reference this sidebarMenu within the conditionalPanel:
conditionalPanel(
condition = "input.tabs== 'Species' ",
switchInput(
inputId = "long1",
onLabel = "Go",
offLabel = "NoGo",
value = T
)
)
To finish, I have altered the layouts of the ui.R and server.R, as the shinyApp function was not needed for the app to work with the server and ui files. This is how I lay out my dashboards. It may show you a few other possible ways you can use the app structure within Shiny, but equally you could just align the changes to the basic layout.
ui.R
header <- dashboardHeader(title = "Basic dashboard")
sidebar <- dashboardSidebar(
sidebarMenu(id = "tabs",
menuItem(
"Species",
tabName = "Species",
icon = NULL),
conditionalPanel(
condition = "input.tabs== 'Species' ",
switchInput(
inputId = "long1",
onLabel = "Go",
offLabel = "NoGo",
value = T
)
),
actionButton("Gobtn", "Get data"),
menuItem("Specs", tabName = "Spec", icon = NULL)
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "Species",
DT::dataTableOutput("Table1")),
tabItem(tabName = "Spec",
h2("Hi"))
)
)
dashboardPage(skin = "blue", header = header, sidebar = sidebar, body = body)
server.R
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(DT)
shinyServer(function(input, output, session){
output$Table1 <- DT::renderDataTable({
datatable(iris)
})
})
You need to change/add some part of the dashboardBody, see Using shiny modules and shinydashboard: shiny.tag error
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(DT)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(sidebarMenu(
menuItem(
"Species",
tabName = "Species",
icon = NULL,
switchInput(
inputId = "long1",
onLabel = "Go",
offLabel = "NoGo",
value = T
),
actionButton("Gobtn", "Get data")
)
)),
dashboardBody(tags$div(
tabName = "Species",
fluidRow(box(DT::dataTableOutput("Table1"))), class = "tab-content"
))
)
server.r
server <- shinyServer(function(input, output, session) {
output$Table1 <- DT::renderDataTable({
iris
})
})
shinyApp(ui, server)

Resources