Removing a sidebarPanel for a specific tabPanel - r

I am working with shiny to do un R web application, the application is composed with a sidebarpanel on the left of the mainpanel (with 2 tabspanel) and another sidebarpanel below.
I want to keep the bottom sidebarpanel for the first mainpanel but removing it for the second.
The code looks like :
sidebarPanel(
wellPanel(
fileInput('file1', 'Choisissez les data service ?',
accept = c('text/csv', 'text/comma-separated-values',
'text/tab-separated-values', 'text/plain',
'.csv', '.tsv', 'RData'
)
)
),
wellPanel(
selectInput(inputId = "fonction.de",
label = "En fonction de ?",
choices = fonctions.de,
selected = "perimetre_commercial_estime"
),
selectInput(inputId = "perimetre",
label = "Perimetres commercial",
choices = perimetres,
selected = "2-HDM MARCHAND",
multiple = TRUE
),
checkboxInput(inputId = "case1", label = "Tous perimetres", value = FALSE),
selectInput(inputId = "ae",
label = "AE",
choices = aes,
selected = "AE Paris",
multiple = TRUE
),
checkboxInput(inputId = "case2", label = "Tous AE", value = FALSE),
selectInput(inputId = "segment",
label = "Segment commercial",
choices = segments,
selected = "Premium",
multiple = TRUE
),
checkboxInput(inputId = "case3", label = "Tous segments", value = FALSE)
)
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Graphiques",
plotlyOutput("my.chart")),
tabPanel("Tables",
dataTableOutput("my.table"),
htmlOutput("my.text1"))
)
),
sidebarPanel(
selectInput(inputId = "abscisse",
label = "Abscisse",
choices = abscisses,
selected = "",
multiple = FALSE
),
selectInput(inputId = "ordonnee",
label = "Ordonnee",
choices = ordonnees,
selected = "",
multiple = FALSE
)
),
sidebarPanel(
img(src="Dymetryyy.jpg", height = 150, width = 350)
)
)
)

For people who are facing the same problem, you have to use conditionalPanel to solve it.
mainPanel(
tabsetPanel(id = "bab",
type = "tabs",
tabPanel(value = "graphiques",
"Graphiques",
plotlyOutput("my.chart")),
tabPanel(value = "tables",
"Tables",
dataTableOutput("my.table"),
htmlOutput("my.text1"))
)
),
conditionalPanel(condition = "input.bab == 'graphiques'",
sidebarPanel(
selectInput(inputId = "abscisse",
label = "Abscisse",
choices = abscisses,
selected = "",
multiple = FALSE
),
selectInput(inputId = "ordonnee",
label = "Ordonnee",
choices = ordonnees,
selected = "",
multiple = FALSE
)
),
sidebarPanel(
img(src="dymetryyy", height = 150, width = 350)
)
)

Related

How to align shiny input boxes, specifically selectInput and numericInput

