[英]Create reactive selectInput - flexdashboard with Shiny in Rmarkdown
i'm using shiney for the first time.我是第一次使用 Shiney。 I want to create a reactive widget containing the names of the cities of interest for my study.
我想创建一个反应式小部件,其中包含我研究感兴趣的城市的名称。 When selecting cities, the rendered graph must change.
选择城市时,渲染的图表必须改变。
This is my imported csv:这是我进口的 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
that's the way i'm trying to create a reactive selectImput这就是我尝试创建反应性 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)
```
Although the widget with the names of the cities appear the graphics are formed.尽管带有城市名称的小部件出现,但图形已形成。 I've tried several ways to resolve this to no avail.
我尝试了几种方法来解决这个问题,但无济于事。 If anyone can help me I will be very grateful.
如果有人可以帮助我,我将不胜感激。
Felipe - looks like there may be a few things going on here. Felipe - 看起来这里可能发生了一些事情。
If your II()
reactive
expression is to pull the date and city data from Data20
, then you may want (note the hard brackets):如果您的
II()
reactive
表达式是从Data20
中提取日期和城市数据,那么您可能需要(注意硬括号):
Data20[ , c("DIAS", input$Cidades)]
otherwise, it will think Data20
is a function.否则,它会认为
Data20
是 function。
Also, the sample data Data20
has column names such as "SAUBARA", but the selectInput
choices refer to `SAUBARA ` (with back ticks and a space) - these need to be the same.此外,样本数据
Data20
具有列名称,例如“SAUBARA”,但selectInput
选项引用的是 `SAUBARA `(带有反引号和空格) - 这些必须相同。
Finally, in ggplotly
, if you want the y-axis to display the selected city, then you could try:最后,在
ggplotly
中,如果您希望 y 轴显示所选城市,那么您可以尝试:
aes_string(x = "DIAS", y = input$Cidades, group = 1)
...and ggplotly
should be called on a ggplot
object. ...并且应该在
ggplotly
object 上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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.