r shiny: access input fields in UI - r

I'm trying to access an input field in mainPanel from the sidebarPanel, but I couldn't succeed.
Code:
shinyUI(pageWithSidebar{
sidebarPanel(
sliderInput("x", "X", min = 10, max = 100, value = 50)
),
mainPanel(
#this is where I wanna use the input from the sliderInput
#I tried input.x, input$x, paste(input.x)
)
})
Where seems to be the problem? Or isn't possible to use the input from the sidebarPanel in the mainPanel?

You can only use the inputs in the server side.
For example :
library(shiny)
runApp(list(
ui = pageWithSidebar(
headerPanel("test"),
sidebarPanel(
sliderInput("x", "X", min = 10, max = 100, value = 50)
),
mainPanel(
verbatimTextOutput("value")
)
),
server = function(input, output, session) {
output$value <- renderPrint({
input$x
})
}
))
EDIT ::
Dynamically set the dimensions of the plot.
Use renderUi to render a plot output using the values of your inputs.
library(shiny)
runApp(list(
ui = pageWithSidebar(
headerPanel("Test"),
sidebarPanel(
sliderInput("width", "Plot Width (%)", min = 0, max = 100, value = 100),
sliderInput("height", "Plot Height (px)", min = 0, max = 400, value = 400)
),
mainPanel(
uiOutput("ui")
)
),
server = function(input, output, session) {
output$ui <- renderUI({
plotOutput("plot", width = paste0(input$width, "%"), height = paste0(input$height, "px"))
})
output$plot <- renderPlot({
plot(1:10)
})
}
))

Related

R Shiny: Using numeric output variable as the initial value of slider

My shiny app looks like this:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
fluidRow(
box(plotOutput("plot1", height = 250)),
box(textInput("my_text", # supposed to be a numeric input
"Text input:"),
title = "Controls",
sliderInput("slider",
"Number of observations:",
min = 1, max = 100,
value = 50 # Want it to be output$my_init
)
)
)
)
)
server <- function(input, output) {
set.seed(122)
histdata <- rnorm(500)
output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
# My attempt
# output$my_init <- input$my_text + 28
}
shinyApp(ui, server)
I want to set the initial value of the slider as output$my_init, a numeric variable that will be the result of operating some input variables. I tried using renderPrint, but the output is not numeric.
Thanks in advance.
I piggybacked on #stefan's comments and came up with this answer:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
fluidRow(
box(plotOutput("plot1", height = 250)),
box(numericInput("my_num", "Numeric input:", value = 50),
title = "Controls",
sliderInput("slider",
"Number of observations:",
min = 1, max = 100,
value = 50 # Want it to be output$my_init
)
)
)
)
)
server <- function(input, output) {
set.seed(122)
histdata <- rnorm(500)
output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
# My attempt
observeEvent(input$my_num, {
updateSliderInput(inputId = "slider", value = input$my_num)
})
}
shinyApp(ui, server)
The idea is to use observeEvent() to trigger updateSliderInput() (update slider input widget) and update the value parameter of sliderInput()

Is there a way to make a wrapper function for renderUI in Shiny (R)?

