【问题标题】:Create reactive selectInput - flexdashboard with Shiny in Rmarkdown在 Rmarkdown 中使用 Shiny 创建响应式 selectInput - flexdashboard
【发布时间】:2020-12-09 12:38:54
【问题描述】:

我是第一次使用 Shiney。我想创建一个反应式小部件,其中包含我研究感兴趣的城市的名称。选择城市时,渲染图必须改变。

这是我导入的 csv:

```{r}
    Data20 = read_delim("SAC55.csv", delim = ",")
    tail(Data20)
```
   `DIAS `    `SANTOAMARO ` ` CACHOEIRA ` `CDA ` `SAUBARA `   SFC
  <date>             <dbl>         <dbl>  <dbl>      <dbl> <dbl>
1 2020-08-10           690           333    466         47   668
2 2020-08-11           695           335    470         47   676
3 2020-08-12           717           350    483         48   687
4 2020-08-13           719           356    500         48   701
5 2020-08-14           736           370    507         49   708
6 2020-08-15           763           377    527         49   713


```{r}
colnames(Data20) <- gsub(" ","",colnames(Data20))

```
DIAS       SANTOAMARO CACHOEIRA   CDA SAUBARA   SFC
  <date>          <dbl>     <dbl> <dbl>   <dbl> <dbl>
1 2020-08-10        690       333   466      47   668
2 2020-08-11        695       335   470      47   676
3 2020-08-12        717       350   483      48   687
4 2020-08-13        719       356   500      48   701
5 2020-08-14        736       370   507      49   708
6 2020-08-15        763       377   527      49   713

这就是我尝试创建反应式 selectImput 的方式

Data20 = read_delim("SAC55.csv", delim = ",")
colnames(Data20) <- gsub(" ","",colnames(Data20))
Data20$DIAS = as.Date(Data20$DIAS)



  ```{r}

ui = shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
selectInput(inputId = "Cidades", 
              label = "Cidades",
             choices = c("Santo Amaro" = "SANTOAMARO", "Cachoeira" = "CACHOEIRA", "Cruz das Almas" = "CDA", "Saubara" = "SAUBARA", "SFC" = "SFC"), 
             selected = "SANTOAMARO")),

mainPanel(plotlyOutput("city")))

  ))

server = shinyServer(function(input, output) {

  II <- reactive({
    Data20[ , c("DIAS", input$Cidades)]
     })

output$city = renderPlotly({
   ggplotly(ggplot(data = II(), mapping = aes_string("DIAS", y = input$cidades),  group=1))+
  geom_line(size = 1, col="pink")+
  geom_point(size = 1, col="red" )+
  labs(x = "DIAS") + 
  labs(y = "CASOS")+
  scale_x_date(NULL, date_labels = "%d, %b", date_breaks = "3 days")+
  scale_y_continuous(limits = c(0, 800), breaks = c(0,10,20,40,60,80,100,120,140,180,160,200,220,240,260,280,300,320,340,360,380,400,420,440,460,480,500,520,540,560,580,600,620,640,660,680,700,720,740,760,780,800,900))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(text=element_text(family=" Arial ", size=9, face="bold"))+
  theme(axis.text.x = element_text(family="Arial", face="bold", size=10, angle=45)) 
 })
})
shinyApp(ui = ui,server = server)
```

虽然带有城市名称的小部件出现,但图形已形成。我尝试了几种方法来解决这个问题,但无济于事。 如果有人可以帮助我,我将非常感激。

【问题讨论】:

    标签: r r-markdown flexdashboard


    【解决方案1】:

    Felipe - 看起来这里可能发生了一些事情。

    如果您的II() reactive 表达式是从Data20 中提取日期和城市数据,那么您可能需要(注意硬括号):

    Data20[ , c("DIAS", input$Cidades)] 
    

    否则,它会认为Data20是一个函数。

    此外,样本数据Data20 具有诸如“SAUBARA”之类的列名,但selectInput 选项指的是 `SAUBARA `(带有反引号和空格) - 这些必须相同。

    最后,在ggplotly,如果你想让y轴显示选择的城市,那么你可以试试:

    aes_string(x = "DIAS", y = input$Cidades, group = 1)

    ...和ggplotly 应该在ggplot 对象上调用。

    library(shiny)
    library(ggplot2)
    library(plotly)
    
    ui <- shinyUI(fluidPage(
      sidebarLayout(
        sidebarPanel(
          selectInput(inputId = "Cidades", 
                      label = "Cidades:",
                      choices = c("Santo Amaro" = "SANTOAMARO", "Cachoeira" = "CACHOEIRA", "Cruz das Almas" = "CDA", "Saubara" = "SAUBARA", "SFC" = "SFC"), 
                      selected = "SANTOAMARO")),
        
        mainPanel(plotlyOutput("city")))
    ))
    
    server <- shinyServer(function(input, output) {
      
      II <- reactive({ 
        Data20[ , c("DIAS", input$Cidades)]
      })
      
      output$city = renderPlotly({
        ggplotly(ggplot(data = II(), mapping = aes_string(x = "DIAS", y = input$Cidades, group = 1)) +
          geom_line(size = 1, col="pink")+
          geom_point(size = 1, col="red" )+
          labs(x = "DIAS",  tail(129)) + 
          labs(y = "CASOS")+
          scale_x_date(NULL, date_labels = "%d, %b", date_breaks = "3 days")+
          scale_y_continuous(limits = c(0, 800), breaks = c(0,10,20,40,60,80,100,120,140,180,160,200,220,240,260,280,300,320,340,360,380,400,420,440,460,480,500,520,540,560,580,600,620,640,660,680,700,720,740,760,780,800,900))+
          theme(plot.title = element_text(hjust = 0.5))+
          theme(text=element_text(family=" Arial ", size=9, face="bold"))+
          theme(axis.text.x = element_text(family="Arial", face="bold", size=10, angle=45)))
      })
      
    })
    
    shinyApp(ui, server)
    

    【讨论】:

    • 嗨,本,感谢您的回复。我希望我对 Date20 城市和日期的反应性表达。我进行了建议的更正,但是未呈现图形并出现以下错误消息:Listening on 127.0.0.1:5530 错误:无法对不存在的列进行子集化。 x 列 DIASSANTOAMARO 不存在。
    • 我认为这是因为您的 csv 文件为您提供的列名与上面示例中的列名不同。列名需要与您的数据匹配。阅读您的 csv 文件后,将确保您的列名没有空格。您是否使用上面的测试数据尝试了我的答案,看看它是否有效?
    • read_delim的代码行之后,您可以删除列名中的所有训练空格:colnames(Data20) &lt;- gsub(" ","",colnames(Data20)) ...然后,您不需要反引号和空格与您的列名。这有帮助吗?
    • 不幸的是,这并没有解决它,即使应用了更正。这次出现了一个新错误:Error in aes_string: argument is missing, with no default [No stack trace available]
    • 您是否使用您提供的示例数据而不是您的 csv 文件中的示例数据来尝试我的示例代码?当我使用您的示例数据和我在答案中发布的代码时,它运行良好。
    猜你喜欢
    • 2018-02-09
    • 2017-10-27
    • 2020-01-23
    • 2021-05-17
    • 2020-07-12
    • 2019-09-25
    • 2021-04-26
    • 2018-10-16
    • 1970-01-01
    相关资源
    最近更新 更多