【问题标题】:SelectInput in Shiny在 Shiny 中选择输入
【发布时间】:2016-12-30 14:11:08
【问题描述】:

我正在尝试构建一个简单的 Shiny 应用程序,但无法使其正常工作。我想选择一个状态,然后应用程序应该计算该状态的平均值以进行采样。臭氧水平的测量。这是我的 ui.R 代码:

require(shiny)
fluidPage(pageWithSidebar(
headerPanel("Ozone Pollution"),
sidebarPanel(
h3('State'),selectInput("inputstate","Select State",state.name)),
mainPanel(
h3('Results'),verbatimTextOutput("res")
)
))

这是我的 server.R 程序:

require(dplyr)
library(shiny)
shinyServer(
function(input, output) {
stat_state<-reactive({filter(ozone_2015,State.Name==input$inputstate)})
output$res<- renderPrint({mean(stat_state$Sample.Measurement)})
}
)

任何提示?谢谢.....

【问题讨论】:

  • 也许把这个stat_state$Sample.Measurement改成这个stat_state()$Sample.Measurement

标签: r shiny


【解决方案1】:

虽然我无法复制您的数据集,因为我不知道臭氧 2015 的来源,但我认为您的问题是您不是指这样的“反应性”对象:

stat_state()

一旦你创建了一个响应式对象,除了响应式值和 input$ 变量之外,你需要在变量末尾使用 '()' 来引用它。

这是一个使用您的一些代码与不同数据集的示例。希望这会有所帮助。

require(shiny)

ui <- 
fluidPage(pageWithSidebar(
  headerPanel("Population"),
  sidebarPanel(
    h3('State'),selectInput("inputstate","Select State",state.name)),
  mainPanel(
    h3('Results'),verbatimTextOutput("res")
  )
))

server <- function(input,output){
  require(dplyr)

  sample.data <- reactive({as.data.frame(state.x77)})

      stat_state <- reactive({sample.data()[which(row.names(sample.data()) == input$inputstate),]})
      output$res <- renderPrint({stat_state()$Population})
    }
  )
}


shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2016-10-26
    • 2020-06-27
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    相关资源
    最近更新 更多