6

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?

1 Answer 1

5

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)
    })
  }
))
Sign up to request clarification or add additional context in comments.

1 Comment

the idea is, that I wanna plot something with the width and height of "x". And on the server-side I've got 2 types of plots, and one of them doesn't have the possibility to choose the size, that's why I need the x in there in the mainPanel. plotOutput() gives me the possibility to choose the size of any plot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.