I'm trying to create an app where the user is initially presented with a sidebar selectInput containing a list of choices: A, B, C and D – populated by file-system contents.
Depending on which choice the user selects, I want to populate the rest of the sidebar with choice-specific contents. For example, if the user chooses "A", then the sidebar will contain an additional selectInput and dateRangeInput.
To keep the code clean, I've kept all "A"-specific code in handle_A.R, etc. This means that eventually when I decide to add a new choice "E", I just need to put all the code in handle_E.R.
ui.R
pageWithSidebar(
headerPanel("Test"),
sidebarPanel(
selectizeInput("choice", "Choice:", c()),
uiOutput("sidebar")
),
mainPanel(uiOutput("main"))
)
server.R (relevant bits)
output$sidebar <- renderUI({
sidebarRenderer[[input$choice]](input, output, session)
})
I have code in server.R that sources all the handle_*.R scripts – each registering their callbacks.
So far, so good. I can select various choices and the relevant callbacks are called. However, I don't know how to implement the callbacks such that I can update the sidebar widgets as the user interacts with the sidebar. I want to essentially do the following (which will not work, because the function needs to return something to renderUI):
handle_A.R
sidebarRenderer[["A"]] <<- function(input, output, session) {
selectInput("day", "Day:", c("Mon", "Wed", "Fri"))
dates <- getDateList(input$day)
dateRangeInput("date", "Date:", start=dates[0], end=dates[length(dates)])
if (hasPublicHoliday(dates))
checkboxInput("ignoreHolidays", "Ignore public holidays")
}
So what I want is to automatically update dateRangeInput to the calculated start and end dates for the corresponding Mon/Wed/Fri. Furthermore, if any of the dates contain a public holiday, I want to display an extra checkbox to let the user ignore public holidays.
If anybody can help me out, I'd greatly appreciate it!
Turns out all I needed was to put the logic in observe and call updateCheckBoxInput.
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 have developed an rshiny app which displays a table and a plot as output for a given city, which comes as an input from user. The thing is now I am giving user the flexibility to enter more than one city as input.
So what i want to do now is to display the output in such a way so that the user has a select list which displays the output based on the value selected by the user.
For ex a user enter 3 city now 3 table and 3 plots have been computed in the observe event, I want the user to select the city based on which the output will be displayed.
Sample code would be something like;
observeEvent(input$get, {
some operations...
output$city_output<- renderDataTable(samp_data)
}
)
And I would need the ouput to be like
selectInput('city','Select the city to be displayed',samp_data$city)
Based on user selection the data table for that city should be displayed
What about something like this using the renderUI command?
library(shiny)
ui <- fluidPage(
# Application title
titlePanel("Test App"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
checkboxGroupInput("City",label = "Choose a city",
choices = c("London","Paris","Hamburg"),
selected="London")
),
# Show a plot of the generated distribution
mainPanel(
uiOutput("ReactiveUI"),
textOutput("Final")
)
)
)
server <- function(input, output) {
output$ReactiveUI <- renderUI(
selectInput("ReactiveUI",
label="Choose one of your choices",
choices = input$City,
selected="London"))
output$Final <- renderText({paste("you selected",input$ReactiveUI)})
}
# Run the application
shinyApp(ui = ui, server = server)
This works because by using the renderUI command we have access to the values stored in input$City as we are in the server.r file.
EDIT:
To elaborate on renderUI, this command pairs with uiOutput (it's ui.R counterpart, like renderText is to textOutput). The renderUI command moves the construction of a UI element from ui.R to server.R. This is useful because we can not do much "programming" in ui.R but we can in server.R. We also can't access anything stored in the input list while in ui.R. To put it simply, by creating our UI element in server.R, we allow the UI of our applications to respond to other user inputs.
In this example, we take advantage of the ability to see other inputs by setting the choices of our dropdown to the input selected by the checkboxGroupInput
I am stuck in a small problem related to shiny/R.
I am reading in a text file and displaying selective column names returned by grep search into the shiny application on the fly. For this, I am using the dynamicUI.
After the file is read in, the following function runs in server.R. It checks for specific colnames and displays this on the UI using uiOutput. Whatever column names are selected by the user, they are sent to another function for data processing and the plot it returned on the mainPanel.
server.R
output$Bait <- renderUI({
data <- input.data();
if(is.null(data)) return()
colnames <- names(data)
colnames = colnames[grep("*LFQ*",colnames,ignore.case=TRUE)]
# Creating the checkboxes using the above colnames
checkboxGroupInput("bait", "Choose Bait LFQ columns",
choices = colnames,
selected = colnames)
})
ui.R
shinyUI(
sidebarPanel(
uiOutput("Bait"),
),
mainPanel(
plotOutput(outputId = 'plot'),
)
)
Everything is fine, what I am trying to create is an action button for the checkboxes. Some files are big and have a longer list of column names >60, so whenever a checkbox is clicked, the whole function runs for processing and displays a plot. This gets unnecessary when the user has to deselect/select more than 10 columns.
An easy fix is, I kept selected=NULL but what I want is to add an actionButton after the checkboxGroupInput, so that user can select as many as checkBoxes but the function only runs when the GO button is pressed via the actionButton. If add a actionButton control after the checkbocGroupInput, it doesnt' works.
Can someone guide me in this regard. After working on this for sometime, now I am bit lost.
Thanks
Did you look into ?isolate? Lets say i want function initialFunction() only be evaluated if input$actionButtonis clicked.
observe({
input$actionButton # everything that triggers initialFunction() should come before isolate()
isolate({
# everything that should not trigger initialFunction() should come inside isolate()
initialFunction()
})
})
Note: After coming up with the answer I reworded the question to make if clearer.
Sometimes in a shiny app. I want to make use of a value selected by the user for a widget, as well as the previous value selected for that same widget. This could apply to reactive values derived from user input, where I want the old and the new value.
The problem is that if I try to save the value of a widget, then the variable containing that value has to be reactive or it will not update every time the widget changes. But, if I save the the value in a reactive context it will always give me the current value, not the previous one.
How can I save the previous value of a widget, but still have it update every time the user changes the widget?
Is there a way that does not require the use of an actionButton every time the user changes things? Avoiding an actionButton can be desirable with adding one is otherwise unnecessary and creates excess clicking for the user.
Seeing as the session flush event method seems to be broken for this purpose, here is an alternative way to do it using an observeEvent construct and a reactive variable.
library(shiny)
ui <- fluidPage(
h1("Memory"),
sidebarLayout(
sidebarPanel(
numericInput("val", "Next Value", 10)
),
mainPanel(
verbatimTextOutput("curval"),
verbatimTextOutput("lstval")
)
)
)
server <- function(input,output,session) {
rv <- reactiveValues(lstval=0,curval=0)
observeEvent(input$val, {rv$lstval <- rv$curval; rv$curval <- input$val})
curre <- reactive({req(input$val); input$val; rv$curval})
lstre <- reactive({req(input$val); input$val; rv$lstval})
output$curval <- renderPrint({sprintf("cur:%d",curre())})
output$lstval <- renderPrint({sprintf("lst:%d",lstre())})
}
options(shiny.reactlog = TRUE)
shinyApp(ui, server)
Yielding:
Update This answer was posted before the advent of the reactiveValues/observeEvent model in shiny. I think that #MikeWise 's answer is the better way to do this.
After some playing around this is what I came up with. The ui.r is nothing special
ui.r
library(shiny)
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
selectizeInput(inputId="XX", label="Choose a letter",choices=letters[1:5])
),
mainPanel(
textOutput("Current"),
textOutput("old")
)
)
))
"Current" will display the current selection and "old" displays the previous selection.
In the server.r I made use of three key functions: reactiveValues, isolate and session$onFlush.
server.r
library(shiny)
server <- function(input, output,session) {
Values<-reactiveValues(old="Start")
session$onFlush(once=FALSE, function(){
isolate({ Values$old<-input$XX })
})
output$Current <- renderText({paste("Current:",input$XX)})
output$old <- renderText({ paste("Old:",Values$old) })
}
The server.r works like this.
First, Values$old is created using the reactiveValues function. I gave it the value "Start" to make it clear what was happening on load up.
Then I added a session$onFlush function. Note that I have session as an argument in my server function. This will run every time that shiny flushes the reactive system - such as when the selectizeInput is changed by the user. What is important is that it will run before input$XX gets a new value - so the value has changed at the selectizeInput but not at XX.
Inside the session$onFlush I then assign the outgoing value of XX to Values$old. This is done inside an isolate() as this will prevent any problems with input$XX gets updated with the new values. I can then use input$XX and Values$old in the renderText() functions.
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')