I wanna have a plot with dynamic size and all should happen in the shinyUI.
Here is my code:
shinyUI{
sidebarPanel(
sliderInput("width", "Plot Width", min = 10, max = 20, value = 15),
sliderInput("height", "Plot Height", min = 10, max = 20, value = 15)
)
mainPanel(
plotOutput("plot", width="15cm", height="15cm")
)
}
I set "15cm" only to see the plot.
I tried different methods to take the data from the sliderInputs and bring it to the plotOutput. I tried "input.height", "input$heigt" but nothing worked.
You must use the inputs in the server side, for example here is one solution :
And the unit of the width and height must be a valid CSS unit, i'm not sure that "cm" is valid, use "%" or "px" (or an int, it will be coerced to a string with "px" at the end)
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("plot.ui")
)
),
server = function(input, output, session) {
output$plot.ui <- renderUI({
plotOutput("plot", width = paste0(input$width, "%"), height = input$height)
})
output$plot <- renderPlot({
plot(1:10)
})
}
))
Related
I wanna have a plot with dynamic size and all should happen in the shinyUI.
Here is my code:
shinyUI{
sidebarPanel(
sliderInput("width", "Plot Width", min = 10, max = 20, value = 15),
sliderInput("height", "Plot Height", min = 10, max = 20, value = 15)
)
mainPanel(
plotOutput("plot", width="15cm", height="15cm")
)
}
I set "15cm" only to see the plot.
I tried different methods to take the data from the sliderInputs and bring it to the plotOutput. I tried "input.height", "input$heigt" but nothing worked.
You must use the inputs in the server side, for example here is one solution :
And the unit of the width and height must be a valid CSS unit, i'm not sure that "cm" is valid, use "%" or "px" (or an int, it will be coerced to a string with "px" at the end)
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("plot.ui")
)
),
server = function(input, output, session) {
output$plot.ui <- renderUI({
plotOutput("plot", width = paste0(input$width, "%"), height = input$height)
})
output$plot <- renderPlot({
plot(1:10)
})
}
))
I created a UI page with some buttons and have two problems with the checkboxInput buttons:
checkboxInput('comissions', label = "Comissions", width = "10%")
Changing the width = "X%" doesn't change anything, same for 'Xpx'.
I suspected this is due to fixed column width, but the X% change works for other buttons well.
Second issue is that the button looks like this:
I would like it to be centered and not be in left column.
Thank you for the help,
Here is a way to center the checkbox, but it requires width = "100%".
library(shiny)
ui <- basicPage(
fluidRow(
column(4,
sliderInput("costs", "Costs", min = 1, max = 10, value = 1)),
column(4, style = "text-align: center;",
checkboxInput("comissions", label = "Comissions", width = "100%")),
column(4,
sliderInput("periods", "Number of periods", min = 1, max = 10, value = 1))
)
)
server <- function(input, output, session) {}
shinyApp(ui, server)
I don't know what you expect to see by changing the width ?
EDIT
To control the white space around the checkbox input, and its vertical alignment:
library(shiny)
ui <- basicPage(
fluidRow(
column(12,
div(style = "display: inline-block;",
sliderInput("costs", "Costs", min = 1, max = 10, value = 1)
),
div(style = "display: inline-block; margin-left: 20px; margin-right: 20px; vertical-align: -20px;",
checkboxInput("comissions", label = "Comissions", width = "100%")
),
div(style = "display: inline-block;",
sliderInput("periods", "Number of periods", min = 1, max = 10, value = 1)
)
)
)
)
server <- function(input, output, session) {}
shinyApp(ui, server)
I've come across some open issues when looking for a way to make a vertical slider in R Shiny apps, to put next to one of my plots so that the user can "move a horizontal line" in the plot with a slider that follows the same range as the plot's y axis.
I managed to make the slider turn vertical, but it still wants the mouse to be dragged horizontal. Anyone a clue how to attack this with css to rotate the drag action?
library(shiny)
ui <- fluidPage(
fluidRow(
column(3,
sliderInput(inputId = 'myslider1', label = 'Change vertical', min = -5, max = 6.3, step = 0.1, value = -6)
),
column(3,
sliderInput(inputId = 'myslider2', label = 'Change horizontal', min = -5, max = 6.3, step = 0.1, value = 0)
), style = "margin-top:200px"
),
tags$style(HTML(".js-irs-0 { transform: rotateZ(270deg)}")),
tags$style(HTML(".js-irs-0 .irs-bar-edge, .js-irs-0 .irs-bar {background: yellow}"))
)
server <- function(input, output, session) {
}
shinyApp(ui = ui, server = server)
There is this function noUiSliderInput() from the shinyWidgets package that does what you need.
See example below:
if (interactive()) {
### examples ----
# see ?demoNoUiSlider
demoNoUiSlider("more")
### basic usage ----
library( shiny )
library( shinyWidgets )
ui <- fluidPage(
tags$br(),
noUiSliderInput(
inputId = "noui1",
min = 0, max = 100,
value = 20
),
verbatimTextOutput(outputId = "res1"),
tags$br(),
noUiSliderInput(
inputId = "noui2", label = "Slider vertical:",
min = 0, max = 1000, step = 50,
value = c(100, 400), margin = 100,
orientation = "vertical",
width = "100px", height = "300px"
),
verbatimTextOutput(outputId = "res2")
)
server <- function(input, output, session) {
output$res1 <- renderPrint(input$noui1)
output$res2 <- renderPrint(input$noui2)
}
shinyApp(ui, server)
}
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))
})
}
)
)
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)
})
}
))