AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77655340
Accepted
tassones
tassones
Asked: 2023-12-14 00:59:22 +0800 CST2023-12-14 00:59:22 +0800 CST 2023-12-14 00:59:22 +0800 CST

在散点图变量名称中允许空格 R闪亮

  • 772

我正在 Rstudio 中开发一个闪亮的应用程序。该应用程序创建两个图 - 时间序列和散点图。绘制的变量是Water Temperature和Air Temperature。时间序列图可以毫无问题地处理变量名称中的空格,但是,与变量名称关联的散点图出现错误。

错误是:

Warning: Error in parse: <text>:1:7: unexpected symbol
1: Water Temperature
          ^

或者

Warning: Error in parse: <text>:1:5: unexpected symbol
1: Air Temperature
        ^

我怀疑这些错误与水/空气和温度之间的空间有关。保留空格而不是用下划线或其他空格填充符替换它至关重要。制作散点图时如何保持变量名称中的空格?

library(tidyverse)
library(shiny)
library(shinydashboard)

# Create example dataset
set.seed(123)

dat <- data.frame(
  Time = rep(seq(1,10,1), times = 2),
  Site = rep(c('A', 'B'), each = 10),
  Water_Temperature = round(rnorm(20, mean = 20, sd = 5)),
  Air_Temperature = round(rnorm(20, mean = 25, sd = 3))
)

dat <- dat %>%
  rename(`Water Temperature` = Water_Temperature,
         `Air Temperature` = Air_Temperature)

dat_long <- dat %>%
  pivot_longer(cols = c("Water Temperature","Air Temperature"),
               names_to = 'Variable',
               values_to = 'Value') %>%
  arrange(Site, Time)

# Define user interface for time series and scatterplot
ui.R <- dashboardPage(
  dashboardHeader(title = "Example", titleWidth = 350),
  dashboardSidebar(
    tags$head(
      tags$style(
        HTML("
          .sidebar-footer {
            text-align: center;
          }
          .sidebar-logo {
            display: flex;
            align-items: center;
            justify-content: center;
          }
          .sidebar-menu a span {
            font-size: 24px;
          }
        ")
      )
    ),
    sidebarMenu(
      menuItem("Time Series", tabName = "time_series", icon = icon("chart-line")),
      menuItem("Scatterplot", tabName = "scatter_plot", icon = icon("chart-line"))
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(
        tabName = "time_series",
        fluidRow(
          box(
            title = "Time Series",
            status = "primary",
            solidHeader = TRUE,
            width = 2,
            selectInput("siteInput", "Select Site", choices = unique(dat_long$Site)),
            selectInput("variableInput", "Select Variable", choices = unique(dat_long$Variable))
          ),
          box(
            title = "Plot",
            status = "primary",
            solidHeader = TRUE,
            width = 10,
            plotOutput("timeSeriesPlot", height = "700px")
          )
        )
      ),
      tabItem(
        tabName = "scatter_plot",
        fluidRow(
          box(
            title = "Scatterplot",
            status = "primary",
            solidHeader = TRUE,
            width = 2,
            selectInput("siteInput2", "Select Site", choices = unique(dat_long$Site)),
            selectInput("xAxisInput", "Select X-axis Variable:", choices = unique(dat_long$Variable)),
            selectInput("yAxisInput", "Select Y-axis Variable:", choices = unique(dat_long$Variable))
          ),
          box(
            title = "Plot",
            status = "primary",
            solidHeader = TRUE,
            width = 10,
            plotOutput("scatterPlot", height = "700px")
          )
        )
      ),
      tabItem(
        tabName = "site_info",
        fluidPage(
          fluidRow(
            selectInput("siteInput3", "Select Site", choices = unique(dat_long$Site)),
            column(4, dataTableOutput('table'))
          )
        )
      )
    )
  )
)

# Define server
server.R <- function(input, output) {
  output$timeSeriesPlot <- renderPlot({
    filteredData <- dat_long %>%
      filter(Site == input$siteInput, Variable == input$variableInput)
    
    variableName <- unique(filteredData$Variable)
    yLabel <- ifelse(variableName == "Water Temperature", expression(Water~Temperature~(degree*C)),
                     ifelse(variableName == "Air Temperature", expression(Air~Temperature~(degree*C))))
    
    ggplot(filteredData, aes(x = Time, y = Value)) +
      geom_line() +
      geom_point() +
      scale_x_continuous(breaks = seq(1,10,1)) +
      labs(x = "", y = yLabel,
           title = paste('Site:', input$siteInput)) +
      theme_bw()
  })
  
  output$scatterPlot <- renderPlot({
    filteredData2 <- dat_long %>%
      filter(Site == input$siteInput2)
    
    # Create a reactive label to display the units for the selected variable on the x and y axes
    xLabel <- reactive({
      switch(input$xAxisInput[1],
             `Air Temperature` = expression(Air~Temperature~(degree*C)),
             `Water Temperature` = expression(Water~Temperature~(degree*C)))
    })
    
    yLabel <- reactive({
      switch(input$yAxisInput[1],
             `Air Temperature` = expression(Air~Temperature~(degree*C)),
             `Water Temperature` = expression(Water~Temperature~(degree*C)))
    })
    
    ggplot(filteredData2, aes_string(x = input$xAxisInput, y = input$yAxisInput)) +
      geom_point() +
      labs(x = xLabel(), y = yLabel(),
           title = paste('Site:', input$siteInput2)) +
      theme_bw()
  })
}

# Run the application
shinyApp(ui = ui.R, server = server.R)

时间序列图有效 在此输入图像描述 散点图无效 在此输入图像描述

  • 1 1 个回答
  • 11 Views

1 个回答

  • Voted
  1. Best Answer
    2023-12-14T01:24:00+08:002023-12-14T01:24:00+08:00

    有趣的是aes_string不能处理空格。但是,您可以通过切换到.data代词来解决此问题,该代词已aes_string被弃用,这也是推荐的方法(请参阅 参考资料?aes_string),即使用

    ggplot(filteredData2, 
        aes(x = .data[[input$xAxisInput]], y = .data[[input$yAxisInput]])
    ) +
    

    此外,您正在尝试根据没有变量的长数据制作散点图Water/Air Temperature。对于散点图,您必须使用宽数据集,即使用

    filteredData2 <- dat %>%
          filter(Site == input$siteInput2)
    

    在此输入图像描述

    • 1

相关问题

  • 将复制活动的序列号添加到 Blob

  • Packer 动态源重复工件

  • 选择每组连续 1 的行

  • 图形 API 调用列表 subscribedSkus 状态权限不足,但已授予权限

  • 根据列值创建单独的 DF 的函数

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve