在 Shiny 应用程序中过滤数据时,长度为 1 的字符向量除了第一个元素之外的所有元素都将被忽略错误

2024-05-14

我有以下闪亮的应用程序:

library(shiny)
library(rhandsontable)
library(shinydashboard)
library(ggplot2)
library(dplyr)

setwd("C:/Users/Marc/Dropbox/PROJECTEN/Lopend/shiny_interactive_graph")

tweets <- data.frame(
  city = c("new york", "texas", "texas"),
  tweet = c("Test1", "Test", "tst")
)


shinyApp(
  ui = dashboardPage(
    dashboardHeader(
      title = "Tweetminer",
      titleWidth = 350
    ),
    dashboardSidebar(
      width = 350,
      sidebarMenu(
        menuItem("Menu Item")
      )
    ),
    dashboardBody(
      fluidRow(
        tabBox(
          tabPanel("Set tweets2", 
                   plotOutput('plot',
                              brush = brushOpts(
                                id = "plot1_brush"
                              )),
                   h4("Selected States"),
                   verbatimTextOutput("select_states"),
                   h4("Selected States' Tweets"),
                   verbatimTextOutput("tweets")
          )
        )
      )
    )
  ),
  server = function(input, output) { 

    output$plot <- renderPlot({

      all_states <- map_data("state") 
      # Add more states to the lists if you want
      states_positive  <-c("new york")
      states_negative  <- c("texas")
      # Plot results
      ggplot(all_states, aes(x=long, y=lat, group = group)) +
        geom_polygon(fill="grey", colour = "white") +
        geom_polygon(fill="green", data = filter(all_states, region %in% states_positive)) +
        geom_polygon(fill="red", data = filter(all_states, region %in% states_negative))

    })

    selected_points <- reactiveVal()

    observeEvent(input$plot1_brush,{
      all_states <- map_data("state")
      selected_points( brushedPoints(all_states, input$plot1_brush))
    })

    observeEvent(selected_points(), {
      showModal(modalDialog(
        title = "Important message",
        tweets[(tweets$city %in% brushed_states()),],
        easyClose = TRUE
      ))
    })

    output$brush_info <- renderPrint({
      all_states <- map_data("state")
      brushedPoints(all_states, input$plot1_brush)
    })

    #get states from brushed coordinates
    brushed_states <- reactive({
      all_states <- map_data("state")
      brushed <- brushedPoints(all_states, input$plot1_brush)
      unique(brushed$region)
    })

    #this is to show the selected states
    output$select_states <- renderText({
      brushed_states()
    })

    output$tweets <- renderPrint({
      tweets[(tweets$city %in% brushed_states()),]
    })


  })

这基本上允许在地图上选择地图,然后获得包含所有相关推文的弹出窗口。我通过这一行获取相关推文:

tweets[(tweets$city %in% brushed_states()),]

然而,当我选择德克萨斯州时,我只得到他的:

texas Test

虽然我期望:

texas Test
texas tst

我认为这与以下错误有关:

Warning in charToRaw(enc2utf8(text)) :
argument should be a character vector of length 1
all but the first element will be ignored

我有点迷失了,这里到底发生了什么......对导致这个错误的原因有什么想法吗?


它不起作用的原因是modalDialog需要文本或 html,但您将其传递给data.frame,它不知道如何打印。所以你必须转换你的data.frame首先到可打印版本。这是一个示例实现:

    observeEvent(selected_points(), {
      my_tweets <- tweets[(tweets$city %in% brushed_states()),]
      showModal(modalDialog(
        title = "Important message",
        HTML(paste(apply(my_tweets,1,function(x) {paste(x,collapse=': ')}),collapse='<br>')),
        easyClose = TRUE
      ))
    })

