r 闪亮:访问 UI 中的输入字段

Pau*_*aul 5 user-interface r input shiny

我试图从 sidebarPanel访问mainPanel 中的输入字段,但我无法成功。

代码:

  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)
      )
  }) 
Run Code Online (Sandbox Code Playgroud)

似乎问题出在哪里?或者不能在 mainPanel 中使用来自 sidebarPanel 的输入?

Jul*_*rre 5

您只能使用服务器端的输入。

例如 :

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
    })
  }
))
Run Code Online (Sandbox Code Playgroud)

编辑 ::

动态设置绘图的尺寸。

使用 renderUi 使用输入值渲染绘图输出。

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)
    })
  }
))
Run Code Online (Sandbox Code Playgroud)