using conditionalPanel with values from checkboxGroupInput - r

I want to use checkboxGroupInput and then, if a certain box is checked, i want a conditionalPanel. A toy example is here:
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
checkboxGroupInput("items","Which Item?",
choices=c("A","B","C","D")),
conditionalPanel( condition = "input.items == 'D'",
numericInput("n","n",value=50,min=0,max=100,step=1)
)
),
mainPanel(
uiOutput("text")
)
)
))
now this works fine if only box 'D' is selected, but not if (as would be normal in my problem) several boxes are selected.
in server.R something like
if("D" %in% input$which)
works fine but that does not seem to work in ui.R. I also tried subsetting ala R, for example
conditionalPanel( condition = "input.items[4] == 'D'",
but that does not work either.
Wolfgang

docendo gave the correct answer: the syntax is
conditionalPanel(condition = "input.items.includes('D')"
Thanks!

somehow it no longer works with shiny v1.1.0. I am using R version 3.5.1 with platform windows. Rather than the includes() solution, the below works well for me (just in case others encounter the same issue):
conditionalPanel(condition = "input.items.indexOf('D') > -1", ...)

Related

How can I put multiple plots side-by-side in a tab panel with other outputs present, shiny r?

This question is a follow-up to How can put multiple plots side-by-side in shiny r? and R Shiny layout - side by side chart and/or table within a tab.
Is it possible to embed two side-by-side plots in a tab panel when there are other outputs present besides the two plots I want to embed side-by-side? I have tried every suggestion offered in the answers to the previous two questions and nothing works. No matter what I try I end up breaking my program so it refuses to display any out put at all.
mainPanel(
width = 10,
tabsetPanel(
tabPanel("Dashboard", textOutput("text30"), textOutput("text31"), textOutput("text28"), textOutput("text29"),
column(6, plotOutput("plot1st"), column(6, plotOutput("plot2nd")), #what do I put here to make this work?
plotOutput("plot3rd"), plotOutput("plot9th"), plotOutput("plot4th"), plotOutput("plot8th"), plotOutput("plot7th"), plotOutput("plot6th")),
tabPanel("Graph Switcher", plotOutput("selected_graph"))
))))
Is there anything I can do to make this work?
Update: Thanks to Florian's answer I was able to make the app look the way I wanted. However, as can be seen in the screenshot below, a small number 12 appears in between the plots, and the plots are not perfectly horizontally aligned. I'm wondering if it's possible to fix this?
You can put multiple column elements in one fluidRow:
Hope this helps!
library(shiny)
ui <- fluidPage(
tabsetPanel(
tabPanel("Dashboard",
fluidRow(12,
column(6,plotOutput('plot1')),
column(6,plotOutput('plot2'))
),
fluidRow(12,
column(4,plotOutput('plot3')),
column(4,plotOutput('plot4')),
column(4,plotOutput('plot5'))
),
fluidRow(12,
column(3,plotOutput('plot6')),
column(3,plotOutput('plot7')),
column(3,plotOutput('plot8')),
column(3,plotOutput('plot9'))
)
)))
server <- function(input,output)
{
lapply(seq(10),function(x)
{
output[[paste0('plot',x)]] = renderPlot({hist(runif(1:20))})
})
}
shinyApp(ui,server)

Connecting values of sliderInput in Shiny

I've been stuck with this simple problem:
Shiny app uses several sliderInputs that are placed in different tabs in standard fluidPage. When one slider is moved, other sliders should move accordingly.
I've been working with updateSliderInput() function placed in observe()
Are there any other functions or procedures that can change values of selected sliders? I've also been experiencing that when I swich to other tab, the initial value of slider is set as in the beggining even though it should have been changed by updateSliderInput().
I'd be really thankful if someone could reference me to some function or algorithm that could help me.
Bonus points: I also need to do this for selectInput and radioButtons
I have a similar problem and followed an example from
R shiny bi-directional reactive widgets
It works for me. Please see the following:
R code
ui <- fluidPage(
titlePanel(""),
fluidRow(
column(3,
wellPanel(
uiOutput('slider1'),
uiOutput('text1')
))
))
server <- function(input, output) {
output$slider1<-renderUI({
text1.value<-input$slider1
default.slider1<-if(is.null(text1.value)) 15 else text1.value
textInput("text1", label="slider",
value = default.slider1)
})
output$text1<-renderUI({
slider1.value<-input$text1
default.text1<-if(is.null(slider1.value)) 15 else slider1.value
sliderInput("slider1", label = h5("text"),
min = -10, max = 10, value = default.text1,step = 1)
})}
Just realized this question is posted a long time ago. Still hope it helps!

gvisTables not rendering in Shiny apps

The actual issue I'm trying to solve: I'm creating a dashboard which will include data tables. I would like for numbers to be formatted with commas as thousands separators, but there is (apparently) an issue with the DT package when it's used with Shiny, in that the comma-separated formatting causes DT::renderDataTable to read in numbers as character, which affects how the numbers are sorted. (DT's number formatting functionality does not work with Shiny, it appears.)
Where I'm at so far: The only solution I've been able to find is to use googleVis instead of DT to create the tables. Now I'm running into a different issue (described below), but what I really care about is having data tables with comma-separated numbers that sort like numbers.
The GoogleVis issue: When I use gvisTable outside of Shiny apps, they render perfectly fine, but they do not render at all when using renderGvis and htmlOutput in Shiny. As an example, I'll borrow Example 4 from here.
Not using Shiny, my code looks like this:
library(datasets)
library(googleVis)
myOptions <- list(page='enable', pageSize=10, width=550)
Table <- gvisTable(Population,options=myOptions)
plot(Table)
Using Shiny, it's like this:
library(datasets)
library(googleVis)
library(shiny)
shinyApp(
ui = pageWithSidebar(
headerPanel("Example 4: pageable table"),
sidebarPanel(
checkboxInput(inputId = "pageable", label = "Pageable"),
conditionalPanel("input.pageable==true",
numericInput(inputId = "pagesize",
label = "Countries per page",10))
),
mainPanel(
htmlOutput("myTable")
)
),
server = function(input,output){
myOptions <- reactive({
list(
page=ifelse(input$pageable==TRUE,'enable','disable'),
pageSize=input$pagesize,
width=550
)
})
output$myTable <- renderGvis({
gvisTable(Population,options=myOptions())
})
}
)
Any help is much appreciated!
I solved my own problem. It turns out that RStudio's native browser has difficulty displaying googleVis exhibits through Shiny. All I needed to do was open it up in Firefox... I don't think I've ever felt so much "woot" and "ugh" at the same time before.

