我想从RStudio中选择Shiny中定义的选项时插入表格和图形。当选择选项“选择所有属性”时,我想在同一页面上显示Table1和Graph1。如果我按“排除产生小于L且大于S的属性”选项,则仅显示Table2和Graph2。生成表格和图形的代码在闪亮代码之后。
发光代码
library (shiny)
ui <- fluidPage (
titlePanel (title = h1 ("Model for the formation of agricultural property clusters", align = "center")),
sidebarLayout (
sidebarPanel (
h2 ("Cluster generation"),
radioButtons ("filter1", h3 ("Potential biogas productions"),
choices = list ("Select all properties" = 1,
"Exclude properties that produce less than L and more than S" = 2),
selected = 1),
),
mainPanel (
textOutput ("nclusters"),
textOutput ("abran"),
textOutput ("bio")
)))
# Define server logic required to draw a histogram
server <- function (input, output, session) {
}
# Run the application
shinyApp (ui = ui, server = server)
用于生成表和图形的代码1
###Table 1
dados_tabela <- Reduce(merge, list(dadoscompleto7, abrangencia, soma_biogas))
dados_tabela <- dados_tabela[order(dados_tabela$cluster, as.numeric(dados_tabela$Propriedade)),]
df_table2 <- aggregate(. ~ cluster + Abrangência_metros + Potencial_Biogás_m³, dados_tabela[,c(1,2,6,7)], toString)
kable(df_table2[order(df_table2$cluster), c(4,1,2,3)], align = "c", row.names = FALSE) %>%
kable_styling(full_width = FALSE)
### Graph 1
suppressPackageStartupMessages(library(ggplot2))
df<-as.data.frame(centro_massa)
colnames(df) <-c("Latitude", "Longitude", "cluster")
g<-ggplot(data=dadoscompleto7, aes(x=Longitude, y=Latitude, color=factor(clusters))) + geom_point(aes(x=Longitude, y=Latitude), size = 4)
Centro_View<- g + geom_text(data=dadoscompleto7, mapping=aes(x=eval(Longitude), y=eval(Latitude), label=Biogas), size=3, hjust=-0.1)+ geom_point(data=df, mapping=aes(Longitude, Latitude), color= "green", size=4) + geom_text(data=df, mapping = aes(x=Longitude, y=Latitude, label = 1:k), color = "black", size = 4)
print(Centro_View + ggtitle("Gráfico de dispersão") + theme(plot.title = element_text(hjust = 0.5)))
用于生成表和图形的代码2
###Table 2
dados_tabela <- Reduce(merge, list(dadoscompleto8, abrangencia, soma_biogas))
dados_tabela <- dados_tabela[order(dados_tabela$cluster, as.numeric(dados_tabela$Propriedade)),]
df_table2 <- aggregate(. ~ cluster + Abrangência_metros + Potencial_Biogás_m³, dados_tabela[,c(1,2,6,7)], toString)
kable(df_table2[order(df_table2$cluster), c(4,1,2,3)], align = "c", row.names = FALSE) %>%
kable_styling(full_width = FALSE)
### Graph 2
suppressPackageStartupMessages(library(ggplot2))
df<-as.data.frame(centro_massa)
colnames(df) <-c("Latitude", "Longitude", "cluster")
g<-ggplot(data=dadoscompleto7, aes(x=Longitude, y=Latitude, color=factor(clusters))) + geom_point(aes(x=Longitude, y=Latitude), size = 4)
Centro_View<- g + geom_text(data=dadoscompleto8, mapping=aes(x=eval(Longitude), y=eval(Latitude), label=Biogas), size=3, hjust=-0.1)+ geom_point(data=df, mapping=aes(Longitude, Latitude), color= "green", size=4) + geom_text(data=df, mapping = aes(x=Longitude, y=Latitude, label = 1:k), color = "black", size = 4)
print(Centro_View + ggtitle("Gráfico de dispersão") + theme(plot.title = element_text(hjust = 0.5))
谢谢!
这里是简化版本,可以适应您自己的使用。这适用于上一个问题的示例数据。