I'm a shiny newbie and was thrilled when I used SO to figure out how to filter one selectInput based on another. However, as I added more inputs they became misaligned and I was hoping there was a simple fix. I do not know HTML at all. And while I searched for similar questions, the different shiny layouts and other code overhead made it difficult to understand the way to fix.
I'm assuming I just need to include the UI part for a reproducible example rather than confuse future searches with all of the "overhead" of longer code, so I am including the UI here. Please let me know if I need to include more.
The shiny input is more and more off with additional inputs as you can see:
Here is the reproducible example:
# Define UI
ui <- fluidPage(headerPanel(strong("Carbohydrate Calculator")),
fluidRow(
column(
width = 3,
selectInput(
inputId = 'cat_1',
label = 'Food Category',
choices = c("None", categories)
),
selectInput(
inputId = 'cat_2',
label = 'Food Category',
choices = c("None", categories)
),
selectInput(
inputId = 'cat_3',
label = 'Food Category',
choices = c("None", categories)
),
selectInput(
inputId = 'cat_4',
label = 'Food Category',
choices = c("None", categories)
),
selectInput(
inputId = 'cat_5',
label = 'Food Category',
choices = c("None", categories)
),
selectInput(
inputId = 'cat_6',
label = 'Food Category',
choices = c("None", categories)
),
selectInput(
inputId = 'cat_7',
label = 'Food Category',
choices = c("None", categories)
),
selectInput(
inputId = 'cat_8',
label = 'Food Category',
choices = c("None", categories)
)
),
column(
width = 3,
selectInput(
inputId = 'food_1',
label = 'Food Item',
choices = foods[1]
),
selectInput(
inputId = 'food_2',
label = 'Food Item',
choices = foods[1]
),
selectInput(
inputId = 'food_3',
label = 'Food Item',
choices = foods[1]
),
selectInput(
inputId = 'food_4',
label = 'Food Item',
choices = foods[1]
),
selectInput(
inputId = 'food_5',
label = 'Food Item',
choices = foods[1]
),
selectInput(
inputId = 'food_6',
label = 'Food Item',
choices = foods[1]
),
selectInput(
inputId = 'food_7',
label = 'Food Item',
choices = foods[1]
),
selectInput(
inputId = 'food_8',
label = 'Food Item',
choices = foods[1]
)
),
column(
width = 3,
numericInput(
inputId = "actual_serving_1",
label = "How much?",
value = "",
min = 0,
max = 100
),
numericInput(
inputId = "actual_serving_2",
label = "How much?",
value = "",
min = 0,
max = 100
),
numericInput(
inputId = "actual_serving_3",
label = "How much?",
value = "",
min = 0,
max = 100
),
numericInput(
inputId = "actual_serving_4",
label = "How much?",
value = "",
min = 0,
max = 100
),
numericInput(
inputId = "actual_serving_5",
label = "How much?",
value = "",
min = 0,
max = 100
),
numericInput(
inputId = "actual_serving_6",
label = "How much?",
value = "",
min = 0,
max = 100
),
numericInput(
inputId = "actual_serving_7",
label = "How much?",
value = "",
min = 0,
max = 100
),
numericInput(
inputId = "actual_serving_8",
label = "How much?",
value = "",
min = 0,
max = 100
)
),
column(8,
tableOutput("my_table"),
span(textOutput("my_message"), style="color:red")
) # Column close
) # fluidRow close
) # fluidPage close
To better align the elements in a row, it may be better to fill the elements in a row. Try this
# Define UI
ui <- fluidPage(headerPanel(strong("Carbohydrate Calculator")),
fluidRow(
column(
width = 3,
selectInput(
inputId = 'cat_11',
label = 'Food Category',
choices = c("None", categories)
)),
column(
width = 3,
selectInput(
inputId = 'food_11',
label = 'Food Item',
choices = foods[1]
)),
column(
width = 3,
numericInput(
inputId = "actual_serving_11",
label = "How much?",
value = "",
min = 0,
max = 100
))
),
fluidRow(
column(
width = 3,
selectInput(
inputId = 'cat_12',
label = 'Food Category',
choices = c("None", categories)
)),
column(
width = 3,
selectInput(
inputId = 'food_12',
label = 'Food Item',
choices = foods[1]
)),
column(
width = 3,
numericInput(
inputId = "actual_serving_12",
label = "How much?",
value = "",
min = 0,
max = 100
))
),
fluidRow(
column(
width = 3,
selectInput(
inputId = 'cat_13',
label = 'Food Category',
choices = c("None", categories)
)),
column(
width = 3,
selectInput(
inputId = 'food_13',
label = 'Food Item',
choices = foods[1]
)),
column(
width = 3,
numericInput(
inputId = "actual_serving_13",
label = "How much?",
value = "",
min = 0,
max = 100
))
),
fluidRow(
column(
width = 3,
selectInput(
inputId = 'cat_14',
label = 'Food Category',
choices = c("None", categories)
)),
column(
width = 3,
selectInput(
inputId = 'food_14',
label = 'Food Item',
choices = foods[1]
)),
column(
width = 3,
numericInput(
inputId = "actual_serving_14",
label = "How much?",
value = "",
min = 0,
max = 100
))
),
fluidRow(
column(
width = 3,
selectInput(
inputId = 'cat_15',
label = 'Food Category',
choices = c("None", categories)
)),
column(
width = 3,
selectInput(
inputId = 'food_15',
label = 'Food Item',
choices = foods[1]
)),
column(
width = 3,
numericInput(
inputId = "actual_serving_15",
label = "How much?",
value = "",
min = 0,
max = 100
))
),
fluidRow(
column(
width = 3,
selectInput(
inputId = 'cat_16',
label = 'Food Category',
choices = c("None", categories)
)),
column(
width = 3,
selectInput(
inputId = 'food_16',
label = 'Food Item',
choices = foods[1]
)),
column(
width = 3,
numericInput(
inputId = "actual_serving_16",
label = "How much?",
value = "",
min = 0,
max = 100
))
)
# column(8,
# tableOutput("my_table"),
# span(textOutput("my_message"), style="color:red")
# ) # Column close
) # fluidPage close
server <- function(input, output) {}
shinyApp(ui = ui, server = server)

