【发布时间】:2019-09-29 11:17:24
【问题描述】:
我正在尝试使用列名为“release_year”的数据框作为滑块输入,因此无论我选择什么时间线,比如 1960 年到 1970 年,我只能在散点图上看到来自该特定时间线的数据。现在我的滑块表现得很奇怪,除了移动几个点之外什么也没做。我怎样才能解决这个问题?像这样的东西 https://shiny.rstudio.com/gallery/movie-explorer.html 你看到年份发布滑块了吗?我想要那个确切的东西。
- 附加我的数据集的图像。
-
[df] (https://imgur.com/NZWuWtF)
structure(list(id = c(135397L, 135397L, 76341L, 76341L, 262500L, 140607L, 140607L, 140607L, 168259L, 168259L), budget = c(150000000L, 150000000L, 150000000L, 150000000L, 110000000L, 200000000L, 200000000L, 200000000L, 190000000L, 190000000L), revenue = c(1513528810, 1513528810, 378436354, 378436354, 295238201, 2068178225, 2068178225, 2068178225, 1506249360, 1506249360), title = structure(c(3L, 3L, 4L, 4L, 2L, 5L, 5L, 5L, 1L, 1L), .Label = c("Furious 7", "Insurgent", "Jurassic World", "Mad Max: Fury Road", "Star Wars: The Force Awakens" ), class = "factor"), homepage = structure(c(2L, 2L, 3L, 3L, 5L, 4L, 4L, 4L, 1L, 1L), .Label = c("http://www.furious7.com/", "http://www.jurassicworld.com/", "http://www.madmaxmovie.com/", "http://www.starwars.com/films/star-wars-episode-vii", "http://www.thedivergentseries.movie/#insurgent" ), class = "factor"), director = structure(c(1L, 1L, 2L, 2L, 5L, 3L, 3L, 3L, 4L, 4L), .Label = c("Colin Trevorrow", "George Miller", "J.J. Abrams", "James Wan", "Robert Schwentke"), class = "factor"), runtime = c(124L, 124L, 120L, 120L, 119L, 136L, 136L, 136L, 137L, 137L), vote_average = c(6.5, 6.5, 7.1, 7.1, 6.3, 7.5, 7.5, 7.5, 7.3, 7.3), release_year = c(2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L), genre = structure(c(1L, 2L, 1L, 2L, 2L, 1L, 2L, 4L, 1L, 3L), .Label = c("Action", "Adventure", "Crime", "Fantasy"), class = "factor"), breakeven = c(1363528810, 1363528810, 228436354, 228436354, 185238201, 1868178225, 1868178225, 1868178225, 1316249360, 1316249360), AerageVotesCat = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("Excellent", "Good"), class = "factor")), row.names = c(NA, 10L), class = "data.frame" [需要控制数据显示的滑块] (https://imgur.com/vdvEJWN)
我几乎花了一周的时间尝试一切。我似乎无法解决这个问题。我知道问题出在我的反应支架周围的某个地方?但是作为新手,我不知道该传递什么。
UI:
library(gifski)
library(gganimate)
library(dplyr)
library(DT)
library(shinythemes)
library(scales)
library(shiny)
library(ggplot2)
library(plotly)
df <- read.csv("C:/Users/XXX/Downloads/movie1.csv")
n_total <- nrow(df)
ui <- fluidPage(theme = shinytheme("united"),
titlePanel("Movie browser, 1960 - 2014", windowTitle = "Movies"),
# Sidebar layout with a input and output definitions
sidebarLayout(
# Inputs
sidebarPanel(
wellPanel(
# Select variable for y-axis
selectInput(inputId = "y",
label = h4("Y-axis:"),
choices =c("Budget" ="budget", "Revenue" = "revenue", "Runtime" = "runtime", "Vote average" = "vote_average", "Year released" = "release_year", "Profit" = "breakeven"),
selected = "revenue"),
sliderInput("SectorTime", h4("Select a time period:"), min = 1960, max = 2015,
value = c(1960,2015), step = 5),
textInput("Director", h4("Director name contains (e.g., Miyazaki)")),
numericInput(inputId = "n",
label = h4("Sample size:"),
value = 30,
min = 1, max = n_total,
step = 1),
radioButtons(inputId = "filetype",
label = "Select filetype:",
choices = c("csv", "tsv"),
selected = "csv"),
# Select variables to download
checkboxGroupInput(inputId = "selected_var",
label = "Select variables:",
choices = names(df),
selected = c("title"))
),
# Outputs
mainPanel(
tabsetPanel(
tabPanel(h4("PLOT"), plotlyOutput("plot"),
tabPanel(h4("DATA"), DT::dataTableOutput(outputId = "moviestable", width = 500)
)
)
)
SERVER:
# Define server function required to create the scatterplot
server <- function(input, output) {
dataset <- reactive({
df[sample(nrow(df), input$SectorTime),]
})
# Create scatterplot object the plotOutput function is expecting
output$plot <- renderPlotly({
point <- format_format(big.mark = " ", decimal.mark = ",", scientific = FALSE)
p <- ggplot(data = dataset(), aes_string(x = input$x, y = input$y, col = input$z)) +
geom_point(alpha = input$alpha, size = 2, shape = 1) + theme_minimal() +
ggtitle("Scatter plot between various variables") +scale_x_continuous(labels = point) + scale_y_continuous(labels = point)
p + theme(axis.text.x = element_text(angle = 30))
})
output$moviestable <- DT::renderDataTable({
movies_sample <- df %>%
sample_n(input$n) %>%
select(title: AerageVotesCat)
DT::datatable(data = movies_sample,
options = list(pageLength = 30),
rownames = FALSE)
})
}
# Create the Shiny app object
shinyApp(ui = ui, server = server)
这显然不是我的完整代码。我知道问题出在此处。非常感谢您的帮助。
【问题讨论】:
-
Hasan,请对如何以有用的方式提出问题进行一些研究。 StackOverflow 的 R page 的索引页特别指出 “使用
dput()表示数据并使用library()调用指定所有非基础包。不要为数据或代码嵌入图片,而是使用缩进代码块。”。所以请使用dput(head(x)),因为很多人(包括我)不会将数据图像转录成要测试的东西。其他参考:stackoverflow.com/questions/5963269、stackoverflow.com/help/mcve 和 stackoverflow.com/tags/r/info。 -
... 但我的第一个猜测是让您阅读
sample的第二个参数的作用。它确实不猜测您想要对特定列的数据行进行采样以在其中包含一个或多个值。也许用df[ df$yr >= input$SectorTime[1] & df$yr <= input$SectorTime[2],,drop=FALSE]替换sample(nrow(df),input$SectorTime),]。 -
嗨。道歉。我正在努力整理我的数据。您提到的这段代码给了我一个空白输出。所以只是一个白屏
-
它可能会出现白屏,因为我不知道您的年份列的名称(现在您提供了数据,我知道它是
$release_year)。我曾使用$yr作为占位符。你改了还是失败了? -
太棒了!作品!过去一周一直坚持这一点。谢谢!
标签: r ggplot2 shiny plotly reactive