Displaying selectInput in sidebar based on tabsetPanel selection in R shiny - r

I'm trying to generate a list in a selectInput, dynamically. I have a sidebarPanel in which I have declared a tabsetPanel. Each tabsetPanel will have different outputs, all of which I want to display in the sidebar. So the output of the first tab would be a selectInput or perhaps 2 selectInputs, while the same would go for the second tab.
Here is my sidebarPanel code in ui.R
## ui.R
sidebarPanel(
tabsetPanel(
tabPanel("aZ", uiOutput("aToZPlayerList")),
tabPanel("byTeam", uiOutput("byTeamPlayerList"))
),
),.......
In server.R, I have written the following:
## server.R
output$aToZPlayerList <- renderUI({
selectInput("alphabet", "Players A-Z", choices=aToZ, selected=0)
htmlOutput("List")
})
output$byTeamPlayerList <- renderUI({
selectInput("team", "Teams", choices=teamList, selected=0)
htmlOutput("List")
})
But this does not work, as nothing is rendered in the sidebarPanel. I feel like I'm missing something, but as I'm quite new to this, I still haven't figured it out.
I haven't been able to find an answer on here or elsewhere as of yet. Any help please? Thanks.

You should not have two widgets under one uiOutput, i would recommend to separate it:
## ui.R
sidebarPanel(
tabsetPanel(
tabPanel("aZ", uiOutput("aToZPlayerList"), htmlOutput("List")),...
## server.R
output$aToZPlayerList <- renderUI({
selectInput("alphabet", "Players A-Z", choices=aToZ, selected=0)
})...
and it will work.
[FULL EXAMPLE]
library(shiny)
fruits <- c("banana","raccoon","duck","grapefruit")
ui <- pageWithSidebar(
# Application title
headerPanel("Hello Shiny!"),
# Sidebar with a slider input
sidebarPanel(
tabsetPanel(
tabPanel("aZ", uiOutput("aToZPlayerList"), htmlOutput("List")),
tabPanel("byTeam", uiOutput("byTeamPlayerList"))
)),
# Show a plot of the generated distribution
mainPanel()
)
server <- function(input,output){
output$aToZPlayerList <- renderUI({
selectInput("alphabet", "Players A-Z", choices=c("A","B","C"), selected="A")
})
output$List <- renderUI({
HTML(paste(fruits))
})
output$byTeamPlayerList <- renderUI({
selectInput("team", "Teams", choices=c("A","B","C"), selected="B")})
}
runApp(list(ui=ui,server=server))

Related

One reactive function to be displayed on two different pages interactively

I have an application which has 3 tabItems. I want to use a slider on second page to display same result on 3rd page interactively, i.e. if 2nd page slider changes then 3rd page slider should also change respectively.
I have a reactive function on server side
choose_segment <- reactive({
Multiple conditions for dropdown{Due to security cant share the exact code.}
})
and this choose_segment is refered in UI once and now i want to use it on the third page as well, but when i am calling the function on third page it is not displaying any thing on ui and also not giving any error.
in UI it is called inside UIoutput.
uiOutput(choose_segment())
My observations : I think as per my study we can not call one function directly twice, so what i am doing is i have made two different functions and calling same function from them, i.e.
output$chooseSegment1 <- renderUI({
choose_segment()
})
output$chooseSegment2 <- renderUI({
choose_segment()
})
Issue : it is giving me output but they both are not interactive :(
Kindly provide a solution so that i can make both the sliders work in interactive manner.
I have faced the same scenario, in that i was suppose to change the code structure.
I made dynamic output uiOutput to the Dropdown menu ob ui and then used the same in my server as Input$xyz in observe on server and it worked for me.
Code :
UI : column(3, selectInput(inputId="ABC",label= "Choose ABC"))
column(3, selectInput(inputId="ABC1",label= "Choose ABC"))
Server : observe({
if(is.null(tab2_summary())) return(NULL)
updateSelectInput(session, "ABC", value = input$ABC)
})
observe({
updateSelectInput(session, "ABC1", value = input$ABC)
})
observe({
updateSelectInput(session, "ABC", value = input$ABC1)
})
So this is how i was able to make the selectInput interactive on two different page.
For your reference there is one full reproducible code.
Kindly refer,
library(shiny)
# UI ----------------------------------------------------------
ui <- navbarPage("Navbar!",
tabPanel("Plot", sidebarLayout(sidebarPanel(
radioButtons("yaxis1", "y-axis", c("speed"="speed", "dist"="dist"),
selected = "speed"
)),
mainPanel( plotOutput("plot"),
textOutput("test2")))), # for input checking
tabPanel("Summary", sidebarLayout(sidebarPanel(
radioButtons("yaxis2", "grouping-var", c("speed"="speed", "dist"="dist")
)),
mainPanel(
verbatimTextOutput("summary"),
textOutput("test1")
)))
)
# Server ------------------------------------------
server <- function(input, output, session) {
observe({
x <- input$yaxis1
updateRadioButtons(session, "yaxis2", selected = x)
})
observe({
y <- input$yaxis2
updateRadioButtons(session, "yaxis1", selected = y)
})
# output$test1 <- renderPrint({cat("yaxis1", input$yaxis1)})
# output$test2 <- renderPrint({cat("yaxis2", input$yaxis2)})
# output$plot <- renderPlot({ plot(cars[['speed']], cars[[input$yaxis1]]) })
# output$summary <- renderPrint({ summary(cars[[input$yaxis2]]) })
}
shinyApp(ui, server)
I Hope it will of your help.

Shiny check reactiveValue existence with validate -- Not Found

I have a shiny code like in the below. I need to define variables as reactiveValues to be updatable (or I could define them I think as global but then I have to press clean objects from Rstudio which is not very user-friendly).I try to run a validate code to check for existence of the data I have defined as reactiveValues. validate(need(exists("GSEmRNA$d"),message="Dataframe not found")) yields "Dataframe not found" thus, does not plot my boxplot. If I define them as global variables and forget to press clean objects, code might mix up as old data can be passed as if it is new. Any help is appreciated.
server.R
shinyServer(function(input, output) {
observeEvent(input$GoButton,{
dataset <- data.frame(first= c(1,5,9),second=c(8,5,13), third=c(10,3,17))
GSEmRNA <- reactiveValues(d=dataset)
})
output$BoxplotDataset <- renderPlot({
if (input$GoButton== 0) {return()}
else{
validate(need(exists("GSEmRNA$d"),message="Dataframe not found"))
boxplot(GSEmRNA$d)}
})
})
ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Dataset Selection"),
sidebarPanel(
actionButton("GoButton","GO")
),
mainPanel(
wellPanel(
column(8, plotOutput("BoxplotDataset")
)
)
)))
FOR THE RECORD, I ALSO POSTED THIS QUESTION TO SHINY GOOGLE DISCUSS GROUP https://groups.google.com/forum/#!topic/shiny-discuss/ZV5F6Yy-kFg
Here are the updated code. The points are:
library(shiny)
server <-shinyServer(function(input, output) {
GSEmRNA <- reactiveValues(d=NULL) #define it ouside
observeEvent(input$GoButton,{
dataset <- data.frame(first= c(1,5,9),second=c(8,5,13), third=c(10,3,17))
GSEmRNA$d <- dataset #assign it inside
})
output$BoxplotDataset <- renderPlot({
validate(need(GSEmRNA$d,"Dataframe not found")) # changed as well
boxplot(GSEmRNA$d)
})
})
ui <- pageWithSidebar(
headerPanel("Dataset Selection"),
sidebarPanel(
actionButton("GoButton","GO")
),
mainPanel(
wellPanel(
column(8, plotOutput("BoxplotDataset")
)
)
))
runApp(list(ui=ui,server=server))
Defined the reactiveValues outside of the observeEvent
Changed the reactiveValues inside of the observeEvent
Changed the validate and need.

change selectizeInput choices - wrong values in menu

I try to make a selection menu like this:
Interactively change the selectInput choices
And everything works well with the exception of one thing:
Instead to get the values (like McDonald), I get the indices although I did nothing different (see picture link below). Where could be my mistake?
Picture
Here my global.R:
partners<- read.csv("genes.csv", header=TRUE, fill=TRUE)
server.R
shinyServer(function(input, output) {
#subTable
searchResult<- reactive({
subset(partners, grepl(input$nameSearch, partners$name))
})
output$searchResults <- renderTable({
searchResult()[,1]
})
output$selectUI <- renderUI({
selectizeInput("partnerName", "Click in and select", choices=searchResult()[,1], multiple=TRUE )
})
})
ui.R
library(shiny)
shinyUI(pageWithSidebar(
# Give the page a title
titlePanel("Tilte"),
sidebarPanel(
textInput("nameSearch", "Search for name", "Blah"),
htmlOutput("selectUI"),
br(),
submitButton("Update View"),
br()
),
# Create a spot for the barplot
mainPanel(
textOutput("text"),
plotOutput("plot")
)
)
)
I think you are not getting indices, but rather the integer representation of a factor. Check the class of partners[,1]. Try
output$selectUI <- renderUI({
selectizeInput("partnerName", "Click in and select",
choices=as.character(searchResult()[,1]), multiple=TRUE )
})
You could possibly add the stringsAsFactors=FALSE option when you read the data as well.

multiple choice box in R/shiny - adding a scroll bar

I build an R/shiny web app. I want to have a multiple choice box (I use checkboxGroupInput(), but am open to alternatives). However, the list of choices is long and I want to contain it in a relatively small box of options (that shows 5-6 options at a time) with a scroll bar that enables to scroll through the entire list of choices.
Is there a way this can be done?
minimal example:
ui.R
library(shiny)
choices = paste("A",1:30,sep="_")
shinyUI(pageWithSidebar(
# Application title
headerPanel("my title"),
sidebarPanel(
checkboxGroupInput("inp", "choose any of the following", choices)
),
mainPanel(
tableOutput("result")
)
))
server.R
library(shiny)
shinyServer(function(input, output) {
myInput <- reactive({
input$inp
})
output$result <- renderTable({
x = myInput()
if(length(x)==0) {
x = "No Choice Made"
}
matrix(x,ncol=1)
})
})
I found that using selectInput(..., multiple = TRUE) does the trick.

RStudio Shiny Conditional Plot

Im trying to create a web application with the new RStudio feature Shiny, which plots different stocks. I created the following example.
I want to select the dataset StockMarket then select eg the DAX and then finally the plot should appear.
Right now if you run this code the plot appears immediately
Could you help me please?
ui.R:
library(shiny)
library(ggplot2)
shinyUI(pageWithSidebar(
# Application title
headerPanel("Plot1"),
sidebarPanel(
selectInput("dataset", "Dataset", list("mtcars"="cars", "StockMarket"="stocks")),
conditionalPanel(
condition = "input.dataset=='stocks'",
uiOutput("data")
)),
mainPanel(
plotOutput('plotstock')) ))
server.R:
library(shiny)
require(ggplot2)
library(datasets)
shinyServer(function(input, output) {
output$data<- reactiveUI(function() {
selectInput("data", "Choose Dataset", colnames(EuStockMarkets))
})
output$plotstock <- reactivePlot(function() {
data<-data.frame(EuStockMarkets)
p<- ggplot(data,aes(x=seq(1,length(data[,1])),y=DAX))+geom_line(size=1)+ylab("")+opts(title="Time Series")
print(p)
})})
In the reactivePlot function you can do something like
if (is.null(input$data))
return(NULL)
I would also add a blank entry to the dataset choices ("(Choose one)"="") and also have
if (!nzchar(input$dataset))
return(NULL)
in reactivePlot.
Also make sure that you check for empty strings
if (!nzchar(input$dataset) || input$dataset=='')
return(NULL)

Resources