参见:https://shiny.rstudio.com/tutorial/written-tutorial/lesson5/

    shiny 执行的顺序是,首先当适应shinyapp 允许命令后,会在全局环境中加载相关的内容。

    而只有server 中的render 函数,才会在使用用户第一访问shiny 或通过交互按钮切换显示内容时才会被反复执行。

    在运行官方提供的代码前,先解决一下先前的无法通过获得交互选项结果修改输出表格的问题。

    非常的神奇,无论如何尝试,只有在使用swich 根据结果的判断来进行返回值的情况下,才能够正确的进行显示:

    1. # Define server logic ----
    2. server <- function(input, output) {
    3. output$test_df <- renderDataTable({
    4. data <- switch(input$select,
    5. "Herb info" = test_list$`Herb info`,
    6. "ingredient info" = test_list$`ingredient info`)
    7. # a <- input$select
    8. # data <- test_list$a
    9. # data <- test_list$`input$select`
    10. #data
    11. })
    12. output$test_txt <- renderText({
    13. paste0("output is ", input$select)
    14. })
    15. }

    07. 在shiny 中使用脚本和包 - 图1

    看来以后绑定switch 了。

    可是,如果是批量的进行赋值或者是连续性内容进行判断赋值呢?switch 有没有替代品呢?

    我们还可以玩的更高级,将参数以列表的形式传递给函数:

    1. server <- function(input, output) {
    2. output$map <- renderPlot({
    3. args <- switch(input$var,
    4. "Percent White" = list(counties$white, "darkgreen", "% White"),
    5. "Percent Black" = list(counties$black, "black", "% Black"),
    6. "Percent Hispanic" = list(counties$hispanic, "darkorange", "% Hispanic"),
    7. "Percent Asian" = list(counties$asian, "darkviolet", "% Asian"))
    8. args$min <- input$range[1]
    9. args$max <- input$range[2]
    10. do.call(percent_map, args)
    11. })
    12. }

    全部内容如下:

    1. ##################################################
    2. ## Project: Rescue the Princess
    3. ## File name: app.R
    4. ## Date: Fri Jun 25 16:47:13 2021
    5. ## Author: Peng
    6. ## R_Version: R version 4.0.2 (2020-06-22)
    7. ## R_Studio_Version: 1.4.1106
    8. ## Platform Version: Windows 10 x64 (build 19042)
    9. ##################################################
    10. # # pre-loaded data&library -----------------------------------------------
    11. # Load packages ----
    12. library(shiny)
    13. library(maps)
    14. library(mapproj)
    15. # Load data ----
    16. counties <- readRDS("./app-census/data/counties.rds")
    17. # Source helper functions -----
    18. source("helpers.R")
    19. # User interface ----
    20. ui <- fluidPage(
    21. titlePanel("censusVis"),
    22. sidebarLayout(
    23. sidebarPanel(
    24. helpText("Create demographic maps with
    25. information from the 2010 US Census."),
    26. selectInput("var",
    27. label = "Choose a variable to display",
    28. choices = c("Percent White", "Percent Black",
    29. "Percent Hispanic", "Percent Asian"),
    30. selected = "Percent White"),
    31. sliderInput("range",
    32. label = "Range of interest:",
    33. min = 0, max = 100, value = c(0, 100))
    34. ),
    35. mainPanel(plotOutput("map"))
    36. )
    37. )
    38. # Server logic ----
    39. server <- function(input, output) {
    40. output$map <- renderPlot({
    41. data <- switch(input$var,
    42. "Percent White" = counties$white,
    43. "Percent Black" = counties$black,
    44. "Percent Hispanic" = counties$hispanic,
    45. "Percent Asian" = counties$asian)
    46. color <- switch(input$var,
    47. "Percent White" = "darkgreen",
    48. "Percent Black" = '#fdcc8a',
    49. "Percent Hispanic" = '#fc8d59',
    50. "Percent Asian" = '#d7301f')
    51. legend.title <- switch(input$var,
    52. "Percent White" = "% White",
    53. "Percent Black" = '% Black',
    54. "Percent Hispanic" = '% Hispanic',
    55. "Percent Asian" = '% Asian')
    56. percent_map(var = data, color = color, legend.title = legend.title, input$range[1], input$range[2])
    57. })
    58. }
    59. # Run app ----
    60. shinyApp(ui, server)