R-我尝试将输入连接到图形输出中,但我总是会出现错误



我很抱歉,以防万一问题听起来太基本了,但是我已经尝试了一段时间了,但仍然行不通。(我看到这里已经有一个类似的问题,但是在尝试实施建议的内容后,我仍然收到错误(。因此,如果有人可以更详细地向我解释我在代码的示例中做错了什么。

我有以下数据集:

year <- c(2000, 2002, 2006, 2000, 2004, 2010, 2010, 2011, 2020, 2006)
prices <- c(100, 200, 300, 120, 240, 400, 430, 490, 700, 650)
colors1 <- c("red", "red", "blue", "green","blue", "red", "blue", "green", "green", "red")
size <- c("s", "m", "l", "xl", "l", "s", "m", "xl", "l","m")
city <- c("NY","LA", "DC","NY","LA", "DC","NY","LA", "DC", "NY")
delivery <- c(3, 7, 3, 10, 20, 5, 10, 2, 12,4)
df <- data.frame(year, prices, colors1,size,city, delivery, stringsAsFactors = FALSE)
df$vatprice<- df$prices * 1.2

这是我的数据集的结构。基本上看起来像这样。

   year prices colors1 size city delivery vatprices
1  2000    100     red    s   NY        3       120
2  2002    200     red    m   LA        7       240
3  2006    300    blue    l   DC        3       360
4  2000    120   green   xl   NY       10       144
5  2004    240    blue    l   LA       20       288
6  2010    400     red    s   DC        5       480
7  2010    430    blue    m   NY       10       516
8  2011    490   green   xl   LA        2       588
9  2020    700   green    l   DC       12       840
10 2006    650     red    m   NY        4       780

现在我想做的我想创建一个闪亮的小应用程序,该应用显示以下内容:

在X轴上多年(您可以看到我的岁月是描述的,但我希望我仍然可以将它们设置为一个范围(

在y轴上:价格或vatprice

所有这些数据应为按颜色,大小或城市进行分类

ui <- basicPage(
  sidebarPanel(
    sliderInput("range", "Year:",min = 1990, max = 2040, value = c(2000,2020)),textOutput("SliderText"),
    selectInput("yaxis", "Y Variable", names(df[,c(2,7)]), selected = names(df)[[2]]),
    radioButtons(inputId = "cat", label = "Categorized by:", names(df[,c(3:5)]))
  ), # closing of the sidebar panel
  plotOutput("scatter")
)

# server 
server <- shinyServer(function(input, output){
  my_range <- reactive({
    cbind(input$range[1],input$range[2])
  })
  output$SliderText <- renderText({my_range()})
  output$scatter <- renderPlot({
    ggplot(df, aes(df$year, df$prices)) + 
   geom_point(aes(color = factor(df$size))) + labs(title = "Overview",
                  color = "Legend") + xlab("Year") + ylab("Price") + theme_minimal()
  })})

shinyApp(ui = ui, server = server)

因此,当我运行程序时,我基本上已经获得了整个UI和图本身。但是我必须以某种方式将DF数据集与服务器连接起来,以使我的闪亮应用程序交互式。

所以我的问题是:我是否必须为每个列中创建一个反应性变量?还是我应该创建一个反应性数据集?有人可以告诉我背后的逻辑,因为我仍然感到迷失。

我试图像这里这样做:如何将输入连接到图形输出但是仍然有一个错误 - 因此我将其删除并仅保留了我的基本UI和服务器功能,因为它们没有显示任何错误。

首先,我们需要根据用户输入的内容来获得年度范围和列名的最终DF。然后,我们需要使用!!sym将这些变量传递给ggplot,因为它们将以字符串形式为字符串,即prices将是"prices"而不是prices

library(dplyr)
library(ggplot2)
server <- shinyServer(function(input, output){
  my_range <- reactive({
    cbind(input$range[1],input$range[2])
  })
  df_final <- reactive({
    filter(df, between(year,input$range[1],input$range[2])) %>% 
    select(year,input$yaxis,input$cat) 
  })
  output$SliderText <- renderText({my_range()})
  #Check if filter and select work correctly and as we wanted
  observe(print(str(df_final()))) 
  output$scatter <- renderPlot({
    ggplot(df_final(), aes(year, !!sym(input$yaxis))) + 
      geom_point(aes(color = factor(!!sym(input$cat)))) + labs(title = "Overview",
                                                      color = "Legend") + xlab("Year") + ylab("Price") + theme_minimal()
  })})

shinyApp(ui = ui, server = server)

我提出了另一个答案,我做了几个更改,首先是我从基本教程中启发了自己:

https://shiny.rstudio.com/tutorial/written-tutorial/lesson4/

其次我使用AES_STRING如下:

将字符字符串转换为r/ggplot2中的函数参数的最佳方法?

在我看来,我的所作所为更自然。其次,我在服务器内进行了选择,我不知道它是否有效,但我认为这很清楚。其次,您的cbind data.frame呼叫是一个坏主意,您强迫每个变量是相同的类型,而不是您想要的。

year <- c(2000, 2002, 2006, 2000, 2004, 2010, 2010, 2011, 2020, 2006)
Price <- c(100, 200, 300, 120, 240, 400, 430, 490, 700, 650)
colors1 <- c("red", "red", "blue", "green","blue", "red", "blue", "green", "green", "red")
size <- c("s", "m", "l", "xl", "l", "s", "m", "xl", "l","m")
city <- c("NY","LA", "DC","NY","LA", "DC","NY","LA", "DC", "NY")
delivery <- c(3, 7, 3, 10, 20, 5, 10, 2, 12,4)
df <- data.frame(year,
                 Price,
                 colors1,
                 size, city,
                 delivery)
df$Vatprice <- df$Price * 1.2
str(df)
library(shiny)
library(ggplot2)
ui <- fluidPage(
  sidebarPanel(
    sliderInput("range",
                "Year:", min = 1990,
                max = 2040, value = c(2000, 2020)),
    textOutput("SliderText"),
    selectInput("yaxis",
                "Y Variable",
                names(df[,c(2,7)]),
                selected = names(df)[[2]]),
    radioButtons(inputId = "cat",
                 label = "Categorized by:",
                 names(df[,3:5]))
  ),
  mainPanel(
    plotOutput("scatter")
  )
)
server <- function(input, output){
  output$scatter <- renderPlot({
    select_row_year <- df$year %in% input$range[1]:input$range[2]
    y_indices <- which(colnames(df) == input$yaxis)
    color <- which(colnames(df) == input$cat)
    ggplot(df[select_row_year, ], aes_string(x = "year",
                                             y = colnames(df)[y_indices],
                                             color = colnames(df)[color])) + 
      geom_point() +
      labs(title = "Overview", color = "Legend") +
      xlab("Year") + ylab(input$yaxis) + theme_minimal()
  })
  }

我希望它对您有帮助。我使你的传奇y轴交互。

编辑1:

是您正在寻找的类似的东西:

ui <- fluidPage(
  sidebarPanel(
    sliderInput("range",
                "Year:", min = 1990,
                max = 2040, value = c(2000, 2020)),
    textOutput("SliderText"),
    selectInput("yaxis",
                "Y Variable",
                names(df[,c(2,7)]),
                selected = names(df)[[2]]),
    radioButtons(inputId = "cat",
                 label = "Categorized by:",
                 names(df[,3:5])),
    checkboxGroupInput(inputId = "city",
                       label = "No city choice",
                       choices = NULL,
                       selected = NULL)
  ),
  mainPanel(
    plotOutput("scatter")
  )
)
server <- function(input, output, session){
  observe({
    if(input$cat  == "city"){
     updateCheckboxGroupInput(session=session, inputId = "city",
                             label = "Select city",
                             choices = levels(df$city),
                             selected = NULL)
    }
  })
  output$scatter <- renderPlot({
    if(length(input$city) != 0){
      select_row <- df$year %in% input$range[1]:input$range[2] & df$city %in% input$city
    }else{
      select_row <- df$year %in% input$range[1]:input$range[2]
    }
    y_indices <- which(colnames(df) == input$yaxis)
    color <- which(colnames(df) == input$cat)
    ggplot(df[select_row, ], aes_string(x = "year",
                                             y = colnames(df)[y_indices],
                                             color = colnames(df)[color])) + 
      geom_point() +
      labs(title = "Overview", color = "Legend") +
      xlab("Year") + ylab(input$yaxis) + theme_minimal()
  })
  }
shinyApp(ui = ui, server = server)

最新更新