problem with selecting variables/columns using radioButtons and selectInput in Shiny - r

I'm unable to select/unselect different columns of mtcars dataset using both radioButtons and selectInput function in Shiny.
Can someone please help me out as i'm stuck on it since last 2 days.
I shall be extremely grateful.
Regards
data(mtcars)
#Ui
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
column(width = 10,
radioButtons(inputId="variables", label="Select variables:",
choices = c("All","mpg","cyl","disp"),
selected = "All", inline = TRUE )),
column(width = 10,
selectInput(inputId = "level", label = "Choose Variables to
display", multiple = TRUE, choices = names(mtcars)[4:11]))),
mainPanel (
h2("mtcars Dashboard"),
DT::dataTableOutput("table"))))
#server
server<-function(input, output) {
output$table <- DT::renderDataTable(DT::datatable(filter='top', editable = TRUE, caption = 'mtcars',
{
data <- mtcars
data<-data[,input$variables,drop=FALSE]
column = names(mtcars)
if (!is.null(input$level)) {
column = input$level }
data
})) }
shinyApp(ui = ui, server = server)

library(shiny)
library(DT)
data(mtcars)
#Ui
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
column(width = 10,
radioButtons(inputId="variables", label="Select variables:",
choices = c("All","mpg","cyl","disp"),
selected = "All", inline = TRUE )),
column(width = 10,
selectInput(inputId = "level", label = "Choose Variables to
display", multiple = TRUE, choices = names(mtcars)[4:11]))),
mainPanel (
h2("mtcars Dashboard"),
DT::dataTableOutput("table"))
))
#server
server<-function(input, output, session) {
data <- mtcars
tbl <- reactive({
if(input$variables=='All'){
data
}else{
data[,c(input$variables,input$level),drop=FALSE]
}
})
output$table <- DT::renderDataTable(DT::datatable(filter='top', caption='mtcars', tbl()))
}
shinyApp(ui = ui, server = server)
Here is what I understand from your requirements, I hope it what you are looking for. Always try to avoid calculations inside render*.

Related

R Shiny: How to Displayed values in filter with ascending Order