I need to use renderUI to create multiple input options based on another input value. I want to wrap everything inside renderUI as a function so I can apply this to many similar inputs. Here is a simplified example (which is working for me, but I don't want to repeat the renderUI part many times, because I have many other inputs like the i1):
library(shiny)
ui <- fluidPage(
fluidRow(
selectInput(
inputId = 'i1',
label = 'choice 1',
choices = list(5, 10)
),
uiOutput('o1')
)
)
server <- function(input, output, session) {
output$o1 <- renderUI(
fluidRow(
sliderInput(
inputId = 's1',
label = 'slider 1',
min = 0, max = as.numeric(input$i1) * 10,
value = 0.5
),
sliderInput(
inputId = 's2',
label = 'slider 2',
min = 0, max = as.numeric(input$i1) * 100,
value = 0.5
)
)
)
}
shinyApp(ui = ui, server = server)
The problem is that when I tried to wrap it into a function, the output created by renderUI stops to update when I change the input value. Here is the code that doesn't work for me:
library(shiny)
renderUI_warpper <- function(i){
renderUI(
fluidRow(
sliderInput(
inputId = 's1',
label = 'slider 1',
min = 0, max = as.numeric(i) * 10,
value = 0.5
),
sliderInput(
inputId = 's2',
label = 'slider 2',
min = 0, max = as.numeric(i) * 100,
value = 0.5
)
)
)
}
ui <- fluidPage(
fluidRow(
selectInput(
inputId = 'i1',
label = 'choice 1',
choices = list(5, 10)
),
uiOutput('o1')
)
)
server <- function(input, output, session) {
output$o1 <- renderUI_warpper(input$i1)
}
shinyApp(ui = ui, server = server)
Though I don't see the point to do this because even you move that part to a function, you still have to define each sliderInput, here is one way to do it.
The point is you should wrap the Input instead of renderUI, because you need reactive expressions to be able to update input and reactive only works within another reactive or render* functions
library(shiny)
ui <- fluidPage(
fluidRow(
selectInput(
inputId = 'i1',
label = 'choice 1',
choices = list(5, 10)
),
uiOutput('o1')
)
)
server <- function(input, output, session) {
wrapper <- reactive({
function(i){
fluidRow(
sliderInput(
inputId = 's1',
label = 'slider 1',
min = 0, max = as.numeric(i) * 10,
value = 0.5
),
sliderInput(
inputId = 's2',
label = 'slider 2',
min = 0, max = as.numeric(i) * 100,
value = 0.5
)
)
}
})
output$o1 <- renderUI(wrapper()(input$i1))
}
shinyApp(ui = ui, server = server)
Here is a possible alternative:
library(shiny)
create_sliders <- function(i) {
fluidRow(
column(
width = 12,
sliderInput(
inputId = "s1",
label = "slider 1",
min = 0, max = as.numeric(i) * 10,
value = 0.5
),
sliderInput(
inputId = "s2",
label = "slider 2",
min = 0, max = as.numeric(i) * 100,
value = 0.5
)
)
)
}
ui <- fluidPage(
fluidRow(
selectInput(
inputId = "i1",
label = "choice 1",
choices = list(5, 10)
),
uiOutput("o1")
)
)
server <- function(input, output, session) {
output$o1 <- renderUI({
create_sliders(input$i1)
})
}
shinyApp(ui = ui, server = server)

R shiny plots based on radio buttons and slider input

Based on this post:
create plots based on radio button selection R Shiny
I want a different plot output depending on which radio option the user selects and adjust the numbers of committees using the slider input.
The slider input doesn't work and I don't realize how to solve the problem.
Many thanks for the help!
Here is my code:
library(shiny)
library(Cubist)
plotType <- function(x, type, committe) {
switch(type,
Cond = dotplot(finalModel, what = "splits"),
Coeff = dotplot(finalModel, what = "coefs"))
}
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons(inputId = "ptype", label = "Select the plot", choices = c("Cond", "Coeff")),
sliderInput(inputId = "commit", min=1, max = 25, value = 2)
),
mainPanel(
plotOutput("plots"))
)))
server <- shinyServer(function(input, output) {
output$plots <-renderPlot({
plotType(finalModel, input$ptype, input$commit)
})
})
shinyApp(ui = ui, server = server)
Make sure to add lable to your sliderinput too:
library(shiny)
plotType <- function(x, type, committe) {
switch(type,Cond = dotplot(finalModel, what = "splits"),Coeff = dotplot(finalModel, what = "coefs"))
}
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons(inputId = "ptype", label = "Select the plot", choices = c("Cond", "Coeff")),
sliderInput(inputId = "commit","", min=1, max = 25, value = 2)
),
mainPanel(
plotOutput("plots"))
)))
server <- shinyServer(function(input, output) {
output$plots <- renderPlot({
plotType(finalModel, input$ptype, input$commit)
})
})
shinyApp(ui = ui, server = server)
There is no need for comma at the end of this line:
sliderInput(inputId = "commit", min=1, max = 25, value = 2),
should be:
sliderInput(inputId = "commit", min=1, max = 25, value = 2)

