Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 289
Paul Avatar asked Oct 02 '22 07:10

Paul


1 Answers

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)
    })
  }
))
like image 150
Julien Navarre Avatar answered Oct 05 '22 12:10

Julien Navarre