Insert bspopover in Shiny

I would like to insert a bspopover next to the text: "Shapefile Import". For the Filter options I was able to insert as you can see in the code below, however for fileImput no. The executable code is below.
can anybody help me?
Thank you!
library(shinyBS)
library(shiny)
popoverTempate <-
'<div class="popover popover-lg" role="tooltip"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'
DES_filter1<-paste("Text text text text text text.", sep = "<br>")
ui <- fluidPage(
tags$head(
tags$style(HTML(".popover.popover-lg {width: 500px; max-width: 500px;}"))
),
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
fileInput("shp", h3("Shapefile import"), multiple = TRUE, accept = c('.shp', '.dbf','.sbn', '.sbx', '.shx', '.prj')),
radioButtons(
"filter1",
h3("Select properties"),
choiceValues = c(1, 2),
choiceNames = list(
tagList(
tags$span("All properties"),
tags$span(icon("info-circle"), id = "icon1", style = "color: blue;")
),
tagList(
tags$span("Exclude properties"),
tags$span(icon("info-circle"), id = "icon2", style = "color: blue;")
)
),
selected = 1
),
bsPopover("icon1", "TITLE1", DES_filter1, placement = "right",
options = list(template = popoverTempate)),
bsPopover("icon2", "TITLE2", "CONTENT2", placement = "right"),
radioButtons("filter2", h3("Select"),
choices = list("All" = 1,
"Exclude" = 2),
selected = 1),
),
mainPanel(
)
)
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
You can add the icon in the fileInput title:
sidebarPanel(
fileInput("shp",
h3(
span("Shapefile import"),
span(icon("info-circle"), id = "icon3", style = "color: blue")
),
multiple = TRUE,
accept = c('.shp', '.dbf','.sbn', '.sbx', '.shx', '.prj')
),
bsPopover("icon3", "TITLE3", "CONTENT3", placement = "right"),
...

Adjusting location of Main panel in Shiny

I have created the following UI using R shiny. The script is as follows.
The first part deals with the sidebar layout.
ui <- fluidPage(
titlePanel("Graphingtool"),
h3("DB"),
hr(),
fluidRow(
column(12,
fluidRow(
column(3,
fileInput("file1", 'DB',
multiple = TRUE,
accept = c("xlsx/xls",".xls",".xlsx"))),# We have added the formats here- will load excel alone- in case csv/txt needed amend here
column(5,
column(4,downloadButton(outputId = "Downloaddata", label = "Downloader"))
)
),
fluidRow(
column(3,
textInput(inputId = 'T',label = 'T'),
textInput(inputId = 'C',label = 'C'),
textInput(inputId = "P", label = "P"),
column(2, offset = 2,
actionButton(inputId = "CLS", label = "CLS"))
),
column(5,
textInput(inputId = "P", label = "P"),
textInput(inputId = "Y", label = "Y"),
numericInput(inputId = "Numberinput", label = "Ninput", min = -1000000000, max = 1000000000, value = 0,step = 1)
)),
fluidRow(
column(3,
radioButtons("dist", "Vertical Axis Scale",
c("Linear" = "Linear Reg",
"Log" = "Log reg"))),
column(5,
radioButtons("dist", "Horizontal Axis Scale",
c("Linear" = "Linear reg",
"Log" = "Log reg")))
),
fluidRow(
column(5,
sliderInput(inputId = "Scale", label = "Scale", min = 0, max = 100,
step = 2.5, value = 1))) ) )
Here we create the main panel
,mainPanel(
tabsetPanel(
tabPanel("Plot", plotOutput("plot")),
tabPanel("Table", tableOutput("table"))
)))
server<-function(input, output){reactive({})}
shinyApp(ui, server)
I Have created the main panel. However, the Panel with two tabs is opening under the sidebar panel and not on the right hand side of the panel.
The script works but is it possible to open the main panel on the right of the sidebar panel. I request someone to guide me here.
Take a look on this if this okay or not for you..
library(shiny)
ui <- fluidPage(
titlePanel("Graphingtool"),
h3("DB"),
hr(),
sidebarLayout(
sidebarPanel(
fluidRow(
column(5,
ailgn="center",
fileInput("file1", 'DB',
multiple = TRUE,
accept = c("xlsx/xls",".xls",".xlsx"))),# We have added the formats here- will load excel alone- in case csv/txt needed amend here
column(5,
ailgn="center",
HTML('<br/>'),
column(4,downloadButton(outputId = "Downloaddata", label = "Downloader"))
)
),
fluidRow(
column(5,
ailgn="center",
textInput(inputId = 'T',label = 'T'),
textInput(inputId = 'C',label = 'C'),
textInput(inputId = "P", label = "P"),
column(2, offset = 2,
ailgn="center",
actionButton(inputId = "CLS", label = "CLS"))
),
column(5,
ailgn="center",
textInput(inputId = "P", label = "P"),
textInput(inputId = "Y", label = "Y"),
numericInput(inputId = "Numberinput", label = "Ninput", min = -1000000000, max = 1000000000, value = 0,step = 1)
)),
HTML('<br/>'),
fluidRow(
column(5,
ailgn="center",
radioButtons("dist", "Vertical Axis Scale",
c("Linear" = "Linear Reg",
"Log" = "Log reg"))),
column(5,
ailgn="center",
radioButtons("dist", "Horizontal Axis Scale",
c("Linear" = "Linear reg",
"Log" = "Log reg")))
),
fluidRow(
column(5,
ailgn="center",
sliderInput(inputId = "Scale", label = "Scale", min = 0, max = 100,
step = 2.5, value = 1)))
)
,mainPanel(
tabsetPanel(
tabPanel("Plot", plotOutput("plot")),
tabPanel("Table", tableOutput("table"))
)))
)
server<-function(input, output){reactive({})}
shinyApp(ui, server)
You can tweak the size by adjusting column

Arrange R shiny Side bar objects

I have created the following interface using R shiny.
library(shiny)
ui <- fluidPage(
#Can be fluid row also
fluidRow(
column(3,
h3("Excel DB"),
hr(),
fileInput("file1", "Excel DB",
multiple = TRUE,
accept = c("text/csv/xlsx/xls",
"text/comma-separated-values,text/plain",
".csv", ".xls", ".xlsx")),
textInput(inputId = 'Type',label = 'Type'),
textInput(inputId = 'Client',label = 'Client'),
textInput(inputId = "Task", label = "Task"),
actionButton(inputId = "ClearAll", label = "Clear Screen"),
radioButtons("dist", "Vertical Axis Scale",
c("Linear" = "Linear Regression",
"Log" = "Logistic Regression"))
),
column(5,h5(' '),
hr(), downloadButton(outputId = "Downloaddata", label = "Fetch Dataset"),
textInput(inputId = "Commodity", label = "Commodity"),
textInput(inputId = "Year", label = "Year"),
radioButtons("dist", "Horizontal Axis Scale",
c("Linear" = "Linear Regression",
"Log" = "Logistic Regression")),
numericInput(inputId = "Numberinput", label = "Numinput", min = -1000000000, max = 1000000000, value = 0,step = 1)
)
), sliderInput(inputId = "Scale", label = "Scale", min = 0, max = 100, step
= 2.5, value = 1)
)
I have created the following empty server
server<-function(input, output){}
shinyApp(ui, server)
The above code creates a shiny application with a sidebar panel having two columns. However, the 2 columns are not aligned with each other. I would like to know how to arrange the components of the app as follows.
The fetch dataset button should be along the browse button. Similarly the commodity text box should be next to type text box and the year box should come near the client. The two sets of radio buttons should align with each other. I request some help here. Am unable to arrange them in that pattern
To achieve this you need to do some nesting of column() and fluidRow(). This answer should explain enough to get you started, along with the examples on Shiny's layout guide. I believe the following should get you roughly what you want:
library(shiny)
ui <- fluidPage(
#Can be fluid row also
h3("Excel DB"),
hr(),
fluidRow(
column(12,
fluidRow(
column(3,
fileInput("file1", NULL,
multiple = TRUE,
accept = c("text/csv/xlsx/xls",
"text/comma-separated-values,text/plain",
".csv", ".xls", ".xlsx"))),
column(5,
column(3,
downloadButton(outputId = "Downloaddata", label = "Fetch Dataset")),
column(2, offset = 2,
actionButton(inputId = "ClearAll", label = "Clear Screen"))
)
),
fluidRow(
column(3,
textInput(inputId = 'Type',label = 'Type'),
textInput(inputId = 'Client',label = 'Client'),
textInput(inputId = "Task", label = "Task")
),
column(5,
textInput(inputId = "Commodity", label = "Commodity"),
textInput(inputId = "Year", label = "Year"),
numericInput(inputId = "Numberinput", label = "Numinput", min = -1000000000, max = 1000000000, value = 0,step = 1)
)),
fluidRow(
column(3,
radioButtons("dist", "Vertical Axis Scale",
c("Linear" = "Linear Regression",
"Log" = "Logistic Regression"))),
column(5,
radioButtons("dist", "Horizontal Axis Scale",
c("Linear" = "Linear Regression",
"Log" = "Logistic Regression")))
),
fluidRow(
column(5,
sliderInput(inputId = "Scale", label = "Scale", min = 0, max = 100,
step = 2.5, value = 1))
)
)
)
)
Created on 2018-09-20 by the reprex package (v0.2.1)

How to add conditional rows which consist of few user inputs in Shiny app

I would like to show in UI only the first row (currently fluidRow) with device, color and linetype if user chooses number one, two first rows if user chooses number two and so on.
How do I add this kind of condition. I tried conditionalPanel but I failed there.
Tried to make a minimal reproducible example:
library(shiny)
shinyApp(
ui <- fluidPage(
fluidRow(
column(2, offset = 1,
selectInput(inputId = "number",
label = "Number:",
choices = list(
"One data set" = "1",
"Two data sets" = "2",
"Three data sets" = "3"))
)
),
fluidRow(
column(2,
selectInput(inputId = "dev1",
label = "Device:",
choices = list("Device 1", "Device 2"))
),
column(2,
selectInput(inputId = "col1",
label = "Color:",
choices = list("black", "red", "blue"))
),
column(2,
selectInput(inputId = "lty1",
label = "Linetype:",
choices = list("solid", "dashed"))
)
),
fluidRow(
column(2,
selectInput(inputId = "dev2",
label = "Device:",
choices = list("Device 1", "Device 2"))
),
column(2,
selectInput(inputId = "col2",
label = "Color:",
choices = list("black", "red", "blue"))
),
column(2,
selectInput(inputId = "lty3",
label = "Linetype:",
choices = list("solid", "dashed"))
)
)
),
server <- function(input, output) {})
Tell me if this needs more clarifying.
solution using server side logic, client side (JS) is also an option however this seems more natural given the platform in R and also it offers more control.
library(shiny)
library(shinyFiles)
shinyApp(
ui = fluidPage(
fluidRow(
column(2, offset = 1,
selectInput(inputId = "number",
label = "Number:",
choices = list(
"One data set" = "1",
"Two data sets" = "2",
"Three data sets" = "3"))
)
),
uiOutput('dynamic_selections')
),
server = function(input, output) {
observeEvent(input$number,{
output$dynamic_selections = renderUI({
lapply(1:as.integer(input$number),function(i){
fluidRow(
column(2,
selectInput(inputId = sprintf("dev%s",i),
label = "Device:",
choices = list("Device 1", "Device 2"))
),
column(2,
selectInput(inputId = sprintf("col%s",i),
label = "Color:",
choices = list("black", "red", "blue"))
),
column(2,
selectInput(inputId = sprintf("lty%s",i),
label = "Linetype:",
choices = list("solid", "dashed"))
),
column(2,
shinyFilesButton(id=sprintf("file%s",i), label='File:',title = 'Select File',multiple=FALSE, buttonType = "default", class = NULL)
)
)
})
})
lapply(1:as.integer(input$number),function(i){
shinyFileChoose(input, sprintf("file%s",i), roots=c(wd='.'), filetypes=c('', 'txt'))
})
})
})
EDIT: it has been highlighted that in some scenarios the file chooser box is not populated. Two things could go wrong
JS cannot read date attribute of folder/file therefore array is populated with null and errors when trying string split:
var mTime = data.files.mtime[i].split('-')
as a temp solution this can be avoided using restrictions parameter to exclude problematic folders/files?
When shinyFilesButton() is rendered via renderUI and server logic then fileChooser fails to populate but without any JS error, needs to be investigated further

Resources