0

My Shiny app suddenly is behaving n a strange manner whereby it opens briefly and then closes itself. There are no errors in the console. On my Mac, the app works fine. However, on Windows, the issue arises.

My complete code can be seen below.

Subsequently, Mac, or Windows, when I am using my full dataset .csv file, (as opposed to my small dummy test dataset), I receive an error input string 1 is invalid UTF-8. I have tried all suggestions here How to identify/delete non-UTF-8 characters in R but without any success. I have also used the CLEAN() function in Excel itself, and also tried read.csv("dummyData.csv, encoding = "UTF-8"), neither of which worked. I'm out of ideas.

Any help on both these issues would be fantastic.

library(shiny)
library(tidyverse)
library(DT)

# Reading the main_data which the shiny app depends on, Please make sure that the column names are same
main_data <- read_csv("dummyData.csv")

ui <- fluidPage(
  fluidRow(column(12, tags$h2("Assignment Details"))),
  sidebarLayout(
    sidebarPanel(
      width = 3,
      tags$div(
        align = "center",
        tags$img(src = "logo.png", width = "120", height = "120")
      ),
      fluidRow(
        column(12, align = "center", tags$br(), tags$b("Filter data")),
        column(12, selectInput("sector_filter", "Sector", unique(main_data$Sector), multiple = TRUE)),
        column(12, selectInput("client_filter", "Client", unique(main_data$`Client Name`), multiple = TRUE)),
        column(12, selectInput("service_filter", "Service", unique(main_data$Service), multiple = TRUE)),
        column(12, selectInput("cost_filter", "Cost", unique(main_data$`Cost (Ex-Vat)`), multiple = TRUE)),
        column(12, align = "center", actionLink("reset_filters", "Clear Filters/Reset", style = "color: #962693"))
      )
    ),
    mainPanel(
      width = 9,
      tabsetPanel(
        tabPanel(
          "Assignment Description",
          uiOutput("assignment_description")
        ),
        tabPanel(
          "Data Table",
          DTOutput("data_table")
        )
      )
    )
  )
)

server <- function(input, output, session) {
  # Creating a new empty tibble (which is basically a data.frame) for filtering based on the filters selected
  filtered_data <- tibble()
  observeEvent(input$reset_filters, {
    updateSelectInput(session, "sector_filter", selected = "")
    updateSelectInput(session, "client_filter", selected = "")
    updateSelectInput(session, "service_filter", selected = "")
    updateSelectInput(session, "cost_filter", selected = "")
  })
  # The observe code block will be triggered everytime any reactive object from the UI is changed (In this case out filters)
  observe({
    # If all the inputs are empty, We will just send the whole data without the filters. Else we filter
    print(input$sector_filter)
    print(input$client_filter)
    print(input$service_filter)
    print(input$cost_filter)
    print(unique(main_data$Sector))
    sector_filter_values <- input$sector_filter
    client_filter_values <- input$client_filter
    service_filter_values <- input$service_filter
    cost_filter_values <- input$cost_filter
    if (is.null(input$sector_filter)) {
      sector_filter_values <- unique(main_data$Sector)
    }
    if (is.null(input$client_filter)) {
      client_filter_values <- unique(main_data$`Client Name`)
    }
    if (is.null(input$service_filter)) {
      service_filter_values <- unique(main_data$Service)
    }
    if (is.null(input$cost_filter)) {
      cost_filter_values <- unique(main_data$`Cost (Ex-Vat)`)
    }
    filtered_data <<- main_data %>%
      filter(Sector %in% sector_filter_values, `Client Name` %in% client_filter_values,
          Service %in% service_filter_values, `Cost (Ex-Vat)` %in% cost_filter_values)
    # This is where the assignment description will be rendered
    output$assignment_description <- renderUI({
      filtered_data$title <- paste0(filtered_data$`Client Name`, " - ", filtered_data$`Assignment Name`)
      HTML(
        paste0(
          "<br><span style='color: #962693'>", filtered_data$title,
          "</span><br>", filtered_data$`Assignment Description`, "<br>"
        )
      )
    })
    # This is where the table is rendered. To customise the table visit here https://rstudio.github.io/DT/
    output$data_table <- renderDT({
      datatable(
        filtered_data %>% select(`Client Name`, `Assignment Name`, `Sector`, `Service`, `Cost (Ex-Vat)`)
      )
    })
  })
  # Whenever a row from the table is selected the Assignment Description must change regardless the filters selected
  observeEvent(input$data_table_rows_selected, {
    print(input$data_table_rows_selected)
    filtered_data_from_table <- filtered_data[input$data_table_rows_selected, ]
    print(filtered_data_from_table)
    output$assignment_description <- renderUI({
      filtered_data_from_table$title <- paste0(filtered_data_from_table$`Client Name`, " - ", filtered_data_from_table$`Assignment Name`)
      HTML(
        paste0(
          "<br><span style='color: #962693'>", filtered_data_from_table$title,
          "</span><br>", filtered_data_from_table$`Assignment Description`, "<br>"
        )
      )
    })
  })
}

shinyApp(ui = ui, server = server)
Ciaran O Brien
  • 374
  • 3
  • 13
  • Can you post the data? – DaveArmstrong Sep 19 '20 at 18:30
  • Unfortunately the data is sensitive so I cannot. It seems Windows has resolved the crashing issue itself. However, I still receive the input string 1 is invalid UTF-8 error. It is a standard .csv file and I cannot spot anything unusual about it. @DaveArmstrong – Ciaran O Brien Sep 19 '20 at 18:33
  • Your program runs fine with a dummyData I defined using airquality data. I ran it on Windows. – YBS Sep 19 '20 at 18:34
  • The solutions you've pointed to that didn't work have worked for me in the past, so I'm at a loss. – DaveArmstrong Sep 19 '20 at 18:34
  • @DaveArmstrong thanks for trying anyway, I appreciate that. I'm scratching my head with this one. I removed all non alphanumeric characters from the Excel but it seems stuck telling me the string is invalid! Very unusual. – Ciaran O Brien Sep 19 '20 at 18:43
  • 1
    `df$`Column name`<- iconv(df$`Column name`, to = "UTF-8")` So it would appear that by converting all my columns with iconv seemed to do the trick. A little variation from what is in my link above. Now just to find an elegant way to loop over them instead of referencing by name. Thanks again @DaveArmstrong – Ciaran O Brien Sep 19 '20 at 19:11
  • Should be able to use sapply or maybe mutate with across(everything(),...) to do it. – DaveArmstrong Sep 19 '20 at 19:14
  • Absolutely. I went with sapply myself. @DaveArmstrong – Ciaran O Brien Sep 19 '20 at 19:46
  • 1
    @CiaranOBrien you can post your solution as an answer and accept it, so in the future it's easier to find your solution – starja Sep 20 '20 at 11:14

1 Answers1

0

I took a small modification of the top answer here:

How to identify/delete non-UTF-8 characters in R

Simply converting my columns via the below code fixed my issues.

df$`Column Name`<- iconv(df$`Column Name`, to = "UTF-8")

Ciaran O Brien
  • 374
  • 3
  • 13