簡體   English   中英

R閃亮錯誤:反應功能問題

[英]R shiny error: Problems with a reactive function

我正在R studio中使用新的閃亮降價制作交互式圖表。 想法是創建一個活動頁面,其中有兩個輸入字段來確定a的大小,b的大小和剩下的是c的大小。 輸入以百分比表示。 輸出是一個帶有直線和條形圖的圖形。 但是,我有兩個問題。

  1. 第二個輸入必須依賴於第一個輸入,因為您永遠不會超過100%。
  2. 給出輸入后確定a和b的大小。 如果輸入發生變化,則必須再次確定a,b和c。

我對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 ))
})
```

渦流

以下是使用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 ))
})

```

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM