我试图在一个闪亮的应用程序中获得一个简单的条形图。
global.R
library(shiny)
library(ggplot2)
library(plotly)
dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23)
)
ui.R
ui <- navbarPage(title = "My Shiny App",
tabPanel(title = "App",
pageWithSidebar(
headerPanel("Shiny App"),
sidebarPanel(
actionButton(inputId = "clicks",
label = "Click me")
),
mainPanel("Main Panel",
plotlyOutput("norm"),
actionButton("renorm", "Resample")
)
)
),
tabPanel(title = "Tab2",
plotOutput("unif"),
actionButton("reunif", "Resample")
),
tabPanel(title = "Tab3",
plotOutput("chisq"),
actionButton("rechisq", "Resample")
)
)
server.R
server <- function(input, output) {
output$norm <- renderPlotly({
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(stat="identity") +
guides(fill=FALSE)
})
这给了我一个如下闪亮应用程序中的情节。我有三个问题。
答案 0 :(得分:0)
以下小修改完成了所有这三件事。
剧情中有一个错误。这是一个有点神秘的解决方法。
出于某种原因,情节上覆盖了guide(fill=FALSE)
,我不知道为什么真的,情节文档不是很广泛。这是另一个
摆脱与情节一起工作的指南的方法。
无论如何,这是代码
library(shiny)
library(ggplot2)
library(plotly)
dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23)
)
ui <- navbarPage(title = "My Shiny App",
tabPanel(title = "App",
pageWithSidebar(
headerPanel("Shiny App"),
sidebarPanel(
actionButton(inputId = "clicks",
label = "Click me")
),
mainPanel("Main Panel",
plotlyOutput("norm"),
actionButton("renorm", "Resample")
)
)
),
tabPanel(title = "Tab2",
plotOutput("unif"),
actionButton("reunif", "Resample")
),
tabPanel(title = "Tab3",
plotOutput("chisq"),
actionButton("rechisq", "Resample")
)
)
server <- function(input, output) {
output$norm <- renderPlotly({
# reorder the levels
oldlevels <- levels(dat$time)
neworder <- order(dat$total_bill,decreasing=T)
dat$time <- factor(dat$time,levels=oldlevels[neworder])
# get the plotly into a temp var
gplt <- ggplotly(ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(stat="identity") +
theme(legend.position="none"))
for (i in 1:length(gplt$x$data)){
gplt$x$data[[i]]$text <- c(gplt$x$data[[i]]$text, "")
}
gplt
})
}
shinyApp(ui,server)
得到以下特性: