【问题标题】:How to return multiple values in R ShinyServer如何在 R ShinyServer 中返回多个值
【发布时间】:2017-03-09 11:19:56
【问题描述】:

我正在做以下事情:

  1. 使用 R ShinyUI,获取客户端对变量 A、B、C 范围的输入;
  2. 在 R ShinyServer 中,读取 csv 文件,并使用客户端输入对 csv 进行切片,并获取我需要的部分;
  3. 在 csv 上执行循环计算,从循环输出计算各种统计数据,并绘制所有这些统计数据。

伪代码:

data = read.csv('file.csv')

shinyServer(function(input, output) {

  data <- reactive({ 
  data = data[data$A<INPUT1 & data$B> INPUT2 & data$C<INPUT3,]
  })

 for (i in 1:dim(data)[1]){
   result1[i] = xxx
   result2[i] = xxx
  }


  output$plot <- renderPlot({
  plot(result1)
  })

 })

上面的代码不起作用。我想知道:

  1. 如何正确合并用户输入并获取变量“数据”
  2. 如何从 output$plot 中绘制 result1 和 result2

谢谢!

【问题讨论】:

  • 我不是很清楚你的问题,但假设“INPUT”是闪亮的 UI 元素,你可以使用input$inputname 访问它们。要读取 csv,您需要在 UI 中使用 fileInput() 并在服务器中捕获它,例如 data &lt;- read.csv(input$inputname$datapath)。见这里:shiny.rstudio.com/gallery/upload-file.html

标签: r shiny shiny-server


【解决方案1】:

for 循环应该在renderPlot 内,因此每次input$month 更改时,响应数据都会更改,然后 for lop 将更新您的变量。如果您在反应式表达式之外有 for 循环,它将仅在应用启动时执行一次,但在 input 发生更改之后。

以下是基于您在原始问题中提供的伪代码的简单示例,用于说明可能的解决方案。

library(shiny)

ui <- shinyUI( fluidPage(
  fluidRow(
    column(4,
      numericInput("input1", "Speed >", 8),
      numericInput("input2", "Dist >", 15)
    ),
    column(8, 
      plotOutput("plot")
    )
  )
))

server <- shinyServer(function(input, output) {
  dat0 <- cars

  data <- reactive({ 
    dat0[dat0$speed > input$input1 & dat0$dist > input$input2,]
  })

  output$plot <- renderPlot({
    s <- dim(data())[1] 
    result1 <- numeric(s)
    result2 <- numeric(s)
    for (i in 1:s){
     result1[i] <- data()[i, 1]
     result2[i] <- data()[i, 2]
    }
    plot(result1, result2)
  })

})

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 2021-07-01
    相关资源
    最近更新 更多