我自己绘制直方图的函数在 Shiny 中不起作用
My own function to plot a histogram doesn't work in Shiny
我写了一个函数,它在一个简单的脚本中运行得很好,但在 shiny 中却不行。此函数创建直方图。
如果我尝试在没有 Shiny 的情况下绘制直方图,它会起作用。
val <- c(2.1490626,3.7928443,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
df1 <- data.frame(val)
df1$type <- "Type 1"
val <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)
df2 <- data.frame(val)
df2$type <- "Type 2"
df3 <- rbind(df1, df2)
bins = 10
min = NULL
max = NULL
histogram_function <- function(data, bins, min, max){
hist <- ggplot(data, aes(val, fill=type)) +
geom_histogram(position = "identity", colour = "grey40", bins = bins) +
ggtitle("Here must be a title") +
xlab("Values") +
ylab("Frequency") +
facet_grid(type ~ .)
return(hist)
}
histogram_function(data=df3, bins=bins, min=min, max=max)
但是,如果我尝试在 Shiny 中做同样的事情,我无法得到任何直方图。
请注意,我已将数据保存到反应函数中,因为在我最初的闪亮应用程序中,我加载了一个包含更多信息的文件(但格式与此处相同)
我在 Shiny 中的代码。
library(shiny)
library(ggplot2)
histogram_function <- function(data, bins, min, max){
hist <- ggplot(data, aes(val, fill=type)) +
geom_histogram(position = "identity", colour = "grey40", bins = bins) +
ggtitle("Here must be a title") +
xlab("Values") +
ylab("Frequency") +
facet_grid(type ~ .)
return(hist)
}
ui <- fluidPage(
# Application title
titlePanel("My shiny app"),
sidebarLayout(
mainPanel(
plotOutput("histogram")
)
)
)
server <- function(input, output) {
dataframe <- reactive({
val <- c(2.1490626,3.7928443,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
df1 <- data.frame(val)
df1$type <- "Type 1"
val <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)
df2 <- data.frame(val)
df2$type <- "Type 2"
df3 <- rbind(df1, df2)
return(df3)
})
draw_histogram <- reactive({
hist <- histogram_function(data = dataframe(), bins = 10, min = NULL, max = NULL)
return(hist)
})
output$histogram <- renderPlot({
h <- draw_histogram()
return(h)
})
}
#Run the app
shinyApp(ui = ui, server = server)
我在 Shiny 中做错了什么吗?不知道怎么回事
提前致谢,
此致
您在需要定义 sidebarPanel
和 mainPanel
的地方使用 sidebarLayout
。在您的示例中,您没有定义导致错误的 sidebarPanel
。您可以使用空 sidebarPanel
或仅使用 fluidPage
更改 ui
,然后就可以了:
ui <- fluidPage(
# Application title
titlePanel("My shiny app"),
sidebarLayout(
sidebarPanel(),
mainPanel(
plotOutput("histogram")
)
)
)
我写了一个函数,它在一个简单的脚本中运行得很好,但在 shiny 中却不行。此函数创建直方图。
如果我尝试在没有 Shiny 的情况下绘制直方图,它会起作用。
val <- c(2.1490626,3.7928443,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
df1 <- data.frame(val)
df1$type <- "Type 1"
val <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)
df2 <- data.frame(val)
df2$type <- "Type 2"
df3 <- rbind(df1, df2)
bins = 10
min = NULL
max = NULL
histogram_function <- function(data, bins, min, max){
hist <- ggplot(data, aes(val, fill=type)) +
geom_histogram(position = "identity", colour = "grey40", bins = bins) +
ggtitle("Here must be a title") +
xlab("Values") +
ylab("Frequency") +
facet_grid(type ~ .)
return(hist)
}
histogram_function(data=df3, bins=bins, min=min, max=max)
但是,如果我尝试在 Shiny 中做同样的事情,我无法得到任何直方图。
请注意,我已将数据保存到反应函数中,因为在我最初的闪亮应用程序中,我加载了一个包含更多信息的文件(但格式与此处相同)
我在 Shiny 中的代码。
library(shiny)
library(ggplot2)
histogram_function <- function(data, bins, min, max){
hist <- ggplot(data, aes(val, fill=type)) +
geom_histogram(position = "identity", colour = "grey40", bins = bins) +
ggtitle("Here must be a title") +
xlab("Values") +
ylab("Frequency") +
facet_grid(type ~ .)
return(hist)
}
ui <- fluidPage(
# Application title
titlePanel("My shiny app"),
sidebarLayout(
mainPanel(
plotOutput("histogram")
)
)
)
server <- function(input, output) {
dataframe <- reactive({
val <- c(2.1490626,3.7928443,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
df1 <- data.frame(val)
df1$type <- "Type 1"
val <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)
df2 <- data.frame(val)
df2$type <- "Type 2"
df3 <- rbind(df1, df2)
return(df3)
})
draw_histogram <- reactive({
hist <- histogram_function(data = dataframe(), bins = 10, min = NULL, max = NULL)
return(hist)
})
output$histogram <- renderPlot({
h <- draw_histogram()
return(h)
})
}
#Run the app
shinyApp(ui = ui, server = server)
我在 Shiny 中做错了什么吗?不知道怎么回事
提前致谢,
此致
您在需要定义 sidebarPanel
和 mainPanel
的地方使用 sidebarLayout
。在您的示例中,您没有定义导致错误的 sidebarPanel
。您可以使用空 sidebarPanel
或仅使用 fluidPage
更改 ui
,然后就可以了:
ui <- fluidPage(
# Application title
titlePanel("My shiny app"),
sidebarLayout(
sidebarPanel(),
mainPanel(
plotOutput("histogram")
)
)
)