Shiny Applications Layouts Exercises (Part-9)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Shiny Application Layouts – Update Input
In the ninth part of our series we will see the updated input scenario. This is different from the Dynamic UI example we met in part 8, where the UI component is generated on the server and sent to the UI, where it replaces an existing UI component.
This part can be useful for you in two ways.
First of all, you can see different ways to enhance the appearance and the utility of your shiny app.
Secondly you can make a revision on what you learnt in “Building Shiny App” series as we will build basic shiny staff in order to present it in the proper way.
Read the examples below to understand the logic of what we are going to do and then test yous skills with the exercise set we prepared for you. Lets begin!
Answers to the exercises are available here.
UI Context
As always let us create the skeleton of the app.
#ui.r
fluidPage(
titlePanel("Title"),
fluidRow(
column(3,
wellPanel()),
column(3,
wellPanel()),
column(3,
wellPanel())
)
)
#server.R
function(input, output, clientData, session) {}
Exercise 1
Build the initial UI of your app. Give it a title, use 4 columns and one row for each one of the three well Panel that we are going to use.
Applying reactivity
To create a reactive context for our app we will use the observe({})
function in our server side.
Exercise 2
Create reactive context. HINT: Use observe({})
.
“Master” Inputs.
Now we are going to create the two inputs that control the rest of the inputs of the app. Look at the example:
#ui.r
textInput("text_input",
"labels:",
"Some Text"),
sliderInput("slider_input",
"values:",
min = 1, max = 100, value = 50)
#server.r
t_input <- input$text_input
s_input <- input$slider_input
Exercise 3
Create a text Input in the ui side that controls labels and then pass it to a new variable on the server side.
Exercise 4
Create a slider Input in the ui side that controls values and then pass it to a new variable on the server side.
Dependent Inputs
Firstly let’s create a text input that changes both the label and the text.
#ui.r
textInput("Text", "Text input:", value = "text")
#server.r
updateTextInput(session, "Text",
label = paste("Sth", t_input),
value = paste("Sth", s_input)
)
Exercise 5
Create a text input, in the second well Panel,that changes its label and its value according to the two “Master” Inputs you created before. HINT: Use updateTextInput()
.
Exercise 6
Create a slider input,in the second well Panel,that changes its label and its value according to the two “Master” Inputs you created before. HINT: Use updateSliderInput()
.
Exercise 7
Create a numeric input,in the second well Panel,that changes its label and its value according to the two “Master” Inputs you created before. HINT: Use updateNumericInput()
.
Exercise 8
Create a date input,in the second well Panel,that changes its label and its value according to the two “Master” Inputs you created before. HINT: Use updateDateInput()
.
In order to create a Checkbox group with the same conditions as the rest of the inputs we just created we should first create a list of options like in the example below:
#server.r
options <- list()
options[[paste("option", s_input, "A")]] <-
paste0("option-", s_input, "-A")
options[[paste("option", s_input, "B")]] <-
paste0("option-", s_input, "-B")
Exercise 9
Create a list with three choices for your Checkbox Group. HINT: Use list()
.
Exercise 10
Create a checkboxgroup input, in the third well Panel,that changes its label and its value according to the two “Master” Inputs you created before. HINT: Use updateCheckboxGroupInput()
.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.