r-闪亮的应用程序:gpplot基于条件,日期x轴,和值BoxOutput不显示



我的数据帧df:

df<- structure(list(Dead = c(0L, 0L, 0L, 0L, 0L, 1L, 9L, 0L, 0L, 0L
), Case = c(120L, 70L, 50L, 40L, 39L, 20L, 18L, 13L, 9L, 2L), Recovered = c(30L,0L, 18L, 13L, 19L, 
10L, 0L,16L, 0L, 1L), Critical = c(0L, 0L, 0L,                                                                                                                               
0L, 8L, 4L, 0L, 3L, 2L, 0L), Date = c("18/03/2020", "17/03/2020",                                                                                                                                                                    
"16/03/2020", "15/03/2020", "14/03/2020", "13/03/2020", "12/03/2020",                                                                                                                                                                    
"11/03/2020", "10/03/2020", "09/03/2020")), class = "data.frame", row.names = c(NA,                                                                                                                                                                                                                                                    
10L))

我的MWE:

library(shiny)
library(plotly)
library(ggplot2)
df$Date = as.Date(df$Date, format = "%d/%m/%Y")
ui <- fluidPage(
title = 'testing',
sidebarLayout(
sidebarPanel(
helpText(),
selectInput("x", "Choose X-axis data", choices = names(df), selected = "Date"),
selectInput("y", "Choose Y-axis data", choices = names(df)),
# Input: Slider for the number of observations to generate ----
sliderInput("n",
"No. of bins:",
value = 5,
min = 1,
max = 15) ,
),
mainPanel(
tabsetPanel(
tabPanel("ggplot", plotlyOutput("regPlot1")),
tabPanel("default plot", plotOutput("regPlot2")),
tabPanel("Histogram", plotOutput("regPlot3"))
),
fluidRow(
shinydashboard::valueBoxOutput("totalCases",width = 2)
)
)
)
)
server <- function(input, output, session) {
#calculation for box values
total.cases <- sum(df$Case)
## Value1: Total cases ## 
output$totalCases <- renderValueBox({
shinydashboard::valueBox(
formatC(total.cases, format="d", big.mark=','),
paste('Total Cases:',total.cases),
icon = icon("stats",lib='glyphicon'),
color = "purple")
})
Graphcase <- reactive({
Gchoice<-input$x
})
myData <- reactive({
df[, c(input$x, input$y)]
})
#plot
output$regPlot1 <- renderPlotly({
# comment the if and else(block) to make run the code
if(Graphcase=="Date"){
ggplotly(ggplot(data = myData(), 
aes_string(x = input$x, y = input$y)) +
geom_line( color="blue") +
geom_point(shape=21, color="black", fill="gray", size=4) +
theme(axis.text.x = element_text(color="#993333", 
size=10, angle=45,hjust = 1),
axis.text.y = element_text(color="#993333", 
size=10, angle=45,hjust = 1)))
+scale_x_date(date_labels = "%b/%d")
}else{
ggplotly(ggplot(data = myData(), 
aes_string(x = input$x, y = input$y)) +
geom_line( color="blue") +
geom_point(shape=21, color="black", fill="gray", size=4) +
theme(axis.text.x = element_text(color="#993333", 
size=10, angle=45,hjust = 1),
axis.text.y = element_text(color="#993333", 
size=10, angle=45,hjust = 1)))
}
})
# plot2
output$regPlot2 <- renderPlot({
par(mar = c(4, 4, .1, .1)) # margin lines
plot(myData(), data = df)
})
#plot 3
output$regPlot3 <- renderPlotly({
ggplotly(ggplot(data = myData(), aes_string(x = input$x)) +
geom_histogram(color="black", 
fill="blue",
binwidth=input$n)
)
})


}
shinyApp(ui, server)

我的问题有三个部分:

