任务:我需要使用 ShinyApp 和给定的数据集创建一个反应式仪表板。我需要创建 3 个输入和至少 1 个 ggplot 作为输出。 数据集:有 14 列(User.ID(显示用户 ID)、First.Stay(是/否);第 3:12 列是酒吧、俱乐部、餐厅...等地点的名称、总体(评级)、评级(以口头形式,即“好”,“优秀”,“差”)
用户ID | 首先入住 | 酒吧 | 俱乐部 | ... | 整体 | 评分 |
---|---|---|---|---|---|---|
用户1 | 没有 | 3.16 | 1.21 | ... | 3.57 | 非常好 |
用户2 | 没有 | 2.15 | 4.8 | ... | 4.2 | 优秀 |
用户980 | 是的 | 3.1 | 1.1 | ... | 2.3 | 不好 |
(我跳过了 977 个用户,但他们在那里,这是一个很大的数据集)
问题:应用程序正在运行,它显示我指定的轴(x&y),但不显示任何数据。这是下面的代码:
rm(list=ls())
library(shiny)
library(shinythemes)
library(ggplot2)
library(dplyr)
library(tidyr)
my_data <- read.csv("tripadvisor_reviews.csv", header = TRUE, sep = ";")
any(is.na(my_data))
my_places <- my_data %>%
pivot_longer(cols = 3:12, names_to= "places", values_to = "rating")
#the dataset is too wide, I was recommended to pivot it to long
# Define UI for application that draws a histogram
ui <- fluidPage(theme = shinytheme("cyborg"),
h1("XX"), #header
h2("Made by vopuidi"), #name
p("xx"), #paragraph/text
br(), #adds some space between the text & Layout
#Layout
sidebarLayout(
sidebarPanel(
#Input 1: Numeric rating
sliderInput("numeric_rating", "Select the desired numeric rating", min = 0, max = 5, value = c(0,5), step = 0.5),
#Input 2: checkboxes for desired places
checkboxGroupInput("desired_places", "Select all desired amusements",
choices = unique(places)),
selected = c("Clubs")),
#Input 3: for first-time travellers
checkboxInput("first_timers", "Display reviews by people with first stay", FALSE),
),
mainPanel(#Output 1: the scatterplot
plotOutput("myscatter")
))
# Define server logic required to draw a histogram
server <- function(input, output) {
#Output 1:
output$myscatter = renderPlot({
p = my_places %>%
filter(rating >= input$numeric_rating[1] & Rating <= input$numeric_rating[2])%>%
filter(places %in% input$desired_places)%>%
ggplot(aes(x= places, y = rating , fill = Overall))+
geom_bar(position="dodge", stat="identity")
print(p)})
}
# Run the application
shinyApp(ui = ui, server = server)
出了什么问题?我是否错过了某些数据规范,并且 R 正在寻找错误的位置?
第二个问题是这样的:#Input 2:所需位置的复选框 checkboxGroupInput("desired_places", "选择所有想要的娱乐项目", 选择=独特(地点)), 选定 = c("俱乐部")),
我相信问题 2 导致了问题 1。
checkboxInput 中的
choices = unique(places)
正在做什么? places
未在代码片段中的任何位置定义。您可能指的是 unique(my_places$places)
,即 places
数据集中 my_places
列的唯一值。
只要输入元素由于问题 #2 而无法正常工作,服务器块中的第二个
filter()
调用就不会具有任何可使用的合理值,并且永远不会与数据集中的任何内容匹配。 ggplot
获取一个空数据集作为输入并准确绘制该数据集。