希望这可以帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Shiny 应用程序中过滤数据时,长度为 1 的字符向量除了第一个元素之外的所有元素都将被忽略错误 的相关文章

  • kableExtra 中的 row_spec() 函数不会在 html 输出中创建水平线

    我想在 kableextra 表中的某一行下方添加一条水平线 row spec 函数的参数 hline after 应该在行下方添加水平线 row spec 文档 https www rdocumentation org packages
  • 如何在for循环中引用变量?

    我正在循环访问不同的 data tables 和 data table 中的变量 但我在引用内部变量时遇到问题for loop dt1 lt data table a1 c 1 2 3 a2 c 4 5 2 dt2 lt data tabl
  • 在 R 中创建一个运行计数变量?

    我有一个足球比赛结果的数据集 我希望通过创建一组类似于世界足球 Elo 公式的运行评级来学习 R 我遇到了麻烦 在 Excel 中看似简单的事情在 R 中并不完全直观 例如 4270 个观察中的前 15 个具有必要的变量 date t 1
  • 如何用外部图像填充地图边界?

    我正在创建一张带有州边界的巴西地图 这可以直接使用ggplot2 and geom sf 然而 这一次 我不想用数据填充每个状态的颜色 而是想用外部图像 png 填充每个状态的边界 类似于this https online olivet e
  • 重复测量引导统计数据,按多个因素分组

    我有一个看起来像这样的数据框 但显然还有更多行等 df lt data frame id c 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 cond c A A B B A A B B A A B B A A B B co
  • R foreach问题(某些进程返回NULL)

    我遇到了问题foreach我正在 R 中使用的程序的一部分 该程序用于运行不同参数的模拟 然后将结果返回到单个列表 然后用于生成报告 当并非所有分配的模拟运行都在报告上实际可见时 就会出现问题 从各方面来看 似乎只有分配的运行的一个子集实际
  • 我无法下载 R 中的 reshape2 包 [关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我在尝试安装 R 包时收到此响应 gt installed packages reshape2 Package LibPath V
  • 使用 purrr 迭代替换数据帧列中的字符串

    我想用purrr使用以下命令在数据框列上迭代运行多个字符串替换gsub 功能 这是示例数据框 df lt data frame Year 2019 Text c rep a aa 5 rep a bb 3 rep a cc 2 gt df
  • 使用 pracma::findpeaks 识别持续峰值

    我的语法有问题peakpat内的选项findpeaks内的函数pramcaR 包 v 2 1 1 我使用的是 R 3 4 3 x64 Windows 我希望该函数能够识别可能有两个重复值的峰值 并且我相信该选项peakpat这就是我能做到的
  • 如何使用 usmap 标记数字而不是名称?

    我知道 usmap 有一个选项label in plot usmap 我想标记一些数字 而不是状态名称 我想 usmap 中应该有与州质心坐标相关的数据 但我不知道如何找到它 如果我能得到 坐标然后我可以用它来标记数字geom text 这
  • 绘制点之间的所有线

    我有以下 R 代码 x lt c 0 01848598 0 08052353 0 06741172 0 11652034 y lt c 0 4177541 0 4042247 0 3964025 0 4074685 d lt data fr
  • R 中的快速 QR 分解

    我有大量矩阵 需要对其执行 QR 分解并存储生成的 Q 矩阵 进行归一化 以便 R 矩阵在其对角线上具有正数 除了使用之外还有其他方法吗qr 功能 这是工作示例 system time Parameters for the matrix t
  • Angular UI 模式的范围问题

    我无法理解 使用角度 UI 模式的范围 虽然这里不是很明显 但我已经正确设置了模块和所有内容 据我所知 但这些代码示例尤其是我发现错误的地方 index html 其中重要部分 div class btn group div
  • 对于多列,将当前行和上一行的差异附加到新列

    对于 df 中的每一列 我想从前一行 row n 1 row n 中减去当前行 但我遇到了困难 我的代码如下 usr bin python3 from pandas datareader import data import pandas
  • R独特的列或行与NA无可比拟

    有谁知道如果incomparables的论证unique or duplicated 曾经被实施过incomparables FALSE 也许我不明白它应该如何工作 无论如何 我正在寻找一个巧妙的解决方案 以仅保留与另一列相同的唯一列 或行
  • 使用 R 选择第一个非 NA 值

    df lt data frame ID c 1 1 1 2 3 3 3 test c NA 5 5 6 4 NA 7 3 NA 10 9 我想创建一个名为 value 的变量 它是每个单独 ID 测试的第一个非 NA 值 对于只有NA的个体
  • pandas 替换多个值

    以下是示例数据框 gt gt gt df pd DataFrame a 1 1 1 2 2 b 11 22 33 44 55 gt gt gt df a b 0 1 11 1 1 22 2 1 33 3 2 44 4 3 55 现在我想根据
  • 使用 Shiny 发布平行坐标图表时出现“错误:路径[1]="”:没有这样的文件或目录”

    我有一个似乎很常见但我还没有找到解决方案的问题 当尝试使用 rCharts Parcoords 发布 Web 应用程序时 出现以下错误 错误 路径 1 没有这样的文件或目录 奇怪的是 该应用程序在我的笔记本电脑上运行得很好 下面是我正在使用
  • 在 r 中的 group_by 之后建模后取消列表列的嵌套

    我想对所有组进行线性回归group by 将模型系数保存在列表列中 然后使用 unnest 扩展列表列 这里我用的是mtcars以数据集为例 注 我想用do here becausebroom tidy 不适用于所有型号 mtcars gt
  • 使用基于正则表达式的部分匹配来选择 Pandas 数据帧的子数据帧

    我有一个 Pandas 数据框 它有两列 一列 进程参数 列 包含字符串 另一列 值 列 包含相应的浮点值 我需要过滤出部分匹配列 过程参数 中的一组键的子数据帧 并提取与这些键匹配的数据帧的两列 df pd DataFrame Proce

随机推荐