Friends, could you help me with the following question: I would like to adjust my executable code with shiny. The shiny code is created, but I want to know how to link the executable code with the shiny. IF you have suggestions for improving shiny feel free.
I want to show for example, when pressing option 1_1 and option 2_1 show table 1 and graph 1. When pressing option 1_1 and option 2_2 show table 2 and graph 2. In this case both for B1 (Button 1). For button B2, I intend to change the graph. However, if you help me with these examples that I mentioned, I believe I managed to do the rest.
library(shiny)
library(kableExtra)
library(ggplot2)
library(kableExtra)
#database
df<-structure(list(Latitude = c(-23.8, -23.8, -23.9, -24.5, -23.4, -25.4), Longitude = c(-49.6, -49.6, -49.6, 49.3, 49.3, 49.2), Waste = c(526, 350, 526, 600, 240,750)), class = "data.frame", row.names = c(NA, -6L))
#cluster
d<-dist(df)
fit.average<-hclust(d,method="average")
clusters<-cutree(fit.average,k=3)
df$cluster<-clusters
#table1
kable(df[order(df$cluster, as.numeric(df$Waste)),c(4,2,3,1)], align = "c", row.names = FALSE) %>%
kable_styling(full_width = FALSE) %>%
column_spec(1, bold = TRUE) %>%
collapse_rows(columns = 3:4, valign = "middle")
#plot 1
g<-ggplot(data=df, aes(x=Longitude, y=Latitude, color=factor(clusters))) + geom_point(aes(x=Longitude, y=Latitude), size = 4)
Centro_View<- g + geom_text(data=df, mapping=aes(x=eval(Longitude), y=eval(Latitude), label=Waste), size=3, hjust=-0.1)
print(Centro_View + ggtitle("Scatter Plot") + theme(plot.title = element_text(hjust = 0.5)))
#table2
kable(df[order(df$cluster, as.numeric(df$Waste)),c(2,4,1,3)], align = "c", row.names = FALSE) %>%
kable_styling(full_width = FALSE) %>%
column_spec(1, bold = TRUE) %>%
collapse_rows(columns = 3:4, valign = "middle")
#plot 2
g<-ggplot(data=df, aes(x=Latitude, y=Longitude, color=factor(clusters))) + geom_point(aes(x=Longitude, y=Latitude), size = 4)
Centro_View<- g + geom_text(data=df, mapping=aes(x=eval(Longitude), y=eval(Latitude), label=Waste), size=3, hjust=-0.1)
print(Centro_View + ggtitle("Scatter Plot") + theme(plot.title = element_text(hjust = 0.5)))
library(shiny)
library(ggplot2)
layouts <- expand.grid(rb1 = c("Option1_1", "Option1_2"), rb2 = c("Option2_1", "Option2_2"), rb3 = c("B1", "B2"),
stringsAsFactors = FALSE)
layouts$choice <- c(1:4, 4:1)
shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons("rb1", "Choice 1", c("Option1_1", "Option1_2")),
radioButtons("rb2", "Choice 2", c("Option2_1", "Option2_2"))
),
mainPanel(
fluidRow(
actionButton("btn1", "Button B1"),
actionButton("btn2", "Button B2")
),
tableOutput("tbl"),
plotOutput("plt")
)
)
),
server = function(input, output, session) {
btn <- reactiveVal("B1")
observeEvent(input$btn1, btn("B1"))
observeEvent(input$btn2, btn("B2"))
selection <- reactive({
req(input$rb1, input$rb2)
out <- with(layouts, choice[ rb1 == input$rb1 & rb2 == input$rb2 & rb3 == btn() ])
updateTextInput(session, "txt", value = out)
out
})
output$tbl <- renderTable({
req(selection())
tables[[ selection() ]]
})
output$plt <- renderPlot({
req(selection())
print(plots[[ selection() ]])
})
}
)
Thank you very much!