Reputation: 93
I am making an interactive graph in R studio with the new shiny markdown. What the idea is to create an active page where you have two input fields to determine the size of a, the size of b and what is left is the size of c. The input is in percentage. The output is a graph with a line and bars, which works. However, I have two problems.
I am quite new to R and searched to other questions but could not solve my problems. This is my code and how I tried to solve it:
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)
)
This is my first problem. I want to be the second numeric input to be reactive on the first input. I tried the following options for this problem:
numericInput("bb", label = "b:",
min = 1 , max = observe(100-input$aa,suspended= TRUE) , value = 15 , step = 1)
This did not work. The next thing I tried is:
numericInput("bb", label = "b:",
min = 1 , reactive({max = 100-input$aa}) , value = 15 , step = 1)
This as well gave an error. After trying this options, I find it quite logic it does not work, however, still can not find it how to solve this.
The second problem is in the next code.
###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"}}})}
I want to change the value of the column b in df. This piece of code does not give any errors, however, it is not active, it does not change the column b. I tried the following:
###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"}}})}})
This seems to ignore the code even more. This is what I tried and I am quite new to R. I hope someone can help me with this issue.
Thanks in advance, EDIT After comment
Shortest program (in my opinion)
---
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 ))
})
```
Eddy
Upvotes: 3
Views: 2321
Reputation: 30425
Here is an example using renderUI
. I havent looked at using shiny in markdown documents but im guessing it parses the document in a clever manner to allocate the appropriate quantities to a shinyUI
and server
function:
---
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 ))
})
```
Upvotes: 3