【问题标题】:How to prevent R Shiny app immediately crashing when launching locally如何防止 R Shiny 应用程序在本地启动时立即崩溃
【发布时间】:2021-01-06 07:47:52
【问题描述】:

我的 Shiny 应用程序突然出现了一种奇怪的行为,它会短暂打开然后自行关闭。控制台中没有错误。在我的 Mac 上,该应用程序运行良好。但是,在 Windows 上,问题就出现了。

我的完整代码如下所示。

随后,在 Mac 或 Windows 中,当我使用完整的数据集 .csv 文件(而不是我的小型虚拟测试数据集)时,我收到错误 input string 1 is invalid UTF-8。我在这里尝试了所有建议How to identify/delete non-UTF-8 characters in R,但没有任何成功。我还在 Excel 本身中使用了CLEAN() 函数,还尝试了read.csv("dummyData.csv, encoding = "UTF-8"),但都没有奏效。我没有想法。

在这两个问题上的任何帮助都会很棒。

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)

【问题讨论】:

  • 你能发布数据吗?
  • 不幸的是数据很敏感,所以我不能。 Windows 似乎已经解决了崩溃问题本身。但是,我仍然收到输入字符串 1 is invalid UTF-8 错误。这是一个标准的 .csv 文件,我看不出有什么不寻常的地方。 @DaveArmstrong
  • 您的程序使用我使用空气质量数据定义的 dummyData 运行良好。我在 Windows 上运行它。
  • df$Column name&lt;- iconv(df$Column name, to = "UTF-8") 所以看来,通过使用 iconv 转换我的所有列似乎可以解决问题。与我上面链接中的内容略有不同。现在只是为了找到一种优雅的方式来循环它们而不是按名称引用。再次感谢@DaveArmstrong
  • @CiaranOBrien 您可以将您的解决方案作为答案发布并接受它,以便将来更容易找到您的解决方案

标签: r shiny


【解决方案1】:

我在这里对最佳答案进行了小修改:

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

只需通过以下代码转换我的列即可解决我的问题。

df$`Column Name`&lt;- iconv(df$`Column Name`, to = "UTF-8")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-20
    • 2013-12-13
    • 1970-01-01
    • 2013-12-19
    • 2015-06-09
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    相关资源
    最近更新 更多