【问题标题】:r shiny: access input fields in UIr shiny:访问 UI 中的输入字段
【发布时间】:2014-04-20 19:52:39
【问题描述】:

我正在尝试从 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)
      )
  }) 

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

【问题讨论】:

    标签: r user-interface input shiny


    【解决方案1】:

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

    例如:

    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
        })
      }
    ))
    

    编辑::

    动态设置绘图的尺寸。

    使用 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)
        })
      }
    ))
    

    【讨论】:

    • 这个想法是,我想用“x”的宽度和高度绘制一些东西。在服务器端,我有 2 种类型的绘图,其中一种无法选择大小,这就是为什么我需要在 mainPanel 中使用 x。 plotOutput() 让我可以选择任何绘图的大小
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 2017-01-30
    • 1970-01-01
    • 2021-01-15
    • 2014-07-12
    • 2020-10-14
    • 1970-01-01
    相关资源
    最近更新 更多