How to Displayed values in filter with ascending Order.
Want to displayed filter of week in dataTableOutput with value in ascending order.
Here is the code of ui.R
fluidPage(
titlePanel("Delivery Assurance Matrix"),
fluidRow(
column(4,
selectInput("week_count",
"Week",
c("All",
sort(unique(as.character(data$Week))))
))),
DT::dataTableOutput("table")
)
Here is the code of server.R
function(input, output) {
output$table <- DT::renderDataTable(DT::datatable({
data<-data
if (input$week_count != "All") {
data <- data[data$Week >= input$week_count,]
}
data
}))
}
But in UI Values not in ordering
You can also you shinyWidgets package which has Select All Option
library(shiny)
library(shinyWidgets)
data <- c(11,1,2,3,10,21)
ui <- fluidPage(
titlePanel("Delivery Assurance Matrix"),
fluidRow(
column(4,
pickerInput(
inputId = "week_count",
label = "Week",
choices = sort(data),
multiple = TRUE,
options = list(
`actions-box` = TRUE,
`deselect-all-text` = "None...",
`select-all-text` = "Select All",
`none-selected-text` = "None Selected"
)
)
)),
DT::dataTableOutput("table")
)
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
Its solved by this changes.
fluidRow(
column(4,
selectInput("week_count",
"Week",
c("All",
order(sort(unique(as.character(data$Week)))))
))

Subset data in R Shiny using Multiple Variables

I am new to R Shiny. I am attempting to create an app that allows a user to subset a data.frame based on multiple variables and then see the resulting data.
Here is a small example data set:
iter,wave,apples
1,1,600
1,1,500
1,1,400
1,2,300
1,2,200
1,2,100
2,1,1000
2,1,1100
2,1,1200
2,2,1300
2,2,1400
2,2,1500
3,1,1100
3,1,2200
3,1,3300
3,2,4400
3,2,5500
3,2,6600
I would like the user to be able to specify the value of iter and of wave and see the resulting data.
Here is my attempt at the Shiny code. I realize I must be making several silly mistakes.
Edit
Here is my revised code. The end result now comes pretty close to what I want. The sidebar is still not being displayed perfectly.
library(shiny)
setwd('C:/Users/mark_/Documents/simple_RShiny_files/explore')
apple.data <- read.csv('subset_data_based_on_multiple_variables.csv',
header = TRUE, stringsAsFactors = FALSE)
ui <- fluidPage(
titlePanel("Subsetting Apple Dataset"),
sidebarLayout(
sidebarPanel(
uiOutput("codePanel")
),
mainPanel(
tableOutput("view")
)
),
selectInput("codeInput", inputId ="data1", label = "Choose Iter", choices = unique(apple.data$iter)),
selectInput("codeInput", inputId ="data2", label = "Choose Wave", choices = unique(apple.data$wave))
)
server <- function(input, output) {
output$codePanel <- renderUI({
})
dataset <- reactive({
subset(apple.data, (iter == input$data1 & wave == input$data2))
})
output$view <- renderTable(dataset())
}
shinyApp(ui = ui, server = server)
The output
The problem is that both selectInputs have the same inputId. This works:
library(shiny)
apple.data <- data.frame(
iter = c(1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,
2L,3L,3L,3L,3L,3L,3L),
wave = c(1L,1L,1L,2L,2L,2L,1L,1L,1L,2L,2L,
2L,1L,1L,1L,2L,2L,2L),
apples = c(600L,500L,400L,300L,200L,100L,1000L,
1100L,1200L,1300L,1400L,1500L,1100L,2200L,3300L,4400L,
5500L,6600L)
)
ui <- fluidPage(
titlePanel("Subsetting Apple Dataset"),
sidebarLayout(
sidebarPanel(
selectInput("codeInput1", label = "Choose Iter", choices = unique(apple.data$iter)),
selectInput("codeInput2", label = "Choose Wave", choices = unique(apple.data$wave))
),
mainPanel(
tableOutput("view")
)
)
)
server <- function(input, output) {
dataset <- reactive({
return(subset(apple.data, (iter == input$codeInput1 & wave == input$codeInput2)))
})
output$view <- renderTable(dataset())
}
shinyApp(ui = ui, server = server)

picketInput change placeholder R Shiny Widgets

pickerInput from shinyWidgets package has a placeholder title Nothing selected.
How is it possible to replace it with Pick a choice for example ? I would prefer a solution that uses css or the options of pickerInput, if possible avoid shinyjs.
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
tags$head(
tags$style(HTML("
"))
),
pickerInput(
inputId = "mtcInputIndicateur",
label = "Select values",
choices = paste("choice", 1:10),
options = list(`live-search` = TRUE),
multiple = TRUE
)
)
server <- function(input, output){
}
shinyApp(ui, server)
Any help would be greatly appreciated.
Just found the answer, use the parameter title in options.
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
pickerInput(
inputId = "mtcInputIndicateur",
label = "Select values",
choices = paste("choice", 1:10),
options = list(`live-search` = TRUE, title = "Pick a choice"),
multiple = TRUE
)
)
server <- function(input, output){
}
shinyApp(ui, server)

dynamic number of selectInput

I am new to shiny so this might be a very basic question.
I want to write a shiny app where the user inputs 'n' and we get n number of selectInput options and am not able to do it. Basically any form of for loop is not working.
The code I attempted is following
library(shiny)
ui = fluidPage(
sidebarLayout(
sidebarPanel(
textInput(inputId = "number", label = "number of selectInput",value = 5)
),
mainPanel(
uiOutput(outputId = "putselect")
)
)
)
server = function(input,output){
output$putselect = renderUI(
if(input$number != 0 ){
for(i in 1:(input$number)){
selectInput(inputId = "i", label = "just write something", choices = c(2,(3)))
}
}
)
}
shinyApp(ui = ui , server = server)
You either need to store the inputs you create in a list and return that list, or you can simply wrap your statement in lapply instead of for. A working example is given below, hope this helps!
library(shiny)
ui = fluidPage(
sidebarLayout(
sidebarPanel(
textInput(inputId = "number", label = "number of selectInput",value = 5)
),
mainPanel(
uiOutput(outputId = "putselect")
)
)
)
server = function(input,output){
output$putselect = renderUI(
if(input$number != 0 ){
lapply(1:(input$number), function(i){
selectInput(inputId = "i", label = paste0("input ",i), choices = c(2,(3)))
})
}
)
}
shinyApp(ui = ui , server = server)

Tabs with different sidebars

I am trying to create a shiny app with multiple tabs. Each tab is to have its own sidebar. I haven't been able to get this to work. Any help on what is wrong would be appreciated.
Below is the code
ui <- fluidPage(
titlePanel("Hi"),
sidebarLayout(position = "left",
sidebarPanel(
conditionalPanel(condition = "input.tabs1==1",
selectizeInput('invar',"Reg in", choices = varnames, multiple = TRUE)),
conditionalPanel(condition = "input.tabs1==2",
selectizeInput('outvar',"Reg out", choices = predictors, multiple = FALSE)),
),
mainPanel(
tabsetPanel(id="tabs1",
tabPanel("input",value=1,plotOutput("Input"),
tabPanel("output",value=2,plotOutput("Output")
))))
))
First of all, check your code again. You made following mistakes:
one tabPanel is nested inside the other one
there's an extra comma at the end of the second conditionalPanel(), so you pass an empty element to sidebarPanel()
If I correct your mistakes and create a mock example, it works perfectly fine as is. So there isn't really a problem here:
ui <- fluidPage(
titlePanel("Hi"),
sidebarLayout(position = "left",
sidebarPanel(
conditionalPanel(condition = "input.tabs1==1",
selectizeInput('invar',"Reg in", choices = letters[1:3], multiple = TRUE)),
conditionalPanel(condition = "input.tabs1==2",
selectizeInput('outvar',"Reg out", choices = letters[4:6], multiple = FALSE))
),
mainPanel(
tabsetPanel(id="tabs1",
tabPanel("input",value=1,plotOutput("Input")),
tabPanel("output",value=2,plotOutput("Output"))
)
)
))
server <- function(input, output, session){
output$Input <- renderPlot(plot(1))
output$Output <- renderPlot(plot(2))
}
shinyApp(ui, server)
You could do this as well by using renderUI:
ui <- fluidPage(
titlePanel("Hi"),
sidebarLayout(position = "left",
sidebarPanel(
uiOutput("mysidebar")
),
mainPanel(
tabsetPanel(id="tabs1",
tabPanel("input",value=1,plotOutput("Input")),
tabPanel("output",value=2,plotOutput("Output")
)))
))
server <- function(input, output, session){
output$mysidebar <- renderUI({
if(input$tabs1 == 1){
selectizeInput('invar',"Reg in", choices = letters[1:3])
} else if(input$tabs1 == 2){
selectizeInput('outvar',"Reg out", choices = letters[4:6])
}
})
}
shinyApp(ui,server)
I do this in a very different but effective way.
shinyApp(
shinyUI(
fluidPage(
uiOutput('mainpage')
)
),
shinyServer(function(input, output, session) {
panel <- reactiveValues(side = NULL)
output$mainpage <- renderUI({
sidebarLayout(position = "left",
sidebarPanel(
uiOutput(panel$side)
),
mainPanel(
tabsetPanel(id="tabs1",
tabPanel("input",value=1,plotOutput("Input")),
tabPanel("output",value=2,plotOutput("Output"))
)
)
})
output$sideinput <- renderUI({
tagList(
selectizeInput('invar',"Reg in", choices = varnames, multiple = TRUE))
)
})
output$sideoutput <- renderUI({
tagList(
selectizeInput('outvar',"Reg out", choices = predictors, multiple =FALSE)
)
})
observeEvent(input$tabs1,{
panel$side <- switch(input$tabs1,
1 = 'sideinput',
2 = 'sideoutput')
})
basically I am using observers as my conditionals and assigning the value of the desired panel to the variable name assigned to that panel position

Resources