Call an input object from selectInput in UI - r

I'm using shinydashboard package to create a shiny app.
In UI function I have a selectInput, which I would like to use those input later on in the box title but I don't know how could I access them I have tried input$xx, input.xx and 'input.xx' but it does not work :
dashboardSidebar(
selectInput("wind","Select Wind speed",choices = c(6,8,10,12),selected = 10),
selectInput("time","Select Time",choices = c(2,3,4),selected = 3),
downloadButton('report')
),
dashboardBody(
fluidRow(
box(width = 12,title = paste("time :", "'input$time'" ,"and wind speed :", "'input$wind'" ,"m/s are recorded."),
column(12,withSpinner(tableOutput("tab6"),type=5))
)
)
)

I have found the sloution :
Using RenderUI function :
in UI :
dashboardBody(
uiOutput("txt")
)
And in server :
output$txt <- renderUI({
fluidRow(
box(width = 12,title = paste("time :", input$time ,"and wind speed :", input$wind ,"m/s are recorded."),
column(12,withSpinner(tableOutput("tab6"),type=5))
),
box(width = 12,
column(12,withSpinner(tableOutput("tab3"),type=5))
)
)
})

This is how I would approach your issue.
Firstly you need to use the "updateTextInput" function of shiny. More details here:
https://shiny.rstudio.com/reference/shiny/1.0.2/updateTextInput.html
Here is how your code should look like:
ui <- dashboardPage(
dashboardHeader(title = "Control Panel"),
dashboardSidebar(
selectInput("wind","Select Wind speed",choices = c(6,8,10,12),selected = 10),
selectInput("time","Select Time",choices = c(2,3,4),selected = 3),
downloadButton('report')
),
dashboardBody(
fluidRow(
column(12,textInput("inText", "Text1"))
)
)
)
)
# 2. Server ---------------------------------------------------------------
server <- function(input, output, session){
observe({
x <- input$time
y <- input$wind
updateTextInput(session, "inText", value = paste("time :", x ,"and wind speed :", y ,"m/s are recorded."))
})
}
# 3. App ------------------------------------------------------------------
shinyApp(ui, server)

Related

Multiple fileInput in one line in shiny app

In my App I would like to have 3 fileInput object per row. How should I modify the code ? currently even if I define the column width It just put one fileInput in each row :
library(shiny)
library(shinythemes)
library(shinydashboard)
library(shinyWidgets)
ui <- fluidPage(
theme = shinytheme("lumen"),
shinyWidgets::useShinydashboard(),
navbarPage("test theme",
tabPanel("tab1",
mainPanel(width = 12,
fluidRow(
box(width = 12,
title = "title", status = "primary", solidHeader = TRUE,
numericInput("num","Number of file input",value = 2),
column(width = 3,
uiOutput("fIn"))
)
)
)
)
)
)
server <- function(input, output, session) {
output$fIn <- renderUI({
ind = as.numeric(input$num)
lapply(1:ind, function(k) {
fileInput(paste0("fIn", k), paste('File:', k), accept=c("xlsx","text"))
})
})
}
shinyApp(ui, server)
Instead of wrapping the uiOutput in column wrap each single fileInput in a column:
library(shinyWidgets)
library(shiny)
library(shinydashboard)
ui <- fluidPage(
shinyWidgets::useShinydashboard(),
navbarPage(
"test theme",
tabPanel(
"tab1",
mainPanel(
width = 12,
fluidRow(
box(
width = 12,
title = "title", status = "primary", solidHeader = TRUE,
numericInput("num", "Number of file input", value = 2),
uiOutput("fIn")
)
)
)
)
)
)
server <- function(input, output, session) {
output$fIn <- renderUI({
ind <- as.numeric(input$num)
lapply(1:ind, function(k) {
column(
4,
fileInput(paste0("fIn", k), paste("File:", k), accept = c("xlsx", "text"))
)
})
})
}
shinyApp(ui, server)

Embedding functions into selectizeinput in shinydashboard