reactive tabPanel in a navbarMenu in Shiny

I am a beginner in this forum. I have a question on R shiny. I looked at some discussions about it in this forum but answers (R Shiny - add tabPanel to tabsetPanel dynamically (with the use of renderUI)) don't fit exactly what i am looking for.
I try to add some tabPalnels in navbarMenu with interaction:
example I choose a directory and I list how many files are in and what are their names.
then I would like to make tabPanel in navbarMenu which have the name of the files in the directory
ex :if dir = c:/charlotte/RY
list.files(dir) = c(fichier1.csv, fichier2.csv)
I would like that the names of 2 tabpanels from the navbarMenu are called fichier1.csv and fichier2.csv.
I try it but without results.
I give you my code :
shinyUI(navbarPage("raster analysis",
tabPanel("Analysis parameters",
actionButton("goButton","load session to analyze"),
textOutput("session")
),
navbarMenu("NISTs",
tabPanel("Set up"
),
tabPanel("About"
)
),
navbarMenu("Samples",
tabPanel("Set up"
),
tabPanel("About"
)
))
shinyServer(function(input, output) {
observe({
if(input$goButton > 0){
output$session = renderUI({
list.files(choose.dir(),pattern="\\.csv$")
}) } })})
Do you have an idea on this topic ?
Is it possible ?
Thank you very much !!
C
After desperately looking for an answer, I wrote to Joe CHen, who made the example of the navbarMenu (http://shiny.rstudio.com/gallery/navbar-example.html), and I asked him if it was possible to do what I explain in my message. He told me that it is not possible at this moment (05/2014).
Stop trying !! It is impossible :)
See U,
Charlotte

shiny use number of select elments in conditionalPanel

I would like to show content of my shiny app depending on the number of selected items of a multiselect input. So far I couldn't figure out what the condition should look like to make this work.
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("select and conditional panel"),
sidebarPanel(
selectInput(inputId = "someSelect", multiple=TRUE, label = "Genes:", choices = colnames(someDataFrame), selected = c("ESR1", "CD44")),
),
mainPanel(
conditionalPanel(
condition="length(input.someSelect.selected) > 2",
tabsetPanel(
...
)
)
)
))
It is probably a matter of taste, but I don't like the conditionalPanel construct, because it enters a javascript logic into an R code. Instead I prefer the uiOutput (and the respective renderUI), that can generate dynamic UI. while the conditionalPanel can handle only rather simple conditions, the dynamic UI approach can create conditional appearance that are based on more complex logic and can take advantage of the power of R.
it is, however, slightly slower to respond to changes.
if you use this approach your ui.r should look something like:
mainPanel(uiOutput("myConditionalPanel"))
and your server.r would look something like:
output$myConditionalPanel = renderUI({
if(length(input$someSelect)>2) {
## some ui definitions here. for example
tabsetPanel(
...
)
} else {
## some alternative definitions here...
}
})
You can't use a R function into the condition of the conditional panel. I think your condition should be : input.someSelect.length > 2
Hopefully this helps someone else having the same issue I had...
Using the above answers I was having issues when trying to make a conditionalPanel only appear when 2 or more items were selected in a selectInput. The uiOutput/renderUi method was buggy and with the condition input.someSelect.length > 1 the conditionalPanel appeared even when nothing was selected in the selectInput.
With the conditionPanel, I needed to include a condition to check whether the selectInput was defined:
"input.someSelect != undefined && input.someSelect.length > 1"

Resources