【问题标题】:checkboxinput server issues in RR中的复选框输入服务器问题
【发布时间】:2022-01-22 10:10:14
【问题描述】:

我想在我的 UI 中有一个复选框,它将在我的服务器中充当 if 语句,但我不确定如何正确编码服务器中的部分。在此示例中,我希望在选中该框时输出图表。

这是我的代码

ui <- fluidPage(

    titlePanel("title"),

    sidebarLayout(
        sidebarPanel(
            checkboxInput("EF", "Efficient Frontier")
        ),

        mainPanel(
            fluidRow(
                align = "center",
                plotOutput("Graphw")
            )
        )
    )
)

server <- function(input, output) {
    
                if(input$EF){
                    X <- function(a,b,c){
                        plot(c(a,b),c(b,c))
                         }
                }
    
                OPw <- reactiveValues()
                OPw$PC <- X(5,10,15)
                
                output$Graphw <- renderPlot({ 
                    OPw$PC
                })
}

shinyApp(ui = ui, server = server)

我需要添加某种反应值,但我不确定在哪里。任何帮助将不胜感激

【问题讨论】:

  • plot 函数不返回任何内容。没有必要使用反应式。例如,ggplot 确实会返回要存储在变量中的图。

标签: r checkbox shiny shinydashboard


【解决方案1】:

我认为您可以在不使用 reactiveValues 的情况下做到这一点。将函数X设为独立函数,可以在renderPlot下调用。

library(shiny)

X <- function(a,b,c){
  plot(c(a,b),c(b,c))
}

ui <- fluidPage(
  
  titlePanel("title"),
  
  sidebarLayout(
    sidebarPanel(
      checkboxInput("EF", "Efficient Frontier")
    ),
    
    mainPanel(
      fluidRow(
        align = "center",
        plotOutput("Graphw")
      )
    )
  )
)

server <- function(input, output) {
  
  output$Graphw <- renderPlot({ 
    if(input$EF){
      X(5,10,15)
    }
  })
}

shinyApp(ui = ui, server = server)

【讨论】:

  • 谢谢!这真的很有帮助。我有一个后续问题,我在这里发布了link。如果你有时间,请随意看看:)
【解决方案2】:

如果绘制有效边界需要一些时间,我们可以使用conditionalPanel 来显示或隐藏plotOutput,而不是每次按下checkboxInput 时都重新渲染。

library(shiny)

X <- function(a, b, c) {
  plot(c(a, b), c(b, c))
}

library(shiny)

ui <- fluidPage(
  titlePanel("title"),
  sidebarLayout(
    sidebarPanel(
      checkboxInput("EF", "Efficient Frontier")
    ),
    mainPanel(
      fluidRow(
        align = "center",
        conditionalPanel(
          condition = "input.EF == true",
          plotOutput("Graphw")
        )
      )
    )
  )
)

server <- function(input, output) {
  output$Graphw <- renderPlot({
    X(5, 10, 5)
  })
}

shinyApp(ui = ui, server = server)

另一个使用reactiveValuesggplot的版本

library(shiny)

ui <- fluidPage(
  titlePanel("title"),
  sidebarLayout(
    sidebarPanel(
      checkboxInput("EF", "Efficient Frontier")
    ),
    mainPanel(
      fluidRow(
        align = "center",
        conditionalPanel(
          condition = "input.EF == true",
          plotOutput("Graphw")
        )
      )
    )
  )
)

server <- function(input, output) {
  X <- function(a, b, c) {
    ggplot(mapping = aes(x = c(a, b), y = c(b, c))) +
      geom_point()
  }

  OPw <- reactiveValues(PC = NULL)

  observe({
    OPw$PC <- X(5, 10, 5)
  })



  output$Graphw <- renderPlot({
    OPw$PC
  })
}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2015-09-01
    • 1970-01-01
    • 2020-03-19
    • 2011-05-15
    • 2018-04-15
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多