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

Hen*_*ten 2 r modal-dialog dataframe 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()),]
    })


  })
Run Code Online (Sandbox Code Playgroud)

这基本上可以在地图上选择一个地图,然后弹出所有相关的推文。我通过此行获取相关的推文:

tweets[(tweets$city %in% brushed_states()),]
Run Code Online (Sandbox Code Playgroud)

但是,当我选择德克萨斯州时,我只会得到他的:

texas Test
Run Code Online (Sandbox Code Playgroud)

虽然我期望:

texas Test
texas tst
Run Code Online (Sandbox Code Playgroud)

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

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

我有点迷茫,这里到底发生了什么...。什么原因导致此错误?

Flo*_*ian 6

它不起作用的原因是modalDialog期望文本或html,但是您将其传递给a 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
      ))
    })
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

希望这可以帮助!