使用基于 selectInput 的动态参数将绘图从 rmarkdown 复制到 Shiny 的问题

如何解决使用基于 selectInput 的动态参数将绘图从 rmarkdown 复制到 Shiny 的问题

我在 rmarkdown 中创建了一个绘图,我试图通过使用 shinyselectInPut 中使用 动态参数 重新创建它。由于字符串和非字符串类型,我遇到了一些问题,我想我无法弄清楚。

数据可在:https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/vaccination_data.csv

rmarkdown 代码和绘图:

vaccination_data %>% 
  filter(date == max(date)) %>%  
  slice_max(order_by = total_vaccinations,n = 20) %>% 
   
  ggplot(aes(x = total_vaccinations,y = fct_reorder(location,total_vaccinations),col = continent)) +
  geom_point() +
  geom_errorbarh(height=0,size=1,aes(xmin=total_vaccinations,xmax=0)) +
  scale_x_continuous(labels = unit_format(scale = 1e-6,unit = "M")) +
  geom_vline(xintercept = 0,col = "midnightblue",lty = 2,size = 1) +
  
  theme(
        panel.grid.major = element_blank(),legend.position = "NULL") 

在上面的代码中,我试图将 total_vaccinations 参数设置为动态的闪亮。

enter image description here

server.R:

    output$top_vaccinating_countries <- renderPlot({
        req(input$id_vaccination_top_country)
        
        vaccination_data %>% 
            filter(date == max(date)) %>% 
            slice_max(order_by = input$id_vaccination_top_country,n = 20) %>% 
            
            ggplot(aes(x = input$id_vaccination_top_country,input$id_vaccination_top_country),col = continent)) +
            geom_point() +
            geom_errorbarh(height=0,aes(xmin=as.numeric(!! sym(input$id_vaccination_top_country)),xmax=0)) +
            # scale_x_continuous(labels = unit_format(scale = 1e-6,unit = "M")) +
            geom_vline(xintercept = 0,size = 1) +
            scale_color_tableau() +
            
            theme(
                panel.grid.major = element_blank(),legend.position = "top",legend.direction = "horizontal")  
    })

用户界面:

 column(4,style = "border: 1px solid gray;",selectInput(inputId = "id_vaccination_top_country",label = "Select Vaccination Parameter",choices = c("total_vaccinations","total_vaccinations_per_hundred","people_vaccinated","people_vaccinated_per_hundred"),selected = "total_vaccinations"),plotOutput("top_vaccinating_countries",height = "570px")
                 )

enter image description here

我尝试了 (!! sym(input$)) 的各种组合,但没有奏效。甚至尝试过 varSelectInput 但如果我这样做会产生另一个错误

varSelectInput(
data = (vaccination_data %>% select(total_vaccinations,people_vaccinated))

新用户界面:


library(shiny)
library(shinydashboard)
library(shinythemes)
library(highcharter)
library(streamgraph)
# library(thematic)
# 
# thematic_shiny(font = "auto")

# Define UI for application 
shinyUI(fluidPage(
    theme=shinytheme("lumen"),themeSelector(),navbarPage(
       title = "Covid19 Dashboard",id = "Covid19_Dashboard",tabPanel("Global Cases Status",# Application title
             titlePanel("Global level"),fluidRow(
             style = "border: 1px solid gray;",column(4,plotOutput("top_CFR_countries",height = "650px")),plotOutput("top_testing_countries",height = "650px")
             ),height = "570px")
             )
             
         )
 
    ) # navbarpage      
    ) # fluid page
) # shiny ui

解决方法

有几个地方应该将字符串更改为 symbol 并求值 (!!)

library(shiny)
library(dplyr)
library(readr)
library(ggplot2)
library(ggthemes)
library(forcats)
library(scales)

-ui

# Define UI
vaccination_data <- read_csv("https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/vaccination_data.csv")
ui <- fluidRow(
  style = "border: 1px solid gray;",h3("Vaccination to Cases Correlation Analysis"),column(4,style = "border: 1px solid gray;",selectInput(inputId = "id_vaccination_top_country",label = "Select Vaccination Parameter",choices = c("total_vaccinations","total_vaccinations_per_hundred","people_vaccinated","people_vaccinated_per_hundred"),selected = "total_vaccinations"),plotOutput("top_vaccinating_countries",height = "570px")
  )
)

-服务器

server <- function(input,output) {
  output$top_vaccinating_countries <- renderPlot({
    req(input$id_vaccination_top_country)
    
    vaccination_data %>% 
      filter(date == max(date)) %>% 
      slice_max(order_by = !! rlang::sym(input$id_vaccination_top_country),n = 20) %>% 
      
      ggplot(aes(x = !! rlang::sym(input$id_vaccination_top_country),y = fct_reorder(location,!! rlang::sym(input$id_vaccination_top_country)),col = continent)) +
      geom_point() +
      geom_errorbarh(height=0,size=1,aes(xmin=as.numeric(!! sym(input$id_vaccination_top_country)),xmax=0)) +
       scale_x_continuous(labels = unit_format(scale = 1e-6,unit = "M")) +
      geom_vline(xintercept = 0,col = "midnightblue",lty = 2,size = 1) +
      scale_color_tableau() +
      
      theme(
        panel.grid.major = element_blank(),legend.position = "top",legend.direction = "horizontal")  
  })
  
  
}
  

-运行应用程序

shinyApp(ui = ui,server = server)

-输出

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?