Paul Paul -4 years ago 55
R Question

r shiny: access input fields in UI

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?

Answer Source

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)
    })
  }
))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download