【问题标题】:How do I make the choices in radioButtons reactive in Shiny?如何在闪亮的单选按钮中做出反应?
【发布时间】:2015-10-27 07:44:53
【问题描述】:

这就是我所拥有的,我认为问题在于 ui.R 首先运行,因此未定义函数 progs()(或者我不知道如何执行反应函数)。我的错误如下所示:

lapply(obj, function(val) { : 找不到函数“progs”中的错误

我的代码如下所示:

ui.R

library(shiny)
progs <- list.files("C:/Users/OIT-Classroom/Desktop/ODP/Report 1/aggregate/")
progs <- gsub('.{6}$', '', progs)

shinyUI(bootstrapPage(
  tabsetPanel(id="Reports",
              tabPanel("Report 1",
                       sidebarPanel(
                         p("text"),
                         radioButtons("choices", choices = progs(), label = "Programs")
                      ),
                       mainPanel(
                         tableOutput('age')
                       )
              ),
              tabPanel("Report 2",
                       sidebarPanel(
                         p("text"),
                         radioButtons("choices", choices = progs(), label = "Programs")
                       ),
                       mainPanel(
                         tableOutput('psc5'),
                         plotOutput('psc5p'),
                       )
              ) 
  )
))

服务器.R

library(shiny)

shinyServer(function(input, output, session) {

  progs <- reactive({
    progs <- list.files(paste("C:/Users/OIT-Classroom/Desktop/ODP/", input$Reports, "/aggregate/", input$choices, ".RData", sep = ""))
    gsub('.{6}$', '', progs)
  })

  output$age <- function(){
    paste(knitr::kable(
      age, format = 'html', output = FALSE,
      table.attr="class='data table table-bordered table-condensed'"),
      sep = '\n')
  }
  output$psc5 <- function(){
    paste(knitr::kable(
      psc5, format = 'html', output = FALSE,
      table.attr="class='data table table-bordered table-condensed'"),
      sep = '\n')
  }
  output$psc5p <- renderPlot({
    plot(psc5[, 2:3], type="b", ylim=c(0, 40), ylab="", xaxt="no", xlab="", col="red", main="Figure 2: Problem Severity Comparison of Mean Scores for Children Ages 5+", cex.main=0.8, cex.axis = 0.8, font.main = 1, family="serif")
    par(new = T)
    axis(1, at=1:2, labels=c("Intake", "Discharge"), col.axis="black", las=1, cex.axis =0.8)
  })
})

【问题讨论】:

  • 将它们放在 Server.R 文件中,或者您可以将它们添加到 global.R 文件,甚至可以在服务器和 ui 中获取的另一个文件中。 shiny.rstudio.com/articles/scoping.html
  • 尝试在 progs reactive 的末尾添加 return(progs)。

标签: r shiny shiny-reactivity


【解决方案1】:

我假设您正在尝试自动更新单选按钮的选项。您尝试调用的 progs 函数未在 UI 脚本中定义,因此这就是您的错误的来源。尝试在服务器脚本的某处使用observeupdateRadioButtons 函数。可能看起来像这样:

observe({
  updateRadioButtons(session, "choices", choices = progs())
})

如果您随后进入您的 UI 脚本并将单选按钮定义的 progs() 部分更改为只是 progs 它应该可以解决您在 UI 开头定义的 progs 变量。我无法测试所有这些,因为我没有文件,但它应该能让你接近。

【讨论】:

    猜你喜欢
    • 2021-05-01
    • 2019-08-02
    • 2016-12-15
    • 2016-07-08
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    相关资源
    最近更新 更多