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
Related
I have built shiny App before, but I am getting rusty after not writing the code for a while.
Here is my goal:
I am designing a dynamic UI using shinydashboard. I have one selectInput (e.g., A, B, C and D).
What I want to make the app perform is that, based on the user option (either A, B, C or D), it will dynamically pop up another input panel (a panel with specific collection of "numericInput"s or "selectInput"s), as each A/B/C/D option defines different situation.
I could not provide my own code (as I do not have any idea on even how to get started), but I find some page with example code here:
An example of dynamic UI
It is the example in section 10.2.1 of the page using tabsetPanel and updateTabsetPanel.
I checked their app link and it seems to be exactly what I want, but I suspect that some of the code has been depleted by Shiny already as when I copy and paste the code to my R script, I could not run it without error.
If you could kindly help, either on how to properly adjust the code on that page, or if there is any other approach, it will be greatly appreciated.
Easiest way would be using uiOutput and renderUI.
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("controller", "Show", choices = c("A", "B", "C", "D")),
uiOutput("newSelect")
),
mainPanel(
textOutput("textId")
)
)
)
server <- function(input, output, session) {
output$textId <- renderText({
input$controller
})
output$newSelect <- renderUI({
if(input$controller == "B"){
selectInput("controller2", "New Select", choices = c(1:4))
} else {
NULL
}
})
}
shinyApp(ui,server)
Bare in mind this can make your app laggish if it gets too complex. If so, you can use some js to show or hide the other selectInputs conditionally
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!
I am trying to display from 1 to 5 tabPanels in a navbarPage in Shiny.
I have 5 plots my code generates, but I'd like a user to be able to select how many they want to have access to -- to be displayed one plot in each tabPanel, naturally.
I've got an external configuration file (config.txt) that via source('config.txt'), I have access to a number_of_pages variable.
For example, number_of_tabPages <- 3
How would I set this up in UI.R?
The number of tabPanels can't be hardcoded at all in the UI file, since it depends on a value that is specified by a user, not using a control.
I've searched around and found that most of the approaches to this kind of thing
involve using uiOutput and renderUI functions, such as this similar problem, but I don't want any special control in the UI to do any selecting.
This is where things get tricky, when we are building the UI depending on values that may change. My brain is trying to wrap itself around the best method for doing this type of thing -- I feel like it isn't exactly in line with how Shiny wants to communicate with itself using a UI <--> server environment.
Any advice is greatly appreciated.
My UI.R is easy to create when it isn't dynamic:
fluidRow(
column(12,
"",
navbarPage("",tabPanel("First Tab",
plotOutput("plot1")),
tabPanel("Second Tab",
plotOutput("plot2")),
tabPanel("Third Tab",
plotOutput("plot3")),
tabPanel("Fourth Tab",
plotOutput("plot4")),
tabPanel("Fifth Tab",
plotOutput("plot5"))
)
)
)
)
Thanks!
If you don't need the user to change the number of tabPanel interactively, but just load varying numbers of them when the app is started you can use the do.call function in the navBarPage:
library(dplyr)
library(shiny)
library(ggvis)
#number of tabs needed
number_of_tabPages <- 10
#make a list of all the arguments you want to pass to the navbarPage function
tabs<-list()
#first element will be the title, empty in your example
tabs[[1]]=""
#add all the tabPanels to the list
for (i in 2:(number_of_tabPages+1)){
tabs[[i]]=tabPanel(paste0("Tab",i-1),plotOutput(paste0("plot",i-1)))
}
#do.call will call the navbarPage function with the arguments in the tabs list
shinyUI(fluidRow(
column(12,
"",
do.call(navbarPage,tabs)
)
)
)
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"
I am new to Shiny and trying to build a more accessible input and output for a function I built. I am giving this to people that don't run R so trying to build something that runs my functions in the background and then spits out the answer.
I am having some trouble getting everything in the way that I want it unfortunately and dealing with a bunch of errors. However, here is my more pointed question:
The actual function that I want to run takes a name (in quotations as "Last,First") and a number.
PredH("Last,First",650)
So I want a shiny application that takes a name input and a number input that then runs this program and then spits back out a data table with my answer. So a couple of questions.
How do I get it in the right form to input into my equation on the server side script, do I need to return it in the function so it can be accessed using a function$table type access? (Right now I am just printing using cat() function in the console for the function but know that may not be usable for this type of application.
I want to return a dataframe that can be gotten at PredH14$table. How do I go about building that shiny?
Here is my code so far:
UI:
library(shiny)
shinyUI(pageWithSidebar(
# Application title
headerPanel("Miles Per Gallon"),
# Sidebar with controls to select the variable to plot against mpg
# and to specify whether outliers should be included
sidebarPanel(
textInput("playername", "Player Name (Last,First):", "Patch,Trevor"),
radioButtons("type", "Type:",
list("Pitcher" = "P",
"Hitter" = "H"
)),
numericInput("PAIP", "PA/IP:", 550),
submitButton("Run Comparables")
),
mainPanel(
textOutput("name")
)
Server:
library(shiny)
shinyServer(function(input, output) {
sliderValues <- reactive({
data.frame(
Name = c("name", "PA"),
Value = c(as.character(playername),
PAIP),
stringsAsFactors=FALSE)
})
name=input[1,2]
PAIP=input[2,2]
testing <- function(name,PAIP){
a=paste(name,PAIP)
return(a) }
output$name=renderText(testing$a)
})
I am not quite sure I understood your question 100% but I clearly see you are wondering how to pass the input of the UI into the server and maybe, the other way back.
In your server code, clearly you are not getting any input from the UI. Basically you have created three input variables in your ui.R:
1. input$playername
2. input$type
3. input$PAIP
And one output:
1. output$name
Just let you know, the function sliderValues <- reactive(..) is called every time there is any input from the input... like people click the dropdown list or people modifies words in the text box.
You can even get started without the submit button just to get started. But the existence of the submit button actually makes everything easier. Create a submit button for an input form. Forms that include a submit button do not automatically update their outputs when inputs change, rather they wait until the user explicitly clicks the submit button.
So you can put your code in a way similar like this:
# server.R
library(shiny)
shinyServer(function(input, output) {
sliderValues <- reactive({
result <- ... input$playername ... input$type ... input$PAIP
return(result)
})
output$name <- renderPlot/renderText (... sliderValues...)
})
# ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Miles Per Gallon"),
sidebarPanel(
textInput("playername" ... ),
radioButtons("type" ... ),
numericInput("PAIP" ... ),
submitButton("...")
),
mainPanel(
textOutput/plotOutput...("name")
)
))
In the end, check out the shiny example that might be what you want.
library(shiny)
runExample('07_widgets')