r - 在 Shiny Application 中过滤数据时,长度为 1 的字符向量除第一个元素外的所有元素都将被忽略错误

标签 r dataframe shiny modal-dialog

我有以下 Shiny 的应用程序:

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()),]
    })


  })

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

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

但是,当我选择 Texas 时,我只会得到他的:

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
      ))
    })

enter image description here

希望这对您有所帮助!

关于r - 在 Shiny Application 中过滤数据时,长度为 1 的字符向量除第一个元素外的所有元素都将被忽略错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48383010/

相关文章:

r - 列与基线的差异

r - 如何使用 Plotly 摆脱默认工具提示?

c++ - 通过从 C/C++ 替换 R 控制台中的输出来创建进度更新

r - 在 ggplot2 中使用 Squash_axis 时出现错误 "NAs are not allowed in subscripted assignments",数据集没有 NA 值

r - 删除R中具有聚合组的重复项

python - 根据日期合并 Pandas Dataframes

r - 日期的 slider 输入

R Shiny : Dynamic Row of Action Buttons

r - 根据变量是否在列表中对数据进行子集化

python - 迭代使用 "locals": Python 生成的多个数据帧