1( 如果您运行代码并将鼠标悬停在图形点上,您会注意到ggplot在x轴上没有显示正确的日期。我放了+scale_x_date(date_labels = "%b/%d"),它解决了这个问题,但是,它破坏了其他数据的图表。换句话说,如果我将x轴更改为数据的任何其他变量,它将无法正确显示。经过搜索,我发现使用if statements可以解决这个问题。因此,我想把一个条件设置为:如果x轴是Date,则图将具有scale_x_date(..(。如果不是,则我将在示例中使用相同的代码,并且如果选择日期,则该条件也将应用于y轴。我添加了图2"默认图",只是为了表明正常的绘图功能即使有日期也能正常工作。我尝试了代码中的条件,但出现了错误。

2( 我很难显示框,因为你可以看到代码显示的值甚至是icom,但没有框。我根据建议使用了名称空间,没有希望。IMHO,我认为这与包有关,正如我注意到的警告一样,有些包是屏蔽命令。!

3( 日期作为数据不能用于计算直方图。当柱状图选项卡打开时,是否可能只显示一个输入字段而不是两个,即输入$x和下拉列表菜单中的日期被排除在外?

任何建议。

为了将来的参考,不要在同一篇文章中问几个问题。StackOverflow不仅可以帮助您,还可以帮助其他人在代码中寻找问题的答案。如果一个帖子和它的答案是同时提出和回答的许多问题,那么很难看出它们是否有用。

回到你的问题:

  • 问题1:你的括号有问题,我在你的帖子中纠正了这个问题
  • 问题2:valueBox只能在dashboardPage中显示,不能在fluidPage中显示。这是程的回答:

    很抱歉,valueBoxOutput和shinydashboard的其他功能仅在与dashboardPage一起使用时可用。

  • 问题3:您可以使用observeupdateSelectInput根据所选的选项卡Panel更改selectInput中的选项。为此,首先需要为tabsetPanel创建id

以下是工作代码:

library(shiny)
library(plotly)
library(ggplot2)
library(shinydashboard)
df <- structure(
list(
Dead = c(0L, 0L, 0L, 0L, 0L, 1L, 9L, 0L, 0L, 0L),
Case = c(120L, 70L, 50L, 40L, 39L, 20L, 18L, 13L, 9L, 2L),
Recovered = c(30L, 0L, 18L, 13L, 19L,
10L, 0L, 16L, 0L, 1L),
Critical = c(0L, 0L, 0L,
0L, 8L, 4L, 0L, 3L, 2L, 0L),
Date = c(
"18/03/2020",
"17/03/2020",
"16/03/2020",
"15/03/2020",
"14/03/2020",
"13/03/2020",
"12/03/2020",
"11/03/2020",
"10/03/2020",
"09/03/2020"
)
),
class = "data.frame",
row.names = c(NA,
10L)
)
df$Date = as.Date(df$Date, format = "%d/%m/%Y")
ui <- fluidPage(
title = 'testing',
sidebarLayout(
sidebarPanel(
helpText(),
selectInput("x", "Choose X-axis data", choices = names(df), selected = "Date"),
uiOutput("second_select"),
# Input: Slider for the number of observations to generate ----
sliderInput("n",
"No. of bins:",
value = 5,
min = 1,
max = 15) ,
),
mainPanel(
tabsetPanel(
id = "tabs",
tabPanel("ggplot", plotlyOutput("regPlot1")),
tabPanel("default plot", plotOutput("regPlot2")),
tabPanel("Histogram", plotlyOutput("regPlot3"))
)
)
)
)
server <- function(input, output, session) {
Graphcase <- reactive({
Gchoice<-input$x
})
myData <- reactive({
df[, c(input$x, input$y)]
})
#plot
output$regPlot1 <- renderPlotly({
req(input$x)
req(input$y)
# comment the if and else(block) to make run the code
if(Graphcase()=="Date"){
ggplotly(ggplot(data = myData(),
aes_string(x = input$x, y = input$y)) +
geom_line( color="blue") +
geom_point(shape=21, color="black", fill="gray", size=4) +
theme(axis.text.x = element_text(color="#993333",
size=10, angle=45,hjust = 1),
axis.text.y = element_text(color="#993333",
size=10, angle=45,hjust = 1)) +
scale_x_date(date_labels = "%b/%d"))
}else{
ggplotly(ggplot(data = myData(),
aes_string(x = input$x, y = input$y)) +
geom_line( color="blue") +
geom_point(shape=21, color="black", fill="gray", size=4) +
theme(axis.text.x = element_text(color="#993333",
size=10, angle=45,hjust = 1),
axis.text.y = element_text(color="#993333",
size=10, angle=45,hjust = 1)))
}
})
# plot2
output$regPlot2 <- renderPlot({
par(mar = c(4, 4, .1, .1)) # margin lines
plot(myData(), data = df)
})
#plot 3
observe({
if(input$tabs == "Histogram"){
updateSelectInput(session = session,
inputId = "x",
choices = names(subset(df, select = -c(Date))))
output$second_select <- renderUI(NULL)
}
else {
updateSelectInput(session = session,
inputId = "x",
choices = names(df),
selected = "Date")
output$second_select <- renderUI({
selectInput("y", "Choose Y-axis data", choices = names(df))
})
}
})
output$regPlot3 <- renderPlotly({
ggplotly(ggplot(data = myData(), aes_string(x = input$x)) +
geom_histogram(color="black",
fill="blue",
binwidth=input$n)
)
})
}
shinyApp(ui, server)

编辑:如果要在单击"直方图"选项卡时删除第二个selectInput,则必须使用uiOutputrenderUI。此外,为了防止由于前两个图中缺少输入而导致错误,在开始计算这两个图之前,请使用req()向Shiny发出信号,告知您需要这两个输入。因此,我修改了上面的代码。

相关内容

  • 没有找到相关文章

最新更新