I am trying to add functions to the selectizeInput holder in my shinydashboard to use them interactively on my dataframe. Is there a way to display a name for each function (e.g monthly and annual) instead of having the function itself printed out?
ibrary(shiny)
library(shinydashboard)
annual <- function(x){
(x/lag(x, 12) - 1)*100
}
monthly <- function(x){
(x/lag(x) - 1)*100
}
ui <- dashboardPage(
dashboardHeader(title = 'Dashboard'),
dashboardSidebar(sidebarMenu
(menuItem(tabName = 'Panel1', text = 'Panel 1')
)
),
dashboardBody(
tabItems(tabItem(tabName = 'Panel1',
fluidRow(box(selectizeInput('select', 'Select',
choices = c(monthly, annual)),height=80,width=4,
)
),
fluidRow(box(width = 13, height = 655))
)
)
)
)
server <- function(input, output) {
}
shinyApp(ui, server)
You could use a named vector to add labels for the choices:
library(shiny)
library(shinydashboard)
annual <- function(x) {
(x / lag(x, 12) - 1) * 100
}
monthly <- function(x) {
(x / lag(x) - 1) * 100
}
ui <- dashboardPage(
dashboardHeader(title = "Dashboard"),
dashboardSidebar(sidebarMenu
(menuItem(tabName = "Panel1", text = "Panel 1"))),
dashboardBody(
tabItems(tabItem(
tabName = "Panel1",
fluidRow(box(selectizeInput("select", "Select",
choices = c("monthly" = monthly, "annual" = annual)
), height = 80, width = 4, )),
fluidRow(box(width = 13, height = 655))
))
)
)
server <- function(input, output) {
}
shinyApp(ui, server)
#>
#> Listening on http://127.0.0.1:6875

Adding custom number of tabPanels to a tabsetPanel in shiny dashboard

