1

I am trying to let the user select the kind of display he wishes, but when I try to render the plot it gives me an error.

Here is the code:

library(shiny)
library(DT)
library(data.table)

runApp(list(
    ui = fluidPage(
        wellPanel(
            radioButtons("visuBtn", NULL, choices = c(Table = "table", Plot = "plot"))
        ),
        wellPanel(
            uiOutput("DataTable")
        )
    ),
    server = function(input, output){

        observeEvent(input$visuBtn,{
            output$DataTable <- renderUI({
                dfconc <- data.table(time = c(1,2,3,4,5), concentration = c(0.1, 0.4, 0.5, 0.7, 0.8))

                if(input$visuBtn == "table"){
                    output$aa <- renderDataTable(dfconc, options = list(paging = FALSE, searching = FALSE))
                    dataTableOutput("aa")
                }
                else { ### NOT WORKING
                    output$aa <- renderPlot({
                        plot(dfconc$time, dfconc$concentration, xlab = "Time", ylab = "Concentration")
                    })
                    fixedRow(
                        plotOutput("aa")
                    )
                }      ###

            })
        })
    }
))


Thanks for your help

Go.0
  • 35
  • 7

2 Answers2

4

I think its better if you render the outputs on the client side and then simply show and hide the elements based on the selection. This way you're not wasting resources on the server side

library(shiny)
library(shinyjs)
library(DT)
library(data.table)

runApp(list(
  ui = fluidPage(
    useShinyjs(),
    wellPanel(
      radioButtons("visuBtn", NULL, choices = c(Table = "table", Plot = "plot"))
    ),
    wellPanel(
      dataTableOutput("mytable"),
      plotOutput("myplot")
    )
  ),
  server = function(input, output, session){

    dfconc <- data.table(time = c(1,2,3,4,5), concentration = c(0.1, 0.4, 0.5, 0.7, 0.8))

    output$mytable <- renderDataTable(
      dfconc, options = list(paging = FALSE, searching = FALSE)
    )

    output$myplot <- renderPlot({
      plot(dfconc$time, dfconc$concentration, xlab = "Time", ylab = "Concentration")
    })

    observeEvent(input$visuBtn,{
      req(input$visuBtn)
      if(input$visuBtn == "plot"){
        hide("mytable")
        show("myplot")
      }else{
        hide("myplot")
        show("mytable")
      }
    })
  }
))
Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • Thanks for the answer, I'm gonna use this. But I wonder why my version would not work. – Go.0 Nov 27 '19 at 12:51
  • You had the outputs with same id `output$aa`, you can only have 1 unique id per output – Pork Chop Nov 27 '19 at 13:04
  • dear @PorkChop could you please help with this https://stackoverflow.com/questions/59129562/in-r-shiny-how-to-fix-error-in-sourcing-app-as-nothing-changes-when-clicking-on?fbclid=IwAR2XoSXF7weHkpZ2a9rOXa69KgKcgeMGO6xdQ2Xv1ZRUKWmAa7bVeZlktMo – John Smith Dec 02 '19 at 06:44
2

Agree with @Pork Chop. But why not simply use a conditionalPanel

library(shiny)
library(DT)
library(data.table)

runApp(list(
  ui = fluidPage(
    wellPanel(
      radioButtons("visuBtn", NULL, choices = c(Table = "table", Plot = "plot"))
    ),

      conditionalPanel(
        condition = "input.visuBtn == 'table'",
        DTOutput('aa')
      ),
      conditionalPanel(
        condition = "input.visuBtn == 'plot'",
        plotOutput('bb')
      )

  ),
  server = function(input, output){
    dfconc <- data.table(time = c(1,2,3,4,5), concentration = c(0.1, 0.4, 0.5, 0.7, 0.8))

          output$aa <- renderDT(dfconc, options = list(paging = FALSE, searching = FALSE))
          output$bb <- renderPlot({
            plot(dfconc$time, dfconc$concentration, xlab = "Time", ylab = "Concentration")
          })
        } 

))
Dhiraj
  • 1,650
  • 1
  • 18
  • 44