esterodr
esterodr

Reputation: 117

R Shiny: PlotOutput not updating in Shiny app

I'm trying to make a simple Shiny app with the following features:

2 or 3 inputs:

The output is a ggplot where the labels inside of it change with the inputs.

I also want a default plot and a Reset button.

I made 3 files: ui.R, server.R and function.R. The last one is the one that makes the plot.

When input1 takes the values of "A" or "B", I get the desired output. The "Reset" button also seems to work fine. However, when I select "C" in input 1, it brings me back to the default plot: a plot whith the label "Nothing", instead of something like "C1 10".

I checked the code many times, but I can't get where is the problem.

Here is the code of my files:

# file ui.R

library(shiny)

shinyUI(fluidPage(
    
    plotOutput("printsomething"),
    
    selectInput(
        inputId="input1",
        label="input1",
        choices=c("A","B","C"),
        selected = NULL,
        multiple = FALSE,
        selectize = TRUE,
        width = NULL,
        size = NULL),
    
    conditionalPanel(
        condition = "input.input1 == 'A'",
        selectInput("input2", "input2",
                    list("A1", "A2"))),
    
    conditionalPanel(
        condition = "input.input1 == 'C'",
        selectInput("input2", "input2",
                    list("C1", "C2"))),
    
    numericInput("num","Number",value=0,min=0),
    
    actionButton("Run","Run"),
    actionButton("Reset","Reset")

    )
)
# file server.R

source("function.R")

library(shiny)

shinyServer(function(input, output) {
  
  graph <- reactiveValues(data = NULL)
  
  observeEvent(input$Run, {
    
    graph$data <- printsomething(data,input$input1,input$input2,input$num)

  })
  
  observeEvent(input$Reset, {
    graph$data <- printsomething("Nothing","NA","NA","NA")
  })  
  
  output$printsomething <- renderPlot({
    if (is.null(graph$data)) return(printsomething(data,"NA","NA","NA"))
    graph$data
  })
  
  })
    
# file function.R

library(ggplot2)

data <- "Nothing"

printsomething <- function(data,input1=NA,input2=NA,num=0) {
  
  if(is.na(input1)) {
    
    data <- "Nothing"
    
  } else if(input1=="A") {
    
    if(input2=="A1") {
      data <- paste("A1",num)
    } else if(input2=="A2") {
      data <- paste("A2",num)
    }
    
  } else if(input1=="B") {
    
    data <- paste("B",num)
    
  } else if(input1=="C") {
    
    if(input2=="C1") {
      data <- paste("C1",num)
    } else if(input2=="C2") {
      data <- paste("C2",num)
    }
    
  }
  
  ggplot() +
    geom_label(aes(x=1,y=1,label=data))
}

I'll really appreciate it if someone can help me. I'm new to Shiny.

Thanks.

Upvotes: 0

Views: 553

Answers (1)

YBS
YBS

Reputation: 21287

As stated in the comment, unique inputId will make it work. There is a minor adjustment in the server side, and no change in the function printsomething. Try this

library(shiny)

ui <- shinyUI(fluidPage(

  plotOutput("printsomething"),

  selectInput(
    inputId="input1",
    label="input1",
    choices=c("A","B","C"),
    selected = NULL,
    multiple = FALSE,
    selectize = TRUE,
    width = NULL,
    size = NULL),

  conditionalPanel(
    condition = "input.input1 == 'A'",
    selectInput("input2", "input2", list("A1", "A2"))),

  conditionalPanel(
    condition = "input.input1 == 'C'",
    selectInput("input3", "input2", list("C1", "C2"))),

  numericInput("num","Number",value=0,min=0),

  actionButton("Run","Run"),
  actionButton("Reset","Reset")

))

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

  graph <- reactiveValues(data = NULL)

  observeEvent(input$Run, {

    if (input$input1=="A"){ input2 = input$input2
    }else if (input$input1=="C") input2 = input$input3

    graph$data <- printsomething(data,input$input1,input2,input$num)

  })

  observeEvent(input$Reset, {
    graph$data <- printsomething("Nothing","NA","NA","NA")
  })

  output$printsomething <- renderPlot({
    if (is.null(graph$data)) return(printsomething(data,"NA","NA","NA"))
    graph$data
  })

})

shinyApp(ui, server)

Upvotes: 3

Related Questions