how can I summarize survey answers from a data table in R shiny?

Hello community!
I feel like my question is very basic but I couldn't find a solution in the shiny tutorials or online.
I would like to summarize survey answers from a table first. I could import the data to my shiny app so far:

anreise2013 <- read_csv("anreise2013.csv")

... and display the data table in my ui:

render Datatable
_ output$table <- DT::renderDataTable({_
_ DT::datatable(anreise2013)_
_ })_

The table contains only one row with the header "how did you get here" and below are the standardized answers from the respondents in text format (by car, by feet, by train or by bicycle).

I tried to use the data.table package to preform following action:
dt[, .(x = sum(a))] – create a data.table with new columns based on the summarized values of rows.
(copied from data.table cheat sheet)

but I looks like my syntax was wrong or maybe I already imported the table with the wrong command.
This is the error message:

Error in .(x = sum(a)) : could not find function "."

The final output I'd like to create should look something like this:

Can someone link me to a tutorial or explain me what I'm doing wrong?
I'd appreciate it a lot :slight_smile:

Thank you!
Julie

You should be aware that data.table and DT::datatable are very different things, so it's possible you have confused these. As you are already using the tidyverse function read_csv I suspect this is the case.

The error message is likely because you have not loaded data.table via: library(data.table)

The tidyverse/dplyr equivalent would be something like dt %>% summarise(x = sum(a))

For any further help re the plots you should produce a reproducible example:

:

Hi Martin,
thank you for the fast answer :slightly_smiling_face:

I tried to preform a reprex, I hope it turned out well. I put together the stuff I learned from the shiny tutorial to make myself a template. this is how my app looks like at the moment:

library(shiny)
library(readr)
library(markdown)
#> Warning: Paket 'markdown' wurde unter R Version 3.5.2 erstellt
library(plotly)
#> Warning: Paket 'plotly' wurde unter R Version 3.5.2 erstellt
#> Lade nötiges Paket: ggplot2
#> Warning: Paket 'ggplot2' wurde unter R Version 3.5.2 erstellt
#> 
#> Attache Paket: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
library(datasets)
library(data.table)
library(tidyverse)
#> Warning: Paket 'tidyverse' wurde unter R Version 3.5.2 erstellt
#> Warning: Paket 'tidyr' wurde unter R Version 3.5.2 erstellt
#> Warning: Paket 'dplyr' wurde unter R Version 3.5.2 erstellt
#> Warning: Paket 'stringr' wurde unter R Version 3.5.2 erstellt
#> Warning: Paket 'forcats' wurde unter R Version 3.5.2 erstellt

anreise2013 <- read_csv("anreise2013.csv")
#> Error: 'anreise2013.csv' does not exist in current working directory ('C:/Users/julie/AppData/Local/Temp/Rtmpsb5ylA/reprex3f2435231785').



# Define UI for app that draws a histogram ----
ui <- fluidPage(
  
  # App title ----
  titlePanel("Vorlage"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      h3("Sidebar Panel"),
      br(),
      
      # Input Action Button
      h4("Action Button"),
      actionButton("action", label = "Action"),
      hr(),
      fluidRow(column(12, verbatimTextOutput("value1"))),
      
      #Input dropdown selection box
      selectInput("select", label = h4("Select box"), 
                  choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3), 
                  selected = 1),
      hr(),
      fluidRow(column(12, verbatimTextOutput("value2"))),
      
      #Text input
      textInput("text", label = h4("Text input"), value = "Enter text..."),
      hr(),
      fluidRow(column(12, verbatimTextOutput("value3"))),
      
      # slider input
      sliderInput("slider1", label = h4("Slider"), min = 1, 
                  max = 6, value = 5),
      hr(),
      verbatimTextOutput("value4")
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      h3("Main Panel"),
      #rendertext
      p("rendertext function: you pushed the action button", textOutput("action_value1"), "times."),
      #renderimage
      p("renderImage function: (input: selection box)"),
      imageOutput("numberimage"),
      #rendertable
      tabPanel("Table", DT::dataTableOutput("table")),
      #plotoutput bar plot
      plotOutput("anreisebar"),
      #fonts and image input
      img(src = "tree.png", align = "right"),
      p("this is a paragraph // font test below"),
      strong("bold text"),
      em("italicized text"),
      code("computer code style"),
      div("blue text", style = "color:blue"),
      div("green text", style = "color:green"),
      p("some words in a different style with span",
        span("some words", style = "color:orange"),
        "that appear inside a paragraph.")
      
      # Output: 
      
    )
  )
)
# Define server logic required to draw a histogram ----
server <- function(input, output) {
  #renderprint
  output$value1 <- renderPrint({ input$action })
  output$value2 <- renderPrint({ input$select })
  output$value3 <- renderPrint({ input$text })
  output$value4 <- renderPrint({ input$slider1 })
  #rendertext
  output$action_value1 <- renderText({ input$action })
  #renderimage
  output$numberimage <- renderImage({
    if (is.null(input$select))
      return(NULL)
    
    if (input$select == "1") {
      return(list(
        src = "www/nr1.png",
        contentType = "image/png",
        alt = "1"
      ))
    } else if (input$select == "2") {
      return(list(
        src = "www/nr2.png",
        filetype = "image/png",
        alt = "2"
      ))
    }else if (input$select == "3") {
      return(list(
        src = "www/nr3.png",
        filetype = "image/png",
        alt = "3"
      ))
    }
    
  }, deleteFile = FALSE)
  #Render Datatable
  output$table <- DT::renderDataTable({
    DT::datatable(anreise2013)
  })

  
}

shinyApp(ui = ui, server = server)

Shiny applications not supported in static R Markdown documents

Created on 2019-01-29 by the reprex package (v0.2.1)

this is the csv I want to work with: https://drive.google.com/file/d/1DMAIpOT9k63iMn5Ou4gT9zby1EybN44U/view?usp=sharing

Sorry, I overlooked that this was for Shiny. There is a separate guide for Shiny reprexes:

I don't use Shiny, so hopefully somebody else can help you, but as a general piece of advice it is best to generate the plots you want outside of Shiny first to ease the process.

Ok, then I try to figure it out in R first, thank you :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.