简体   繁体   中英

Shiny update data from selectInput

I am facing an issue in getting the output of selectizeInput to update the uploaded csv data. The specific code block having issue is

observeEvent(input$Go, {
    tmp <- values$df_data
    output$Grp = renderUI({

      lvls <- tmp %>%
        pull(Group) %>%
        levels

      selectizeInput('group', 'Groups', choices = lvls, selected = lvls[1], multiple = FALSE)


    })



    tmp1 <- tmp %>% 
      filter(Group == "A") # need to change here by updating the value from input$Grp
    values$df_data <- tmp1


  })

The 'Group' column is filtered with a hard coded value. My intention is to make it dynamic by getting the values of "Grp". Image below shows the issue

enter image description here

The full code is below

library(shinydashboard)
 library(shiny)
 library(dplyr)
 library(tidyr) 

ui = dashboardPage(
   dashboardHeader(),
   dashboardSidebar( 
     width = 200,
     sidebarMenu(
       menuItem("File Upload", icon = shiny::icon("table")),

       fileInput('datafile', 'Choose CSV file',
                 accept=c('text/csv', 'text/comma-separated-values,text/plain')),
       uiOutput('Grp'),
       actionButton("Go","Subset Data"))
   ),

   dashboardBody( fluidPage(


     titlePanel("Data Table"),


     sidebarLayout(
       tabPanel("tab",
                div( id ="Sidebar",sidebarPanel(

                ))), 


       mainPanel(
                 DT::dataTableOutput("tableOut")


       )
     )
   )
   )
 )

server = function(input, output, session) {




  values <- reactiveValues(df_data = NULL)

  observeEvent(input$datafile, {
    values$df_data <- read.csv(input$datafile$datapath)


  })

  observeEvent(input$Go, {
    tmp <- values$df_data
    output$Grp = renderUI({

      lvls <- tmp %>%
        pull(Group) %>%
        levels

      selectizeInput('group', 'Groups', choices = lvls, selected = lvls[1], multiple = FALSE)


    })

    tmp1 <- tmp %>% 
      filter(Group == "A") # need to change here by updating the value from input$Grp
    values$df_data <- tmp1


  })

  output$tableOut <- DT::renderDataTable({

    DT::datatable(values$df_data)



  })


}

shinyApp(ui = ui, server = server)

I am uploading .csv file. The below data is similar to the one I have.

set.seed(12345)
data <- data.frame(DATE = rep(seq(as.Date("2014-01-01"), length.out = 200,
by = "week"), each = 4), Group = rep(c("A", "B", "C", "D"), each = 200),
       ValueColumn = rnorm(800))

welcome to the site here. This is also a problem I have had recently -- it does not seem to be isolated to selectizeInput but pertains to any new data that you are trying to get your Shiny app to "see". Here are a couple things to try:

  1. Delete any _cache directories in the root of your shiny server, then try to load again. If there is no difference,
  2. touch the .csv files (or make an edit if on Windows, to get a new timestamp) and then try to load again. If there is no difference,
  3. Restart the shiny server with sudo systemctl restart shiny-server where the last argument is the name of your shiny server (default is shiny-server )

This last solution is the one that worked most reliably for me -- it is #2 from this document ( Shiny app does not reflect changes in update RData file ) -- to restart the shiny server from the command line. This worked reliably and routinely. The other ideas in this solution did not work on my machine so must be dependent on your server or client specifications.

The second thing that worked for me was to use reactiveTimer ( https://shiny.rstudio.com/reference/shiny/0.14/reactiveTimer.html ).

In any case, if you have been beating your head against a wall with this, it is a tricky issue and is troublesome for many of us. Good luck with picking a best solution for your context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM