问题
I have the following Shiny Application:
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()),]
})
})
This basically allows to select a map on the map and then get a popup with all the relevant tweets. I fetch the relevant tweets by this line:
tweets[(tweets$city %in% brushed_states()),]
However, when I select Texas I only get his:
texas Test
While I expect:
texas Test
texas tst
I think this has to do with the following error:
Warning in charToRaw(enc2utf8(text)) :
argument should be a character vector of length 1
all but the first element will be ignored
I am kind of lost, what is exactly happening here.... Any thoughts on what is causing this error?
回答1:
The reason it does not work is that the modalDialog
expects text or html, but you are passing it a data.frame
, which it does not know how to print. So you have to convert your data.frame
to a printable version first. Here is an example implementation:
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
))
})
Hope this helps!
来源:https://stackoverflow.com/questions/48383010/character-vector-of-length-1-all-but-the-first-element-will-be-ignored-error-whe