gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:31:31 26 4
gpt4 key购买 nike

我有以下 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com