【问题标题】:Got an error when running css with Shiny R使用 Shiny R 运行 css 时出错
【发布时间】:2020-07-15 12:35:08
【问题描述】:

我正在尝试在 Shiny R 中运行我的 css 文件。我在没有 css 的情况下成功运行。但是我添加了 style.css 并且它不起作用。这是我所做的:

library(shiny)
library(tidyverse)
library(leaflet)
library(leaflet.extras)

fake_data <- read.csv("https://raw.githubusercontent.com/gabrielburcea/stackoverflow_fake_data/master/gather_divided.csv")




# Define UI for application that draws a histogram
ui <- fluidPage(
    
    navbarPage("SARS-Covid-19 Symptom Mapper", 
             div(class = "outer", 
                 
                 tabPanel("Interactive map", 
                          div(class = "outer", 
                              
                              tags$head(
                                  # Include our custom CSS
                                  includeCSS("style.css")   
                              ), 
                              
                              leafletOutput("map", width = "100%", height = "95vh"),
                              
                              
                              #Floating panel 
                              absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
                                            draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
                                            width = 330, height = "auto",
                                            
                                            h2("Select symptom"),
                                            
                                            selectInput("symptom", "Select Symptom", c("Chills",
                                                                                       "Cough", "Diarrhoea",
                                                                                       "Fatigue",
                                                                                       "Headache",
                                                                                       "Loss of smell and taste",
                                                                                       "Muscle ache",
                                                                                       "Nasal congestion",
                                                                                       "Nausea and vomiting",
                                                                                       "Shortness of breath",
                                                                                       "Sore throat",
                                                                                       "Sputum",
                                                                                       "Temperature")
                                            )
                              
                              )
             
             )

server <- function(input, output) {
    
    filtered_data <- reactive({
        fake_data %>% 
            dplyr::filter(Symptom %in% input$symptom)
    })
    
    output$map <- renderLeaflet({
        
        leaflet() %>%
            addTiles() %>%
            addMarkers(data = filtered_data(), clusterOptions = markerClusterOptions())
        
    })
    
}


# Run the application 
shinyApp(ui = ui, server = server)

这是我得到的错误:

Error in parse(file, keep.source = FALSE, srcfile = src, encoding = enc) : 
  /Users/myname/Rprojects/training_shiny/app.R:53:1: unexpected symbol
52: 
53: server

然而,如果我排除使用 css,我在运行应用程序时没有问题。如果您从 navbarPage 一直排除行到 lineleafletOutput,让代码从 LeafletOutput 开始,我运行我的应用程序没有问题。当我尝试添加 style.css 时会出现问题。有关如何在没有 css 的情况下运行应用程序的更多详细信息,请点击此链接:integrating leaflet map in RShiny - inputselect by country and symptom

【问题讨论】:

  • 我的样式表和你发的新的一模一样。以防万一,我复制了新的并进行了测试。它仍然可以正常工作,并在我的答案中为我提供了第一张图片。如果ui 对话框位于图表的中间,我喜欢它会消失。据我所知,应该没有问题。我的 RStudio 是 1.1.453 版。这应该不是问题。
  • 我的 R Studio 是 1.3.156。我不相信这是因为 R/RStudio 版本。
  • 无论如何,将为此开一个新问题。

标签: css r shiny


【解决方案1】:

试试这个 整个代码,它使用你的样式表作为 style2.css

library(shiny)
library(tidyverse)
library(leaflet)
library(leaflet.extras)

fake_data <- read.csv("https://raw.githubusercontent.com/gabrielburcea/stackoverflow_fake_data/master/gather_divided.csv")

# Define UI for application that draws a histogram
ui <- fluidPage(

  navbarPage("SARS-Covid-19 Symptom Mapper",
             div(class = "outer",
              tabPanel("Interactive map",
                div(class = "outer",
                    tags$head(
                      # Include our custom CSS
                      #includeCSS("style.css")
                      tags$link(rel = "stylesheet", type = "text/css", href = "style2.css")
                    ),

                    leafletOutput("map", width = "100%", height = "95vh"),

                    #Floating panel
                    absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
                                  draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
                                  width = 330, height = "auto",

                                  h2("Select symptom"),

                                  selectInput("symptom", "Select Symptom", c("Chills",
                                                                             "Cough", "Diarrhoea",
                                                                             "Fatigue",
                                                                             "Headache",
                                                                             "Loss of smell and taste",
                                                                             "Muscle ache",
                                                                             "Nasal congestion",
                                                                             "Nausea and vomiting",
                                                                             "Shortness of breath",
                                                                             "Sore throat",
                                                                             "Sputum",
                                                                             "Temperature")
                                  )

                    )

                )
              )))
  )

server <- function(input, output) {
  filtered_data <- reactive({
    fake_data %>%
      dplyr::filter(Symptom %in% input$symptom)
  })
  
  output$map <- renderLeaflet({
    
    leaflet() %>%
      addTiles() %>%
      addMarkers(data = filtered_data(), clusterOptions = markerClusterOptions())
    
  })
  
}

###  Run the application
shinyApp(ui = ui, server = server)

以下输出与您的样式表有关:

这没有你的样式表:

【讨论】:

  • 您是否仍然遇到同样的错误?我没有收到任何错误。请确保将 style.css 文件保存在工作目录下名为“www”的文件夹中。
  • 是的,我得到了同样的错误。你能用我的 GitHub 上的 style.css 文件检查运行吗 -github.com/gabrielburcea/stackoverflow_fake_data/blob/master/…
  • 我在 www 文件夹中有 style.css 另外,在代码 'tags$head(tags$link(rel = "stylesheet", type = "text/css", href = "style .css"))' 我有另一个括号,后跟一个昏迷。
  • 请看上面答案中的图片。不知道为什么你有错误,我没有。
  • 非常感谢您的努力。可能它会帮助我发布您运行的整个代码。我相信要么某处缺少昏迷,要么增加了断路器。不确定,,,
猜你喜欢
  • 2021-04-16
  • 2020-12-08
  • 1970-01-01
  • 2016-06-22
  • 2020-05-23
  • 2020-08-13
  • 2016-09-02
  • 2021-03-29
  • 1970-01-01
相关资源
最近更新 更多