I'm trying to make a tabsetPanel with a number of tabPanels based on a variable form. I've tried the code below, but got some error:
Error in attr(x, "selected") <- TRUE :
trying to specify an attribut in a NULL
Some one could aid me, please?
library(shinydashboard)
library(tidyverse)
form <- 2
ui <- dashboardPage(
title = "Rolê de Aventura", skin="blue",
dashboardHeader(titleWidth = 1024,
title=list(title=tags$img(src="LogoPQ.png",
heigth=45, width=45,
align="left"),
title=tags$p(style="text-align:center;",
"Rolê de Aventura")
)
),
dashboardSidebar(
selectInput("categoria", label = "Categoria:",
choices = list("Quarteto Misto",
"Dupla Masculina",
"Dupla Mista",
"Dupla Feminina"), width="200px"
)
),
dashboardBody(
textInput("equipe", "Nome da equipe:", width = NULL),
tabsetPanel(width = NULL, type = "tabs",
do.call(tabsetPanel,c(width=NULL, type="tabs",
lapply(1:form, function(i) {
tabPanel(title = str_c("Atleta", i),
textInput(str_c("atleta", i), "Nome:", width=NULL),
dateInput(str_c("at.nasc", i), "Nascimento:", width="30%"),
checkboxGroupInput(str_c("at.sex", i), "Sexo:", width="30%",
choices=list("Masculino", "Feminino")))
})
)
)
)
)
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
I can't seem to exactly identify your error. However, there is an answer here.
Ive modified your code accordingly, and it works as intended. See below,
library(shinydashboard)
library(tidyverse)
library(shiny)
form <- 2
ui <- dashboardPage(
title = "Rolê de Aventura", skin="blue",
dashboardHeader(titleWidth = 1024,
title=list(title=tags$img(src="LogoPQ.png",
heigth=45, width=45,
align="left"),
title=tags$p(style="text-align:center;",
"Rolê de Aventura")
)
),
dashboardSidebar(
selectInput("categoria", label = "Categoria:",
choices = list("Quarteto Misto",
"Dupla Masculina",
"Dupla Mista",
"Dupla Feminina"), width="200px"
)
),
dashboardBody(
textInput("equipe", "Nome da equipe:", width = NULL),
do.call(
tabsetPanel,
c(
id = "t",
lapply(
1:form,
FUN = function(x) {
tabPanel(
title = paste("tab", x)
)
}
)
)
)
)
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
I find useful in this cases to use exec() function with !!!.
library(shinydashboard)
library(tidyverse)
library(shiny)
form <- 2
#store the ui code in a list to use later.
tabs <- map(1:form, function(i) {
tabPanel(title = str_c("Atleta", i),
textInput(str_c("atleta", i), "Nome:"),
dateInput(str_c("at.nasc", i), "Nascimento:", width="30%"),
checkboxGroupInput(str_c("at.sex", i), "Sexo:", width="30%",
choices=c("Masculino", "Feminino")))
})
ui <- dashboardPage(
title = "Rolê de Aventura", skin="blue",
dashboardHeader(titleWidth = 1024,
title=list(title=tags$img(src="LogoPQ.png",
heigth=45, width=45,
align="left"),
title=tags$p(style="text-align:center;",
"Rolê de Aventura")
)
),
dashboardSidebar(
selectInput("categoria", label = "Categoria:",
choices = list("Quarteto Misto",
"Dupla Masculina",
"Dupla Mista",
"Dupla Feminina"), width="200px"
)
),
dashboardBody(
textInput("equipe", "Nome da equipe:"),
exec(tabsetPanel, !!!tabs))
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = 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)

R Shiny valueBox and gauge not working together

I'm having an issue with my Shiny App. My app has a valueBox that worked fine before I introduced a gauge from the flexdashboard package.
With the gauge my valueBox not longer renders in the UI.
Reading other posts, I think this is an issue with the flexdashboard package.
Any work arounds would be much appreciated.
Some reproducible code below:
library(shiny)
library(shinydashboard)
#library(flexdashboard)
ui <-dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
fluidRow(
valueBoxOutput("vbox1"),
column(6,box(plotOutput("plt1"),width=12,title="Gauge Graph",background ="green") ),
column(6,box(plotOutput("plt2"),width=12,title="Graph2",background="yellow") )
),
fluidRow( actionButton("plot","plot") )
)
)
server <- shinyServer(function(input, output, session) {
observeEvent(input$plot,{
output$plt1 <- renderPlot({
flexdashboard::gauge(56, min = 0, max = 100, symbol = '%', label = paste("Test Label"),gaugeSectors(
success = c(100, 6), warning = c(5,1), danger = c(0, 1), colors = c("#CC6699")
))
})
output$plt2 <- renderPlot({plot(runif(100),runif(100))})
})
output$vbox1 <- renderValueBox({
valueBox(
"Gender",
input$count,
icon = icon("users")
)
})
})
shinyApp(ui = ui, server = server)
You could use flexdashboard namespace instead of sourcing the library.
You could do something like this:
library(shiny)
library(shinydashboard)
# library(flexdashboard)
ui <-dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
fluidRow(
valueBoxOutput("vbox1"),
column(6,box(flexdashboard::gaugeOutput("plt1"),width=12,title="Gauge Graph",background ="green") ),
column(6,box(plotOutput("plt2"),width=12,title="Graph2",background="yellow") )
),
fluidRow( actionButton("plot","plot") )
)
)
server <- shinyServer(function(input, output, session) {
observeEvent(input$plot,{
output$plt1 <- flexdashboard::renderGauge({
flexdashboard::gauge(56, min = 0, max = 100, symbol = '%', label = paste("Test Label"),
flexdashboard::gaugeSectors(success = c(100, 6), warning = c(5,1), danger = c(0, 1), colors = c("#CC6699")
))
})
output$plt2 <- renderPlot({plot(runif(100),runif(100))})
})
output$vbox1 <- renderValueBox({
valueBox(
"Gender",
input$count,
icon = icon("users")
)
})
})
shinyApp(ui = ui, server = server)
Using this code the app looks like this:
Hope it helps!

Resources