I am trying to create a shiny app that will create flextables based on the 'Species' chosen . However I am running into an error (picture below) when running my code.
data
HSI.list = list(`Riverine Arctic Grayling` = structure(list(Species = c("ARGR",
"ARGR", "ARGR", "ARGR", "ARGR", "ARGR", "ARGR", "ARGR", "ARGR",
"ARGR", "ARGR", "ARGR"), `Model Variable` = c("V1", "V2", "V3",
"V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "HSI"), value = c("0.857138",
"1", "0.5", "not limiting", "0.1", "0.233333333333333", "not limiting",
"1", "0.333333333333333", "1", "1", "0.1"), `Figure Caption` = c("Riverine Arctic Grayling",
"Riverine Arctic Grayling", "Riverine Arctic Grayling", "Riverine Arctic Grayling",
"Riverine Arctic Grayling", "Riverine Arctic Grayling", "Riverine Arctic Grayling",
"Riverine Arctic Grayling", "Riverine Arctic Grayling", "Riverine Arctic Grayling",
"Riverine Arctic Grayling", "Riverine Arctic Grayling"), River_Lake = c("River",
"River", "River", "River", "River", "River", "River", "River",
"River", "River", "River", "River"), old_new = c("old", "old",
"old", "old", "old", "old", "old", "old", "old", "old", "old",
"old")), row.names = c(NA, -12L), class = c("tbl_df", "tbl",
"data.frame"), na.action = structure(17:201, names = c("17",
"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50",
"51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61",
"62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72",
"73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83",
"84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94",
"95", "96", "97", "98", "99", "100", "101", "102", "103", "104",
"105", "106", "107", "108", "109", "110", "111", "112", "113",
"114", "115", "116", "117", "118", "119", "120", "121", "122",
"123", "124", "125", "126", "127", "128", "129", "130", "131",
"132", "133", "134", "135", "136", "137", "138", "139", "140",
"141", "142", "143", "144", "145", "146", "147", "148", "149",
"150", "151", "152", "153", "154", "155", "156", "157", "158",
"159", "160", "161", "162", "163", "164", "165", "166", "167",
"168", "169", "170", "171", "172", "173", "174", "175", "176",
"177", "178", "179", "180", "181", "182", "183", "184", "185",
"186", "187", "188", "189", "190", "191", "192", "193", "194",
"195", "196", "197", "198", "199", "200", "201"), class = "omit")),
`Riverine Emerald Shiner` = structure(list(Species = c("EMSH",
"EMSH", "EMSH", "EMSH"), `Model Variable` = c("V3", "V5",
"V7", "HSI"), value = c("0.6", "1", "1", "0.6"), `Figure Caption` = c("Riverine Emerald Shiner",
"Riverine Emerald Shiner", "Riverine Emerald Shiner", "Riverine Emerald Shiner"
), River_Lake = c("River", "River", "River", "River"), old_new = c("old",
"old", "old", "old")), row.names = c(NA, -4L), class = c("tbl_df",
"tbl", "data.frame"), na.action = structure(17:201, names = c("17",
"18", "19", "20", "21", "22", "23", "24", "25", "26", "27",
"28", "29", "30", "31", "32", "33", "34", "35", "36", "37",
"38", "39", "40", "41", "42", "43", "44", "45", "46", "47",
"48", "49", "50", "51", "52", "53", "54", "55", "56", "57",
"58", "59", "60", "61", "62", "63", "64", "65", "66", "67",
"68", "69", "70", "71", "72", "73", "74", "75", "76", "77",
"78", "79", "80", "81", "82", "83", "84", "85", "86", "87",
"88", "89", "90", "91", "92", "93", "94", "95", "96", "97",
"98", "99", "100", "101", "102", "103", "104", "105", "106",
"107", "108", "109", "110", "111", "112", "113", "114", "115",
"116", "117", "118", "119", "120", "121", "122", "123", "124",
"125", "126", "127", "128", "129", "130", "131", "132", "133",
"134", "135", "136", "137", "138", "139", "140", "141", "142",
"143", "144", "145", "146", "147", "148", "149", "150", "151",
"152", "153", "154", "155", "156", "157", "158", "159", "160",
"161", "162", "163", "164", "165", "166", "167", "168", "169",
"170", "171", "172", "173", "174", "175", "176", "177", "178",
"179", "180", "181", "182", "183", "184", "185", "186", "187",
"188", "189", "190", "191", "192", "193", "194", "195", "196",
"197", "198", "199", "200", "201"), class = "omit")))
Code for creating Shiny
note:
The code
x2 = input$sp
x = x2 %>% select(-Species, -`Figure Caption`, -River_Lake, -old_new)
is included because I dont want the - columns in the final flextable and x2 is used to specify the table caption using the Figure Caption
column
library(flextable)
library(shiny)
ui <- fluidPage(
titlePanel("HSI"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "sp",
label = "Choose Species:",
choices = c("ARGR", "EMSH"))
),
mainPanel(uiOutput("my_ft"))
)
)
# Define server logic to summarize and view selected dataset ----
server <- function(input, output) {
spInput <- reactive({
switch(input$sp,
"ARGR" = HSI.list$`Riverine Arctic Grayling`,
"EMSH" = HSI.list$`Riverine Emerald Shiner`)
})
x2 = input$sp
x = x2 %>% select(-Species, -`Figure Caption`, -River_Lake, -old_new)
output$my_ft <- renderUI({
flextable(x, cwidth = c(1,1) ) %>% bg(i = NULL, j = NULL, bg = "#333D47", part = "header") %>%
color(i = NULL, j = NULL, color = "white", part = "header") %>%
bold(i = NULL, j = NULL, bold = TRUE, part = "header") %>%
fontsize(i = NULL, j = NULL, size = 10, part = "all") %>% theme_box() %>% set_caption(caption = as_paragraph(as_chunk(paste0("XXX for ", unique(x2$`Figure Caption`)), props = fp_text_default(font.size = 10, font.family = "Arial", bold = TRUE)))) %>%
font(i = NULL, j = NULL, fontname = fontname, part = "all") %>%
htmltools_value()
})
}
shinyApp(ui = ui, server = server)
Example of table output I want to show in shiny if ARGR was specified.
The issue is that you are trying to access the value of an input
outside of a reactive context, i.e. input$...
can only be used inside a reactive
or observe
r or a renderXXX
function.
Hence, to fix you issue move the data wrangling inside the renderUI
. Additionally I fixed some additional issue, i.e. there is no variable x
in your code. Instead use the value of the reactive
spInput
.
library(flextable)
library(shiny)
library(dplyr, warn = FALSE)
ui <- fluidPage(
titlePanel("HSI"),
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "sp",
label = "Choose Species:",
choices = c("ARGR", "EMSH")
)
),
mainPanel(uiOutput("my_ft"))
)
)
# Define server logic to summarize and view selected dataset ----
server <- function(input, output) {
spInput <- reactive({
switch(input$sp,
"ARGR" = HSI.list$`Riverine Arctic Grayling`,
"EMSH" = HSI.list$`Riverine Emerald Shiner`
)
})
output$my_ft <- renderUI({
caption <- unique(spInput()$`Figure Caption`)
fontname <- "Arial"
x <- spInput() %>%
select(-Species, -`Figure Caption`, -River_Lake, -old_new)
flextable(x, cwidth = c(1, 1)) %>%
bg(i = NULL, j = NULL, bg = "#333D47", part = "header") %>%
color(i = NULL, j = NULL, color = "white", part = "header") %>%
bold(i = NULL, j = NULL, bold = TRUE, part = "header") %>%
fontsize(i = NULL, j = NULL, size = 10, part = "all") %>%
theme_box() %>%
set_caption(caption = as_paragraph(
as_chunk(paste0("XXX for ", caption),
props = fp_text_default(font.size = 10, font.family = "Arial", bold = TRUE)
)
)) %>%
font(i = NULL, j = NULL, fontname = fontname, part = "all") %>%
htmltools_value()
})
}
shinyApp(ui = ui, server = server)
#>
#> Listening on http://127.0.0.1:8267