popeye
popeye

Reputation: 291

rendering through data table row selection - in Shiny

In a shiny app that I am building I have two data frames which are subsets of a primary data frame, the data of which I am rendering data through data tables. Taking cues from this example, I have created two data table output and one text output. While the data table and the text output renders without any trouble, the second data table, which takes input from the first datable, is throwing error. The reproducible example of two data frames are as follows. The first one is tagged as df and the second as dfa

The reproducible data of df:

structure(list(MediaType = c("BILLBOARDS", "BILLBOARDS", "MALL FACADES", 
"BUS SHELTER", "WALL GRAPHICS"), Rental = c(62500, 160000, 240000, 
112000, 210000), Sizeinsq = c(800, 200, 200, 108, 400)), .Names = c("MediaType", 
"Rental", "Sizeinsq"), row.names = c("BrandA", "BrandB", "BrandC", 
"BrandD", "BrandE"), class = "data.frame")

The second data frame dfa :

structure(list(Client = c("BrandA", "BrandB", "BrandC", "BrandD", 
"BrandE"), MediaType = c("BILLBOARDS", "BILLBOARDS", "MALL FACADES", 
"BUS SHELTER", "WALL GRAPHICS"), Rental = c(62500, 160000, 240000, 
112000, 210000), Sizeinsq = c(800, 200, 200, 108, 400)), .Names = c("Client", 
"MediaType", "Rental", "Sizeinsq"), row.names = c(NA, 5L), class = "data.frame")

The code :

library(shiny)
runApp(list(
    ui = bootstrapPage(
        dataTableOutput("x1"), 
        verbatimTextOutput("t1"),
        dataTableOutput("x2")
    ),
    server = function(input, output){
        output$x1 = renderDataTable(
            df, server = TRUE, rownames = TRUE
        )

        output$t1 <- renderPrint({
            s <- input$x1_rows_selected
            if(length(s)){
                cat(s)
            }
        })

        output$x2 <- renderDataTable(
            s <- input$x1_rows_selected
            if(length(s)){
            k <- dfa[dfa$Client %in% s, ]
            k
            }
            )

    }
))

Either I am getting the parenthesis error or the some "args" mismatch error.

The first datatable and the text output renders perfectly, while the second data table doesn't. I have tried many options such as using DT in front of the renderDataTable(of course after adding library(DT)). I tried to convert the verbatimTextOutput to textInput with an assumption that I could take this input to render the second data table. I tried using the textOutput option and tried the take the value. But none seem to be of help.

Essentially, I want to subset the second data frame based on the row name of the first data frame.

my session info:

R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.3 (Yosemite)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] htmltools_0.3        leaflet_1.0.0        data.table_1.9.4    
 [4] markdown_0.7.7       reshape2_1.4.1       lazyeval_0.1.10     
 [7] tidyr_0.3.1          zoo_1.7-12           shinyjs_0.6         
[10] shinyBS_0.61         shinydashboard_0.5.1 plotly_3.4.13       
[13] ggplot2_2.1.0        dplyr_0.4.3          DT_0.1              
[16] shiny_0.13.2         RMySQL_0.10.8        DBI_0.3.1           

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.3      plyr_1.8.3       base64enc_0.1-3  viridis_0.3.2   
 [5] tools_3.2.0      digest_0.6.9     jsonlite_0.9.19  gtable_0.1.2    
 [9] lattice_0.20-33  yaml_2.1.13      parallel_3.2.0   gridExtra_2.0.0 
[13] stringr_1.0.0    httr_1.1.0       htmlwidgets_0.5  grid_3.2.0      
[17] R6_2.1.2         magrittr_1.5     scales_0.3.0     assertthat_0.1  
[21] mime_0.4         xtable_1.7-4     colorspace_1.2-6 httpuv_1.3.3    
[25] stringi_1.0-1    miniUI_0.1.1     munsell_0.4.3    chron_2.3-47    

Upvotes: 0

Views: 877

Answers (1)

Michal Majka
Michal Majka

Reputation: 5471

You forgot to add {} in this part of the code:

output$x2 <- renderDataTable({ # added { 
      s <- input$x1_rows_selected
      if(length(s)){
        k <- dfa[dfa$Client %in% s, ]
        k
      }
    }) # added }

Full code:

Edited: I added library DT


df  <- structure(list(MediaType = c("BILLBOARDS", "BILLBOARDS", "MALL FACADES", 
                                    "BUS SHELTER", "WALL GRAPHICS"),
                      Rental = c(62500, 160000, 240000, 112000, 210000),
                      Sizeinsq = c(800, 200, 200, 108, 400)), 
                      .Names = c("MediaType", "Rental", "Sizeinsq"), 
                      row.names = c("BrandA", "BrandB", "BrandC", "BrandD", "BrandE"), 
                      class = "data.frame")


dfa <- structure(list(Client = c("BrandA", "BrandB", "BrandC", "BrandD", "BrandE"), 
                      MediaType = c("BILLBOARDS", "BILLBOARDS", "MALL FACADES", 
                                    "BUS SHELTER", "WALL GRAPHICS"), 
                      Rental = c(62500, 160000, 240000, 112000, 210000), 
                      Sizeinsq = c(800, 200, 200, 108, 400)), 
                      .Names = c("Client", "MediaType", "Rental", "Sizeinsq"), 
                      row.names = c(NA, 5L), class = "data.frame")



library(shiny)
library(DT)
runApp(list(
  ui = bootstrapPage(
    dataTableOutput("x1"), 
    verbatimTextOutput("t1"),
    dataTableOutput("x2")
  ),
  server = function(input, output){
    output$x1 = renderDataTable(
      df, server = TRUE, rownames = TRUE
    )

    output$t1 <- renderPrint({
      s <- input$x1_rows_selected
      if(length(s)){
        cat(s)
      }
    })

    output$x2 <- renderDataTable({ # added { 
      s <- input$x1_rows_selected
      if(length(s)){
        k <- dfa[dfa$Client %in% s, ]
        k
      }
    }) # added }

  }
))

Upvotes: 1

Related Questions