R闪亮错误:反应功能问题

时间:2014-06-24 10:03:25

标签: r shiny

我正在使用新的闪亮降价在R studio中制作交互式图表。想法是创建一个活动页面,其中有两个输入字段来确定a的大小,b的大小和剩下的是c的大小。输入以百分比表示。输出是一个带有直线和条形图的图形。但是,我有两个问题。

  1. 第二个输入必须依赖于第一个输入,因为你永远不会超过100%。
  2. 在给出输入后确定a和b的大小。如果输入发生变化,则必须再次确定a,b和c。
  3. 我是R的新手,并搜索其他问题,但无法解决我的问题。这是我的代码以及我如何解决它:

    inputPanel(
      numericInput("aa", label = "a:",
                  min = 1 , max =100 , value = 80 , step = 1),
    
      numericInput("bb", label = "b:",
                  min = 1 , max = 100-input$aa , value = 15 , step = 1)
    )
    

    这是我的第一个问题。我想成为第一个输入上被动的第二个数字输入。我为这个问题尝试了以下选项:

      numericInput("bb", label = "b:",
                  min = 1 , max = observe(100-input$aa,suspended= TRUE) , value = 15 , step = 1)
    

    这不起作用。我尝试的下一件事是:

      numericInput("bb", label = "b:",
                  min = 1 , reactive({max = 100-input$aa}) , value = 15 , step = 1)
    

    这也是错误的。在尝试了这个选项之后,我发现它很不合逻辑但是,仍然无法找到它如何解决这个问题。

    第二个问题出现在下一个代码中。

    ###change the column b
     for(i in nrow(df)){reactive({
      if(input$aa>df$a[i]){
      df$b[i] = "a"
    } else {if(input$aa+input$bb>df$a[i]){
      df$b[i] = "b"
    }else {df$b[i] = "c"}}})}
    

    我想在df中更改b列的值。这段代码不会产生任何错误,但是,它不是活动的,它不会更改列b。我尝试了以下方法:

    ###Change the column b
    shinyServer(function(input, output) {
    for(i in nrow(df)){reactive({
      if(input$large>df$a[i]){
      df$b[i] = "a"
    } else {if(input$aa+input$bb>df$a[i]){
      df$b[i] = "b"
    }else {df$b[i] = "c"}}})}})
    

    这似乎更多地忽略了代码。这是我尝试的,我对R很新。我希望有人可以帮我解决这个问题。

    提前致谢, 评论后编辑

    最短的节目(在我看来)

    ---
    title: "a"
    author: "b"
    date: "6/24/2014"
    output: html_document
    runtime: shiny
    ---
    
    ```{r, echo=FALSE}
    
    library(ggplot2)
    library(shiny)
    df= data.frame(a=c(1:20),b=c(1:20))
    
    df$c=1 #just to make sure column c exist.
    inputPanel(
      numericInput("aa", label="a", min = 1, max = 100, value =50)
                  ,
    
      numericInput("bb",label= "b", min = 1, max = 100-input$aa, value = 10)
    )
    
      df$c <- reactive({
      df$c <- input$aa
    })
    
    renderPlot({
      ggplot(df) +
      geom_line(aes(a, b, colour= c ))
    })
    ```
    

    涡流

1 个答案:

答案 0 :(得分:3)

以下是使用renderUI的示例。我没有看过在markdown文档中使用闪亮但是我猜它会以巧妙的方式解析文档,以便为shinyUIserver函数分配适当的数量:

---
title: "a"
author: "b"
date: "6/24/2014"
output: html_document
runtime: shiny
---

```{r, echo=FALSE}
library(ggplot2)
library(shiny)
df= data.frame(a=c(1:20),b=c(1:20))

df$c=1 #just to make sure column c exist.
inputPanel(
  numericInput("aa", label="a", min = 1, max = 100, value = 50)
  , uiOutput('test')
)

output$test <- renderUI({
  numericInput("bb",label= "b", min = 1, max = 100-as.integer(input$aa), value = 10)
})
myReact <- reactive({
  out <- df
  out$b <- out$b + input$aa
  out
})
renderPlot({
  ggplot(myReact()) + geom_line(aes(a, b, colour= c ))
})

```

enter image description here

相关问题