Hiding or showing shiny elements without pressing submit

I have a shiny app where i want to hide or show some elements based on user input. This i tried to do by using conditionalPanel in shiny. However, it works only after pressing the submit button. I want to hide or show the textInput element without pressing the submit button. Below is an example what I tried.
UI.R
library(shiny)
shinyUI(fluidPage(
titlePanel("submitButton example"),
fluidRow(
column(3, wellPanel(
sliderInput("n", "N:", min = 10, max = 1000, value = 200,
step = 10),
checkboxInput("checkbox", label = "Message", value = FALSE),
conditionalPanel(
condition = "input.checkbox == true",
textInput("text", "Text:", "text here")),
submitButton("Submit")
)),
column(6,
plotOutput("plot1", width = 400, height = 300),
verbatimTextOutput("text")
)
)
))
Server.R
shinyServer(function(input, output) {
output$plot1 <- renderPlot({
hist(rnorm(input$n))
})
output$text <- renderText({
paste("Input text is:", input$text)
})
})
I want to show the textInput as soon as user checks the checkbox and hide it on uncheck without any dependency on submit button.
You can try
UI:
library(shiny)
shinyUI(fluidPage(
titlePanel("submitButton example"),
fluidRow(
column(3, wellPanel(
sliderInput("n", "N:", min = 10, max = 1000, value = 200,
step = 10),
checkboxInput("checkbox_1", label = "Message", value = FALSE),
uiOutput('test')
,actionButton("Submit",label ="Submit" )
)),
column(6,
plotOutput("plot1", width = 400, height = 300),
verbatimTextOutput("text")
)
)
))
server:
shinyServer(function(input, output,session) {
output$test=renderUI({
if(input$checkbox_1==T){
list(textInput("text", "Text:", "text here"),
numericInput("num","num",0), numericInput("num1","num1",0))}
})
observeEvent(input$Submit,{
output$plot1 <- renderPlot({
hist(rnorm(isolate(input$n)))
})
output$text <- renderText({
paste("Input text is:", isolate(input$text))
})
})
})

Change size of image with a slider in Shiny

Goal: Make an image change size in response to moving a slider in Shiny (RStudio). Think zoom-in-zoom-out effect.
Problem: There's an error saying "Error in basename(imageinfo$src) : a character vector argument expected". I can't find anything that directly answers the question and I'm not sure what else to try. Is it just a problem with how sliderInput is used as input$slider in server.R?
My current progress: My rational was to set up the slider in the ui.R file and then have the width of the image be the input in the server.R file.
The ui.R part:
shinyUI(fluidPage(
titlePanel("Nancy's Brainstorming"),
sidebarLayout(
sidebarPanel(
h3(
strong("What is this?", style = "font-si24pt")),
p("This is a pilot project."),
sliderInput("slider",
label = "",
min = 100,
max = 300,
value = 200),
imageOutput("logo", width = 200)
)
)
))
The server.R part:
shinyServer(function(input, output) {
output$logo = renderImage({
img(src = "mylogo.png", width = input$slider)
})
})
Additional information: The image shows up just fine by itself when I use img(src = "mylogo.png", width = 200). Also, I'm doing this just to get a better feel for building Shiny apps.
img(src = "mylogo.png", width = input$slider) is just returning html. You can use renderUI instead of renderImage.
library(shiny)
runApp(
list(ui = fluidPage(
titlePanel("Nancy's Brainstorming"),
sidebarLayout(
sidebarPanel(
h3(
strong("What is this?", style = "font-si24pt")),
p("This is a pilot project."),
sliderInput("slider", label = "", min = 100, max = 300, value = 200),
uiOutput('logo')
),
mainPanel(
plotOutput("distPlot")
)
)
),
server = function(input, output, session) {
output$logo <- renderUI({
img(src = "http://i.stack.imgur.com/mTqXa.png", width = as.integer(input$slider))
})
}
)
)

Resources