我目前正在為一個專案創建一個 RShiny 網站,該專案為美國的謀殺、襲擊和強奸資料建模。到目前為止,我已經取得了重大進展,但我在兩個問題上遇到了麻煩。
我正在嘗試將 for 或 while 回圈實作為我的專案的一部分,但在運行應用程式時無法顯示輸出。我希望 for/while 回圈以文本的形式回傳也共享低/中/高犯罪風險的州“其他具有低/中/高犯罪率的州是 State1、State2、State3 , 等等”。這取決于用戶在側邊欄下拉框中選擇的狀態。我沒有為此撰寫代碼。我知道有 100% 更簡單的方法可以在沒有回圈結構的情況下做到這一點,但我的專案要求我使用 for 或 while 回圈。
我如何能夠在選項卡面板上組合多個輸出,以便顯示兩個輸出。我希望只有一個“國家有多安全?” 選項卡,但是每當我嘗試將兩者結合起來時,選項卡上都不會顯示任何輸出。
library(shiny)
library(tidyverse)
library(ggplot2)
# Define UI for application that draws the graphs
ui <- fluidPage(
# Application title
titlePanel("Rate of Crime in United States"),
p("Use the variable selector to refine your search!"),
# Sidebar with widgets adjust server output
sidebarLayout(
sidebarPanel(
checkboxGroupInput("display_var",
"Which Crime/s to Display?",
choices = c("Murder" = "Murder",
"Assault" = "Assault",
"Rape" = "Rape"),
selected = "Murder"
),
sliderInput("bins",
"Number of bins (valid for Histogram chart only):",
min = 5,
max = 10,
value = 7
),
selectInput(
"search", "How safe is this state?", choices = (attributes(USArrests)$row.names), selected = NULL)
),
# Create the tabs
mainPanel(
tabsetPanel(
tabPanel("Bar Plot", plotOutput("barplot")),
tabPanel("Histogram", plotOutput("distPlot")),
tabPanel("How Safe is the State?", textOutput("howsafe")),
tabPanel("How Safe is the State?pt2", textOutput("howsafe2"))
)
)
))
# Define server logic required to draw graphs
server <- function(input, output) {
output$barplot <- renderPlot({
marchoice <- req(input$display_var)
sd <- setdiff(names(USArrests),marchoice)
temp_df <- USArrests
temp_df[,sd] <- 0
counts <- temp_df$Murder temp_df$Assault temp_df$Rape
names(counts) <- rownames(temp_df)
barplot(counts,
main="Aggregate Sum of Crime in the United States",
xlab="State",
ylab="Frequency",las=2,col=rgb(0.2,0.4,0.6,0.6))
})
#Transform numeric variables into categorical
CategorisedMAR <- cut(USArrests$Murder USArrests$Assault USArrests$Rape, breaks=c(0,150,300,450), labels = c("Low", "Medium", "High"))
names(CategorisedMAR) <- attributes(USArrests)$row.names
st <- reactive(input$search)
output$howsafe <- renderText({
#if-else statement to state risk based on state selection
if (CategorisedMAR[[input$search]] == "Low") {
paste0(st(), " has a low rate of crime")
} else if (CategorisedMAR[[input$search]] == "Medium") {
paste0( st()," has a mid-level rate of crime")
} else if (CategorisedMAR[[input$search]] == "High") {
paste0( st(), " has a high rate of crime")
}
})
output$howsafe2 <- renderText({
for(value in CategorisedMAR) {
if(value == "Low") {
print(value)}
}
})
output$distPlot <- renderPlot({
#Create new data based on the selection
USArrests2 <-
USArrests %>%
select(!!input$display_var) %>%
mutate(cumulative_frequency = rowSums(across(where(is.numeric))))
# Create plot - Show the cumulative frequency
ggplot(USArrests2, aes(cumulative_frequency)) ggtitle("Histogram of Variable Frequency")
theme(plot.title = element_text(hjust = 0.5))
geom_histogram(bins = input$bins,
fill = rgb(0.2,0.4,0.6,0.6),
colour = "grey30")
#Create a new label based on what has been selected
xlab(str_c(input$display_var, collapse = " & "))
theme_minimal()
})
}
# Run the application
shinyApp(ui = ui, server = server)
這是我迄今為止構建的代碼。為了清楚起見,我還附上了一張照片,詳細說明了我想要實作的目標。 “代表我正在努力實作的目標
我已經花了無數天的時間來解決這個問題,任何幫助將不勝感激。提前致謝!
uj5u.com熱心網友回復:
也許這會做
library(shiny)
library(tidyverse)
library(ggplot2)
library(DT)
# Define UI for application that draws the graphs
ui <- fluidPage(
# Application title
titlePanel("Rate of Crime in United States"),
p("Use the variable selector to refine your search!"),
# Sidebar with widgets adjust server output
sidebarLayout(
sidebarPanel(
checkboxGroupInput("display_var",
"Which Crime/s to Display?",
choices = c("Murder" = "Murder",
"Assault" = "Assault",
"Rape" = "Rape"),
selected = "Murder"
),
sliderInput("bins",
"Number of bins (valid for Histogram chart only):",
min = 5,
max = 10,
value = 7
),
selectInput( "search", "How safe is this state?", choices = (attributes(USArrests)$row.names), selected = NULL)
),
# Create the tabs
mainPanel(
tabsetPanel(
tabPanel("Bar Plot", plotOutput("barplot")),
tabPanel("Histogram", plotOutput("distPlot")),
tabPanel("How Safe is the State?", textOutput("howsafe"), br(),br(), textOutput("howsafe2"))
)
)
))
# Define server logic required to draw graphs
server <- function(input, output) {
output$barplot <- renderPlot({
marchoice <- req(input$display_var)
sd <- setdiff(names(USArrests),marchoice)
temp_df <- USArrests
temp_df[,sd] <- 0
counts <- temp_df$Murder temp_df$Assault temp_df$Rape
names(counts) <- rownames(temp_df)
barplot(counts,
main="Aggregate Sum of Crime in the United States",
xlab="State",
ylab="Frequency",las=2,col=rgb(0.2,0.4,0.6,0.6))
})
#Transform numeric variables into categorical
labels = c("Low", "Medium", "High")
CategorisedMAR <- cut(USArrests$Murder USArrests$Assault USArrests$Rape, breaks=c(0,150,300,450), labels = labels)
names(CategorisedMAR) <- attributes(USArrests)$row.names
st <- reactive(input$search)
output$howsafe <- renderText({
#if-else statement to state risk based on state selection
if (CategorisedMAR[[input$search]] == "Low") {
paste0(st(), " has a low rate of crime")
} else if (CategorisedMAR[[input$search]] == "Medium") {
paste0( st()," has a mid-level rate of crime")
} else if (CategorisedMAR[[input$search]] == "High") {
paste0( st(), " has a high rate of crime")
}
})
output$howsafe2 <- renderText({
myvalue = CategorisedMAR[[input$search]]
n <- length(CategorisedMAR)
list_states <- c()
for (i in 1:n){
if (CategorisedMAR[[i]]==myvalue) list_states <- c(list_states,names(CategorisedMAR)[i])
}
mylist <- list_states[! list_states %in% st()]
a <- paste0(c("The following states also had", tolower(labels[myvalue]),"rate of crime:"), collapse=" ")
b <- paste0(paste(c(a,mylist), collapse=", "),".")
aa <- gsub(":,",":", b)
paste(aa)
})
output$distPlot <- renderPlot({
#Create new data based on the selection
USArrests2 <-
USArrests %>%
dplyr::select(input$display_var) %>%
mutate(cumulative_frequency = rowSums(across(where(is.numeric))))
# Create plot - Show the cumulative frequency
ggplot(USArrests2, aes(cumulative_frequency)) ggtitle("Histogram of Variable Frequency")
theme(plot.title = element_text(hjust = 0.5))
geom_histogram(bins = input$bins,
fill = rgb(0.2,0.4,0.6,0.6),
colour = "grey30")
#Create a new label based on what has been selected
xlab(str_c(input$display_var, collapse = " & "))
theme_minimal()
})
}
# Run the application
shinyApp(ui = ui, server = server)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/338051.html
